Skip to content

Commit 27c8bc8

Browse files
add option to send speed tests and get results
1 parent cdafe8d commit 27c8bc8

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
@@ -40,7 +40,7 @@
4040
UnknownException,
4141
UnknownPathException,
4242
)
43-
from .models import Device, DeviceInfo, PortMapping
43+
from .models import Device, DeviceInfo, PortMapping, SpeedTestResult
4444

4545

4646
class SagemcomClient:
@@ -168,7 +168,7 @@ def __get_response_value(self, response, index=0):
168168

169169
return value
170170

171-
async def __api_request_async(self, actions, priority=False):
171+
async def __api_request_async(self, actions, priority=False, **request_kwargs):
172172
"""Build request to the internal JSON-req API."""
173173
self.__generate_request_id()
174174
self.__generate_nonce()
@@ -188,7 +188,9 @@ async def __api_request_async(self, actions, priority=False):
188188
}
189189

190190
async with self.session.post(
191-
api_host, data="req=" + json.dumps(payload, separators=(",", ":"))
191+
api_host,
192+
data="req=" + json.dumps(payload, separators=(",", ":")),
193+
**request_kwargs,
192194
) as response:
193195

194196
if response.status == 400:
@@ -408,3 +410,35 @@ async def reboot(self):
408410
data = self.__get_response_value(response)
409411

410412
return data
413+
414+
async def run_speed_test(self, block_traffic: bool = False):
415+
"""Run Speed Test on Sagemcom F@st device."""
416+
actions = [
417+
{
418+
"id": 0,
419+
"method": "speedTestClient",
420+
"xpath": "Device/IP/Diagnostics/SpeedTest",
421+
"parameters": {"BlockTraffic": block_traffic},
422+
}
423+
]
424+
return await self.__api_request_async(actions, False, timeout=100)
425+
426+
async def get_speed_test_results(self):
427+
"""Retrieve Speed Test results from Sagemcom F@st device."""
428+
ret = await self.get_value_by_xpath("Device/IP/Diagnostics/SpeedTest")
429+
history = ret["speed_test"]["history"]
430+
if history:
431+
timestamps = (int(k) for k in history["timestamp"].split(","))
432+
server_address = history["selected_server_address"].split(",")
433+
block_traffic = history["block_traffic"].split(",")
434+
latency = history["latency"].split(",")
435+
upload = (float(k) for k in history["upload"].split(","))
436+
download = (float(k) for k in history["download"].split(","))
437+
results = [
438+
SpeedTestResult(*data)
439+
for data in zip(
440+
timestamps, server_address, block_traffic, latency, upload, download
441+
)
442+
]
443+
return results
444+
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)