Skip to content

Commit 3d68ea4

Browse files
authored
fix: listeners getting protocol data before it exists. (#87)
* fix: listeners getting protocol data before it exists * fix: optimize code
1 parent ef88edd commit 3d68ea4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

roborock/api.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def __init__(self, endpoint: str, device_info: DeviceData) -> None:
173173
for cacheable_attribute, attr in create_cache_map().items()
174174
}
175175
device_cache[device_info.device.duid] = cache
176-
self.cache = cache
176+
self.cache: dict[CacheableAttribute, AttributeCache] = cache
177177
self._listeners: list[Callable[[CacheableAttribute, RoborockBase], None]] = []
178178

179179
def __del__(self) -> None:
@@ -245,12 +245,22 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
245245
_cls: Type[Status] = ModelStatus.get(
246246
self.device_info.model, S7MaxVStatus
247247
) # Default to S7 MAXV if we don't have the data
248+
if self.cache[CacheableAttribute.status].value is None:
249+
_LOGGER.debug(
250+
f"Got status update({data_protocol.name}) before get_status was called."
251+
)
252+
self.cache[CacheableAttribute.status]._value = {}
248253
value = self.cache[CacheableAttribute.status].value
249254
value[data_protocol.name] = data_point
250255
status = _cls.from_dict(value)
251256
for listener in self._listeners:
252257
listener(CacheableAttribute.status, status)
253258
elif data_protocol in ROBOROCK_DATA_CONSUMABLE_PROTOCOL:
259+
if self.cache[CacheableAttribute.consumable].value is None:
260+
_LOGGER.debug(
261+
f"Got consumable update({data_protocol.name}) before get_status was called."
262+
)
263+
self.cache[CacheableAttribute.consumable]._value = {}
254264
value = self.cache[CacheableAttribute.consumable].value
255265
value[data_protocol.name] = data_point
256266
consumable = Consumable.from_dict(value)

0 commit comments

Comments
 (0)