Skip to content

Commit f210ad9

Browse files
authored
Merge pull request #1 from djGrrr/master
Merge djGrrr changes
2 parents 9441fdd + 2563beb commit f210ad9

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The Sagemcom F@st series is used by multiple cable companies, where some cable c
2828
| Sagemcom F@st 5370e | Telia | sha512 | |
2929
| Sagemcom F@st 5566 | Bell (Home Hub 3000) | md5 | username: guest, password: "" |
3030
| Sagemcom F@st 5689 | Bell (Home Hub 4000) | md5 | username: admin, password: "" |
31+
| Sagemcom F@st 5689E | Bell (Giga Hub) | sha512 | username: admin, password: "" |
3132
| Sagemcom F@st 5655V2 | MásMóvil | md5 | |
3233
| Sagemcom F@st 5657IL | | md5 | |
3334
| Speedport Pro | Telekom | md5 | username: admin |

sagemcom_api/client.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(
5555
session: ClientSession = None,
5656
ssl=False,
5757
verify_ssl=True,
58+
keep_keys=False,
5859
):
5960
"""
6061
Create a SagemCom client.
@@ -69,6 +70,7 @@ def __init__(
6970
self.username = username
7071
self.authentication_method = authentication_method
7172
self._password_hash = self.__generate_hash(password)
73+
self.keep_keys = keep_keys
7274

7375
self.protocol = "https" if ssl else "http"
7476

@@ -156,15 +158,16 @@ def __get_response(self, response, index=0):
156158

157159
return value
158160

159-
def __get_response_value(self, response, index=0):
161+
def __get_response_value(self, response, index=0, keep_keys = None):
160162
"""Retrieve response value from value."""
161163
try:
162164
value = self.__get_response(response, index)["value"]
163165
except KeyError:
164166
value = None
165167

166168
# Rewrite result to snake_case
167-
value = humps.decamelize(value)
169+
if (keep_keys is not None and not keep_keys) or (keep_keys is None and not self.keep_keys):
170+
value = humps.decamelize(value)
168171

169172
return value
170173

@@ -293,7 +296,7 @@ async def logout(self):
293296
self._request_id = -1
294297

295298
async def get_value_by_xpath(
296-
self, xpath: str, options: Optional[Dict] = {}
299+
self, xpath: str, options: Optional[Dict] = {}, keep_keys = None
297300
) -> Dict:
298301
"""
299302
Retrieve raw value from router using XPath.
@@ -309,11 +312,11 @@ async def get_value_by_xpath(
309312
}
310313

311314
response = await self.__api_request_async([actions], False)
312-
data = self.__get_response_value(response)
315+
data = self.__get_response_value(response, keep_keys = keep_keys)
313316

314317
return data
315318

316-
async def get_values_by_xpaths(self, xpaths, options: Optional[Dict] = {}) -> Dict:
319+
async def get_values_by_xpaths(self, xpaths, options: Optional[Dict] = {}, keep_keys = None) -> Dict:
317320
"""
318321
Retrieve raw values from router using XPath.
319322
@@ -331,7 +334,7 @@ async def get_values_by_xpaths(self, xpaths, options: Optional[Dict] = {}) -> Di
331334
]
332335

333336
response = await self.__api_request_async(actions, False)
334-
values = [self.__get_response_value(response, i) for i in range(len(xpaths))]
337+
values = [self.__get_response_value(response, i, keep_keys = keep_keys) for i in range(len(xpaths))]
335338
data = dict(zip(xpaths.keys(), values))
336339

337340
return data
@@ -349,7 +352,7 @@ async def set_value_by_xpath(
349352
actions = {
350353
"id": 0,
351354
"method": "setValue",
352-
"xpath": xpath,
355+
"xpath": urllib.parse.quote(xpath),
353356
"parameters": {"value": str(value)},
354357
"options": options,
355358
}
@@ -361,7 +364,7 @@ async def set_value_by_xpath(
361364
async def get_device_info(self) -> DeviceInfo:
362365
"""Retrieve information about Sagemcom F@st device."""
363366
try:
364-
data = await self.get_value_by_xpath("Device/DeviceInfo")
367+
data = await self.get_value_by_xpath("Device/DeviceInfo", keep_keys = False)
365368
return DeviceInfo(**data.get("device_info"))
366369
except UnknownPathException:
367370
data = await self.get_values_by_xpaths(
@@ -380,7 +383,7 @@ async def get_device_info(self) -> DeviceInfo:
380383

381384
async def get_hosts(self, only_active: Optional[bool] = False) -> List[Device]:
382385
"""Retrieve hosts connected to Sagemcom F@st device."""
383-
data = await self.get_value_by_xpath("Device/Hosts/Hosts")
386+
data = await self.get_value_by_xpath("Device/Hosts/Hosts", keep_keys = False)
384387
devices = [Device(**d) for d in data]
385388

386389
if only_active:
@@ -391,7 +394,7 @@ async def get_hosts(self, only_active: Optional[bool] = False) -> List[Device]:
391394

392395
async def get_port_mappings(self) -> List[PortMapping]:
393396
"""Retrieve configured Port Mappings on Sagemcom F@st device."""
394-
data = await self.get_value_by_xpath("Device/NAT/PortMappings")
397+
data = await self.get_value_by_xpath("Device/NAT/PortMappings", keep_keys = False)
395398
port_mappings = [PortMapping(**p) for p in data]
396399

397400
return port_mappings
@@ -405,6 +408,6 @@ async def reboot(self):
405408
}
406409

407410
response = await self.__api_request_async([action], False)
408-
data = self.__get_response_value(response)
411+
data = self.__get_response_value(response, keep_keys = False)
409412

410413
return data

0 commit comments

Comments
 (0)