77import logging
88import secrets
99import time
10- from collections .abc import Callable , Coroutine
10+ from collections .abc import Coroutine
1111from typing import Any
1212
1313from .containers import (
@@ -36,8 +36,8 @@ def __init__(self, endpoint: str, device_info: DeviceData, queue_timeout: int =
3636 self ._endpoint = endpoint
3737 self ._nonce = secrets .token_bytes (16 )
3838 self ._waiting_queue : dict [int , RoborockFuture ] = {}
39- self ._last_device_msg_in = self . time_func ()
40- self ._last_disconnection = self . time_func ()
39+ self ._last_device_msg_in = time . monotonic ()
40+ self ._last_disconnection = time . monotonic ()
4141 self .keep_alive = KEEPALIVE
4242 self ._diagnostic_data : dict [str , dict [str , Any ]] = {
4343 "misc_info" : {"Nonce" : base64 .b64encode (self ._nonce ).decode ("utf-8" )}
@@ -59,15 +59,6 @@ async def async_release(self) -> None:
5959 def diagnostic_data (self ) -> dict :
6060 return self ._diagnostic_data
6161
62- @property
63- def time_func (self ) -> Callable [[], float ]:
64- try :
65- # Use monotonic clock if available
66- time_func = time .monotonic
67- except AttributeError :
68- time_func = time .time
69- return time_func
70-
7162 async def async_connect (self ):
7263 raise NotImplementedError
7364
@@ -81,13 +72,13 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
8172 raise NotImplementedError
8273
8374 def on_connection_lost (self , exc : Exception | None ) -> None :
84- self ._last_disconnection = self . time_func ()
75+ self ._last_disconnection = time . monotonic ()
8576 self ._logger .info ("Roborock client disconnected" )
8677 if exc is not None :
8778 self ._logger .warning (exc )
8879
8980 def should_keepalive (self ) -> bool :
90- now = self . time_func ()
81+ now = time . monotonic ()
9182 # noinspection PyUnresolvedReferences
9283 if now - self ._last_disconnection > self .keep_alive ** 2 and now - self ._last_device_msg_in > self .keep_alive :
9384 return False
0 commit comments