Skip to content

Commit 4201c14

Browse files
committed
refactor: Always use time.monotnic
1 parent 6777dd7 commit 4201c14

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

roborock/api.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import logging
88
import secrets
99
import time
10-
from collections.abc import Callable, Coroutine
10+
from collections.abc import Coroutine
1111
from typing import Any
1212

1313
from .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

roborock/version_1_apis/roborock_client_v1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def _get_payload(
361361

362362
def on_message_received(self, messages: list[RoborockMessage]) -> None:
363363
try:
364-
self._last_device_msg_in = self.time_func()
364+
self._last_device_msg_in = time.monotonic()
365365
for data in messages:
366366
protocol = data.protocol
367367
if data.payload and protocol in [

0 commit comments

Comments
 (0)