Skip to content

Commit

Permalink
fix: status reworking (#121)
Browse files Browse the repository at this point in the history

* 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
  • Loading branch information
Lash-L authored Sep 19, 2023
1 parent b9c2260 commit 8f4b7d3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions roborock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion roborock/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion roborock/local_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 8f4b7d3

Please sign in to comment.