4141 UnknownException ,
4242 UnknownPathException ,
4343)
44- from .models import Device , DeviceInfo , PortMapping
44+ from .models import Device , DeviceInfo , PortMapping , SpeedTestResult
4545
4646
4747class 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 []
0 commit comments