Skip to content

Commit

Permalink
adds telephone list support #53
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo committed Sep 17, 2022
1 parent 92207d9 commit 508751f
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 40 deletions.
38 changes: 31 additions & 7 deletions fritzinfluxdb/classes/fritzbox/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from fritzinfluxdb.classes.fritzbox.service_handler import FritzBoxTR069Service, FritzBoxLuaService, FritzBoxLuaURLPath
from fritzinfluxdb.classes.fritzbox.services_tr069 import fritzbox_services as tr069_services
from fritzinfluxdb.classes.fritzbox.services_lua import fritzbox_services as lua_services
from fritzinfluxdb.classes.fritzbox.services_homeauto import fritzbox_services as homeauto_services
from fritzinfluxdb.classes.fritzbox.services_lua_homeauto import fritzbox_services as homeauto_services
from fritzinfluxdb.classes.fritzbox.services_lua_telephone_list import fritzbox_services as telephopne_services
from fritzinfluxdb.classes.common import FritzMeasurement
from fritzinfluxdb.common import grab

Expand Down Expand Up @@ -265,9 +266,12 @@ def __init__(self, config):
# parse services from fritzbox/services_lua.py
self.add_services(FritzBoxLuaService, lua_services)

# parse services from fritzbox/services_homeauto.py
# parse services from fritzbox/services_lua_homeauto.py
self.add_services(FritzBoxLuaService, homeauto_services)

# parse services from fritzbox/services_lua_telephone_list.py
self.add_services(FritzBoxLuaService, telephopne_services)

def connect(self):

if self.sid is not None:
Expand Down Expand Up @@ -334,29 +338,40 @@ def request(self, service_to_request, additional_params):
"sid": self.sid
}

# appending additional params
if isinstance(additional_params, dict):
params = {**params, **additional_params}

# basic function call attributes
call_attributes = {
"timeout": self.config.connect_timeout
}

if service_to_request.url_path == FritzBoxLuaURLPath.data:
params = {**params, **{
"page": service_to_request.page,
"lang": "de"
}}
call_attributes["data"] = params
session_handler = self.session.post

elif service_to_request.url_path == FritzBoxLuaURLPath.homeautomation:
params["switchcmd"] = service_to_request.switchcmd
session_handler = self.session.get
call_attributes["params"] = params

elif service_to_request.url_path == FritzBoxLuaURLPath.foncalls_list:
session_handler = self.session.get
call_attributes["params"] = params
else:
log.error(f"The URL path '{service_to_request.url_path}' defined for "
f"{service_to_request.name} is not supported.")
return result

if isinstance(additional_params, dict):
params = {**params, **additional_params}

data_url = f"{self.url}{service_to_request.url_path}"

try:
response = session_handler(data_url, timeout=self.config.connect_timeout, data=params)
response = session_handler(data_url, **call_attributes)
except Exception as e:
log.error(f"Unable to perform request to '{data_url}': {e}")
return
Expand All @@ -371,8 +386,17 @@ def request(self, service_to_request, additional_params):
result = xmltodict.parse(response.content)
except ExpatError:
pass
elif service_to_request.url_path == FritzBoxLuaURLPath.foncalls_list:
filter_strings = ['sep=;', 'Typ;Datum;Name;Rufnummer;Nebenstelle;Eigene Rufnummer;Dauer', '']
try:
result = [x for x in response.text.split("\n") if x not in filter_strings]
except ExpatError:
pass
else:
print(service_to_request.url_path)
result = response.content

# Debugging purposes
# log.debug(f"Response: {response.content}")

if response.status_code == 200 and result is not None:
log.debug(f"{self.name} request successful")
Expand Down
3 changes: 3 additions & 0 deletions fritzinfluxdb/classes/fritzbox/service_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def add_action(self, action: Union[AnyStr, Dict] = None) -> None:
class FritzBoxLuaURLPath:
data = "/data.lua"
homeautomation = "/webservices/homeautoswitch.lua"
foncalls_list = "/fon_num/foncalls_list.lua"


class FritzBoxLuaService(FritzBoxService):
Expand Down Expand Up @@ -169,6 +170,8 @@ def __init__(self, service_data=None):
if self.switchcmd is None:
do_error_exit(f"FritzBoxLuaService '{self.name}' instance has no 'switchcmd' defined")
return
elif url_path == FritzBoxLuaURLPath.foncalls_list:
pass
else:
do_error_exit(f"FritzBoxLuaService '{self.name}' instance has has unsupported url_path: {url_path}")

Expand Down
Loading

0 comments on commit 508751f

Please sign in to comment.