Skip to content

Commit 2bee8a1

Browse files
feat: adding device_id to listeners and fixing race condition on connection, disconnection and messages
1 parent 260dfdf commit 2bee8a1

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

roborock/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def __init__(self, endpoint: str, device_info: DeviceData) -> None:
176176
}
177177
device_cache[device_info.device.duid] = cache
178178
self.cache: dict[CacheableAttribute, AttributeCache] = cache
179-
self._listeners: list[Callable[[CacheableAttribute, RoborockBase], None]] = []
179+
self._listeners: list[Callable[[str, CacheableAttribute, RoborockBase], None]] = []
180180

181181
def __del__(self) -> None:
182182
self.release()
@@ -256,7 +256,7 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
256256
value[data_protocol.name] = data_point
257257
status = _cls.from_dict(value)
258258
for listener in self._listeners:
259-
listener(CacheableAttribute.status, status)
259+
listener(self.device_info.device.duid, CacheableAttribute.status, status)
260260
elif data_protocol in ROBOROCK_DATA_CONSUMABLE_PROTOCOL:
261261
if self.cache[CacheableAttribute.consumable].value is None:
262262
self._logger.debug(
@@ -267,7 +267,9 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
267267
value[data_protocol.name] = data_point
268268
consumable = Consumable.from_dict(value)
269269
for listener in self._listeners:
270-
listener(CacheableAttribute.consumable, consumable)
270+
listener(
271+
self.device_info.device.duid, CacheableAttribute.consumable, consumable
272+
)
271273
return
272274
except ValueError:
273275
pass

roborock/cloud_api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,19 @@ def sync_connect(self) -> bool:
132132

133133
async def async_disconnect(self) -> None:
134134
async with self._mutex:
135+
async_response = self._async_response(DISCONNECT_REQUEST_ID)
135136
disconnecting = self.sync_disconnect()
136137
if disconnecting:
137-
(_, err) = await self._async_response(DISCONNECT_REQUEST_ID)
138+
(_, err) = await async_response
138139
if err:
139140
raise RoborockException(err) from err
140141

141142
async def async_connect(self) -> None:
142143
async with self._mutex:
144+
async_response = self._async_response(CONNECT_REQUEST_ID)
143145
connecting = self.sync_connect()
144146
if connecting:
145-
(_, err) = await self._async_response(CONNECT_REQUEST_ID)
147+
(_, err) = await async_response
146148
if err:
147149
raise RoborockException(err) from err
148150

@@ -165,8 +167,9 @@ async def send_message(self, roborock_message: RoborockMessage):
165167
local_key = self.device_info.device.local_key
166168
msg = MessageParser.build(roborock_message, local_key, False)
167169
self._logger.debug(f"id={request_id} Requesting method {method} with {params}")
170+
async_response = self._async_response(request_id, response_protocol)
168171
self._send_msg_raw(msg)
169-
(response, err) = await self._async_response(request_id, response_protocol)
172+
(response, err) = await async_response
170173
self._diagnostic_data[method if method is not None else "unknown"] = {
171174
"params": roborock_message.get_params(),
172175
"response": response,

roborock/local_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ async def send_message(self, roborock_message: RoborockMessage):
153153
if method:
154154
self._logger.debug(f"id={request_id} Requesting method {method} with {params}")
155155
# Send the command to the Roborock device
156+
async_response = self._async_response(request_id, response_protocol)
156157
self._send_msg_raw(msg)
157-
(response, err) = await self._async_response(request_id, response_protocol)
158+
(response, err) = await async_response
158159
self._diagnostic_data[method if method is not None else "unknown"] = {
159160
"params": roborock_message.get_params(),
160161
"response": response,

0 commit comments

Comments
 (0)