Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions roborock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import secrets
import time
from collections.abc import Callable, Coroutine
from collections.abc import Coroutine
from typing import Any

from .containers import (
Expand Down Expand Up @@ -36,8 +36,8 @@ def __init__(self, endpoint: str, device_info: DeviceData, queue_timeout: int =
self._endpoint = endpoint
self._nonce = secrets.token_bytes(16)
self._waiting_queue: dict[int, RoborockFuture] = {}
self._last_device_msg_in = self.time_func()
self._last_disconnection = self.time_func()
self._last_device_msg_in = time.monotonic()
self._last_disconnection = time.monotonic()
self.keep_alive = KEEPALIVE
self._diagnostic_data: dict[str, dict[str, Any]] = {
"misc_info": {"Nonce": base64.b64encode(self._nonce).decode("utf-8")}
Expand All @@ -59,15 +59,6 @@ async def async_release(self) -> None:
def diagnostic_data(self) -> dict:
return self._diagnostic_data

@property
def time_func(self) -> Callable[[], float]:
try:
# Use monotonic clock if available
time_func = time.monotonic
except AttributeError:
time_func = time.time
return time_func

async def async_connect(self):
raise NotImplementedError

Expand All @@ -81,13 +72,13 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
raise NotImplementedError

def on_connection_lost(self, exc: Exception | None) -> None:
self._last_disconnection = self.time_func()
self._last_disconnection = time.monotonic()
self._logger.info("Roborock client disconnected")
if exc is not None:
self._logger.warning(exc)

def should_keepalive(self) -> bool:
now = self.time_func()
now = time.monotonic()
# noinspection PyUnresolvedReferences
if now - self._last_disconnection > self.keep_alive**2 and now - self._last_device_msg_in > self.keep_alive:
return False
Expand Down
2 changes: 1 addition & 1 deletion roborock/version_1_apis/roborock_client_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def _get_payload(

def on_message_received(self, messages: list[RoborockMessage]) -> None:
try:
self._last_device_msg_in = self.time_func()
self._last_device_msg_in = time.monotonic()
for data in messages:
protocol = data.protocol
if data.payload and protocol in [
Expand Down
Loading