From 8f4b7d376d5a475798782496ea52ac9674cb9ae7 Mon Sep 17 00:00:00 2001 From: Luke Lashley Date: Mon, 18 Sep 2023 20:45:29 -0400 Subject: [PATCH] fix: status reworking (#121) * fix: is_available true by default * fix: status type as class variable * fix: don't update status when it was none before listener * fix: reduce info logs --- roborock/api.py | 22 +++++++++++----------- roborock/cloud_api.py | 2 +- roborock/local_api.py | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/roborock/api.py b/roborock/api.py index 3b396db..4dd3d9f 100644 --- a/roborock/api.py +++ b/roborock/api.py @@ -184,12 +184,18 @@ def __init__(self, endpoint: str, device_info: DeviceData, queue_timeout: int = device_cache[device_info.device.duid] = cache self.cache: dict[CacheableAttribute, AttributeCache] = cache self._listeners: list[Callable[[str, CacheableAttribute, RoborockBase], None]] = [] - self.is_available: bool = False + self.is_available: bool = True self.queue_timeout = queue_timeout + self._status_type: type[Status] = ModelStatus.get(self.device_info.model, S7MaxVStatus) def __del__(self) -> None: self.release() + @property + def status_type(self) -> type[Status]: + """Gets the status type for this device""" + return self._status_type + def release(self): self.sync_disconnect() [item.stop() for item in self.cache.values()] @@ -253,17 +259,14 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None: data_protocol = RoborockDataProtocol(int(data_point_number)) self._logger.debug(f"Got device update for {data_protocol.name}: {data_point}") if data_protocol in ROBOROCK_DATA_STATUS_PROTOCOL: - _cls: type[Status] = ModelStatus.get( - self.device_info.model, S7MaxVStatus - ) # Default to S7 MAXV if we don't have the data if self.cache[CacheableAttribute.status].value is None: self._logger.debug( f"Got status update({data_protocol.name}) before get_status was called." ) - self.cache[CacheableAttribute.status]._value = {} + return value = self.cache[CacheableAttribute.status].value value[data_protocol.name] = data_point - status = _cls.from_dict(value) + status = self._status_type.from_dict(value) for listener in self._listeners: listener(self.device_info.device.duid, CacheableAttribute.status, status) elif data_protocol in ROBOROCK_DATA_CONSUMABLE_PROTOCOL: @@ -272,7 +275,7 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None: f"Got consumable update({data_protocol.name})" + "before get_consumable was called." ) - self.cache[CacheableAttribute.consumable]._value = {} + return value = self.cache[CacheableAttribute.consumable].value value[data_protocol.name] = data_point consumable = Consumable.from_dict(value) @@ -406,10 +409,7 @@ async def send_command( return response async def get_status(self) -> Status | None: - _cls: type[Status] = ModelStatus.get( - self.device_info.model, S7MaxVStatus - ) # Default to S7 MAXV if we don't have the data - return _cls.from_dict(await self.cache[CacheableAttribute.status].async_value()) + return self._status_type.from_dict(await self.cache[CacheableAttribute.status].async_value()) async def get_dnd_timer(self) -> DnDTimer | None: return DnDTimer.from_dict(await self.cache[CacheableAttribute.dnd_timer].async_value()) diff --git a/roborock/cloud_api.py b/roborock/cloud_api.py index 51e3e5e..4806435 100644 --- a/roborock/cloud_api.py +++ b/roborock/cloud_api.py @@ -138,7 +138,7 @@ def sync_connect(self) -> tuple[bool, Task[tuple[Any, VacuumError | None]] | Non if self._mqtt_port is None or self._mqtt_host is None: raise RoborockException("Mqtt information was not entered. Cannot connect.") - self._logger.info("Connecting to mqtt") + self._logger.debug("Connecting to mqtt") connected_future = asyncio.ensure_future(self._async_response(CONNECT_REQUEST_ID)) super().connect(host=self._mqtt_host, port=self._mqtt_port, keepalive=KEEPALIVE) diff --git a/roborock/local_api.py b/roborock/local_api.py index 73dcd3a..fb3cef1 100644 --- a/roborock/local_api.py +++ b/roborock/local_api.py @@ -59,7 +59,7 @@ async def async_connect(self) -> None: if not self.is_connected(): self.sync_disconnect() async with async_timeout.timeout(self.queue_timeout): - self._logger.info(f"Connecting to {self.host}") + self._logger.debug(f"Connecting to {self.host}") self.transport, _ = await self.event_loop.create_connection( # type: ignore lambda: self, self.host, 58867 )