Skip to content

Commit 18aee63

Browse files
authored
Merge pull request #6 from alexandrehassan/additions
Additions
2 parents 4ce5db8 + 27c8bc8 commit 18aee63

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

sagemcom_api/client.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
UnknownException,
4242
UnknownPathException,
4343
)
44-
from .models import Device, DeviceInfo, PortMapping
44+
from .models import Device, DeviceInfo, PortMapping, SpeedTestResult
4545

4646

4747
class SagemcomClient:
@@ -172,7 +172,7 @@ def __get_response_value(self, response, index=0, keep_keys = None):
172172

173173
return value
174174

175-
async def __api_request_async(self, actions, priority=False):
175+
async def __api_request_async(self, actions, priority=False, **request_kwargs):
176176
"""Build request to the internal JSON-req API."""
177177
self.__generate_request_id()
178178
self.__generate_nonce()
@@ -192,7 +192,9 @@ async def __api_request_async(self, actions, priority=False):
192192
}
193193

194194
async with self.session.post(
195-
api_host, data="req=" + json.dumps(payload, separators=(",", ":"))
195+
api_host,
196+
data="req=" + json.dumps(payload, separators=(",", ":")),
197+
**request_kwargs,
196198
) as response:
197199

198200
if response.status == 400:
@@ -435,3 +437,35 @@ async def reboot(self):
435437
data = self.__get_response_value(response, keep_keys = False)
436438

437439
return data
440+
441+
async def run_speed_test(self, block_traffic: bool = False):
442+
"""Run Speed Test on Sagemcom F@st device."""
443+
actions = [
444+
{
445+
"id": 0,
446+
"method": "speedTestClient",
447+
"xpath": "Device/IP/Diagnostics/SpeedTest",
448+
"parameters": {"BlockTraffic": block_traffic},
449+
}
450+
]
451+
return await self.__api_request_async(actions, False, timeout=100)
452+
453+
async def get_speed_test_results(self):
454+
"""Retrieve Speed Test results from Sagemcom F@st device."""
455+
ret = await self.get_value_by_xpath("Device/IP/Diagnostics/SpeedTest")
456+
history = ret["speed_test"]["history"]
457+
if history:
458+
timestamps = (int(k) for k in history["timestamp"].split(","))
459+
server_address = history["selected_server_address"].split(",")
460+
block_traffic = history["block_traffic"].split(",")
461+
latency = history["latency"].split(",")
462+
upload = (float(k) for k in history["upload"].split(","))
463+
download = (float(k) for k in history["download"].split(","))
464+
results = [
465+
SpeedTestResult(*data)
466+
for data in zip(
467+
timestamps, server_address, block_traffic, latency, upload, download
468+
)
469+
]
470+
return results
471+
return []

sagemcom_api/models.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dataclasses
44
from dataclasses import dataclass
5+
import time
56
from typing import Any, List, Optional
67

78

@@ -162,3 +163,30 @@ def __init__(self, **kwargs):
162163
def id(self):
163164
"""Return unique ID for port mapping."""
164165
return self.uid
166+
167+
168+
@dataclass
169+
class SpeedTestResult:
170+
"""Representation of a speedtest result."""
171+
172+
timestamp: str
173+
selected_server_address: str
174+
block_traffic: bool
175+
latency: str
176+
upload: str
177+
download: str
178+
179+
def __post_init__(self):
180+
"""Process data after init."""
181+
# Convert timestamp to datetime object.
182+
self.timestamp = time.strftime(
183+
"%Y-%m-%d %H:%M:%S", time.localtime(self.timestamp)
184+
)
185+
self.block_traffic = bool(self.block_traffic)
186+
187+
def __str__(self) -> str:
188+
"""Return string representation of speedtest result."""
189+
return (
190+
f"timestamp: {self.timestamp}, latency: {self.latency}, "
191+
f"upload: {self.upload}, download: {self.download}"
192+
)

0 commit comments

Comments
 (0)