Skip to content

Commit 4c2af3e

Browse files
committed
chore: address PR feedback
1 parent 8e681a2 commit 4c2af3e

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

roborock/devices/rpc/v1_channel.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,8 @@ async def subscribe(self, callback: Callable[[RoborockMessage], None]) -> Callab
308308
loop = asyncio.get_running_loop()
309309
self._reconnect_task = loop.create_task(self._background_reconnect())
310310

311-
# Always attempt to subscribe to MQTT to receive protocol updates (data points)
312-
# even if we have a local connection. Protocol updates only come via cloud/MQTT.
313-
# Local connection is used for RPC commands, but push notifications come via MQTT.
311+
# We maintain an active MQTT subscription even when connected locally to receive
312+
# unsolicited status updates (DPS push messages) directly from the cloud.
314313
try:
315314
self._mqtt_unsub = await self._mqtt_channel.subscribe(self._on_mqtt_message)
316315
except RoborockException as err:
@@ -342,7 +341,7 @@ def add_dps_listener(self, listener: Callable[[dict[RoborockDataProtocol, Any]],
342341
This will attach a listener to the existing subscription, invoking
343342
the listener whenever new DPS values arrive from the subscription.
344343
This will only work if a subscription has already been setup, which is
345-
handled by the device setup.
344+
handled by the device start.
346345
"""
347346
return self._dps_listeners.add_callback(listener)
348347

@@ -447,10 +446,16 @@ def _on_mqtt_message(self, message: RoborockMessage) -> None:
447446
if self._callback:
448447
self._callback(message)
449448
try:
450-
if datapoints := decode_data_protocol_message(message):
451-
self._dps_listeners(datapoints)
449+
datapoints = decode_data_protocol_message(message)
452450
except RoborockException as e:
453451
self._logger.debug("Error decoding data protocol message: %s", e)
452+
return
453+
454+
if datapoints:
455+
try:
456+
self._dps_listeners(datapoints)
457+
except Exception:
458+
self._logger.exception("Error in DPS listener callback")
454459

455460
def _on_local_message(self, message: RoborockMessage) -> None:
456461
"""Handle incoming local messages."""

tests/protocols/test_v1_protocol.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,7 @@ def test_decode_data_protocol_message() -> None:
320320
payload=payload,
321321
)
322322
result = decode_data_protocol_message(message)
323-
assert result is not None
324-
assert result[RoborockDataProtocol.STATE] == 8
325-
assert result[RoborockDataProtocol.BATTERY] == 95
323+
assert result == {RoborockDataProtocol.STATE: 8, RoborockDataProtocol.BATTERY: 95}
326324

327325

328326
def test_decode_data_protocol_message_all_status_fields() -> None:
@@ -338,13 +336,14 @@ def test_decode_data_protocol_message_all_status_fields() -> None:
338336
payload=payload,
339337
)
340338
result = decode_data_protocol_message(message)
341-
assert result is not None
342-
assert result[RoborockDataProtocol.ERROR_CODE] == 0
343-
assert result[RoborockDataProtocol.STATE] == 5
344-
assert result[RoborockDataProtocol.BATTERY] == 100
345-
assert result[RoborockDataProtocol.FAN_POWER] == 102
346-
assert result[RoborockDataProtocol.WATER_BOX_MODE] == 204
347-
assert result[RoborockDataProtocol.CHARGE_STATUS] == 1
339+
assert result == {
340+
RoborockDataProtocol.ERROR_CODE: 0,
341+
RoborockDataProtocol.STATE: 5,
342+
RoborockDataProtocol.BATTERY: 100,
343+
RoborockDataProtocol.FAN_POWER: 102,
344+
RoborockDataProtocol.WATER_BOX_MODE: 204,
345+
RoborockDataProtocol.CHARGE_STATUS: 1,
346+
}
348347

349348

350349
def test_decode_data_protocol_message_unknown_codes() -> None:
@@ -355,9 +354,7 @@ def test_decode_data_protocol_message_unknown_codes() -> None:
355354
payload=payload,
356355
)
357356
result = decode_data_protocol_message(message)
358-
assert result is not None
359-
assert len(result) == 1
360-
assert result[RoborockDataProtocol.STATE] == 8
357+
assert result == {RoborockDataProtocol.STATE: 8}
361358

362359

363360
def test_decode_data_protocol_message_empty_payload() -> None:

0 commit comments

Comments
 (0)