Skip to content

Commit 5e67fa6

Browse files
authored
feat: add datetime parsing in cleanrecord (#119)
* feat: add datetime parsing in cleanrecord * fix: timezone for non-3.11 * feat: add is_available for ha and here in future * fix: add timeout as a variable and set a longer default timeout for cloud
1 parent 360b240 commit 5e67fa6

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

roborock/api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070

7171
_LOGGER = logging.getLogger(__name__)
7272
KEEPALIVE = 60
73-
QUEUE_TIMEOUT = 4
7473
COMMANDS_SECURED = [
7574
RoborockCommand.GET_MAP_V1,
7675
RoborockCommand.GET_MULTI_MAP,
@@ -166,7 +165,7 @@ async def refresh_value(self):
166165

167166

168167
class RoborockClient:
169-
def __init__(self, endpoint: str, device_info: DeviceData) -> None:
168+
def __init__(self, endpoint: str, device_info: DeviceData, queue_timeout: int = 4) -> None:
170169
self.event_loop = get_running_loop_or_create_one()
171170
self.device_info = device_info
172171
self._endpoint = endpoint
@@ -185,6 +184,8 @@ def __init__(self, endpoint: str, device_info: DeviceData) -> None:
185184
device_cache[device_info.device.duid] = cache
186185
self.cache: dict[CacheableAttribute, AttributeCache] = cache
187186
self._listeners: list[Callable[[str, CacheableAttribute, RoborockBase], None]] = []
187+
self.is_available: bool = False
188+
self.queue_timeout = queue_timeout
188189

189190
def __del__(self) -> None:
190191
self.release()
@@ -322,12 +323,12 @@ async def validate_connection(self) -> None:
322323

323324
async def _wait_response(self, request_id: int, queue: RoborockFuture) -> tuple[Any, VacuumError | None]:
324325
try:
325-
(response, err) = await queue.async_get(QUEUE_TIMEOUT)
326+
(response, err) = await queue.async_get(self.queue_timeout)
326327
if response == "unknown_method":
327328
raise UnknownMethodError("Unknown method")
328329
return response, err
329330
except (asyncio.TimeoutError, asyncio.CancelledError):
330-
raise RoborockTimeout(f"id={request_id} Timeout after {QUEUE_TIMEOUT} seconds") from None
331+
raise RoborockTimeout(f"id={request_id} Timeout after {self.queue_timeout} seconds") from None
331332
finally:
332333
self._waiting_queue.pop(request_id, None)
333334

roborock/cloud_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ class RoborockMqttClient(RoborockClient, mqtt.Client):
2929
_thread: threading.Thread
3030
_client_id: str
3131

32-
def __init__(self, user_data: UserData, device_info: DeviceData) -> None:
32+
def __init__(self, user_data: UserData, device_info: DeviceData, queue_timeout: int = 10) -> None:
3333
rriot = user_data.rriot
3434
if rriot is None:
3535
raise RoborockException("Got no rriot data from user_data")
3636
endpoint = base64.b64encode(Utils.md5(rriot.k.encode())[8:14]).decode()
37-
RoborockClient.__init__(self, endpoint, device_info)
37+
RoborockClient.__init__(self, endpoint, device_info, queue_timeout)
3838
mqtt.Client.__init__(self, protocol=mqtt.MQTTv5)
3939
self._logger = RoborockLoggerAdapter(device_info.device.name, _LOGGER)
4040
self._mqtt_user = rriot.u

roborock/containers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import re
66
from dataclasses import asdict, dataclass
7+
from datetime import timezone
78
from enum import Enum
89
from typing import Any, NamedTuple
910

@@ -408,7 +409,9 @@ def __post_init__(self) -> None:
408409
@dataclass
409410
class CleanRecord(RoborockBase):
410411
begin: int | None = None
412+
begin_datetime: datetime.datetime | None = None
411413
end: int | None = None
414+
end_datetime: datetime.datetime | None = None
412415
duration: int | None = None
413416
area: int | None = None
414417
square_meter_area: float | None = None
@@ -424,6 +427,10 @@ class CleanRecord(RoborockBase):
424427

425428
def __post_init__(self) -> None:
426429
self.square_meter_area = round(self.area / 1000000, 1) if self.area is not None else None
430+
self.begin_datetime = (
431+
datetime.datetime.fromtimestamp(self.begin).astimezone(timezone.utc) if self.begin else None
432+
)
433+
self.end_datetime = datetime.datetime.fromtimestamp(self.end).astimezone(timezone.utc) if self.end else None
427434

428435

429436
@dataclass

roborock/local_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import async_timeout
88

99
from . import DeviceData
10-
from .api import COMMANDS_SECURED, QUEUE_TIMEOUT, RoborockClient
10+
from .api import COMMANDS_SECURED, RoborockClient
1111
from .exceptions import CommandVacuumError, RoborockConnectionException, RoborockException
1212
from .protocol import MessageParser
1313
from .roborock_message import MessageRetry, RoborockMessage, RoborockMessageProtocol
@@ -18,10 +18,10 @@
1818

1919

2020
class RoborockLocalClient(RoborockClient, asyncio.Protocol):
21-
def __init__(self, device_data: DeviceData):
21+
def __init__(self, device_data: DeviceData, queue_timeout: int = 4):
2222
if device_data.host is None:
2323
raise RoborockException("Host is required")
24-
super().__init__("abc", device_data)
24+
super().__init__("abc", device_data, queue_timeout)
2525
self.host = device_data.host
2626
self._batch_structs: list[RoborockMessage] = []
2727
self._executing = False
@@ -58,7 +58,7 @@ async def async_connect(self) -> None:
5858
try:
5959
if not self.is_connected():
6060
self.sync_disconnect()
61-
async with async_timeout.timeout(QUEUE_TIMEOUT):
61+
async with async_timeout.timeout(self.queue_timeout):
6262
self._logger.info(f"Connecting to {self.host}")
6363
self.transport, _ = await self.event_loop.create_connection( # type: ignore
6464
lambda: self, self.host, 58867

0 commit comments

Comments
 (0)