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