Skip to content

Commit 4a23a4b

Browse files
committed
chore: minor refactoring
1 parent 74a9ee4 commit 4a23a4b

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

roborock/devices/device.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from typing import Any
1414

1515
from roborock.callbacks import CallbackList
16-
from roborock.data import HomeDataDevice, HomeDataProduct, RoborockErrorCode, RoborockStateCode
16+
from roborock.data import HomeDataDevice, HomeDataProduct
1717
from roborock.diagnostics import redact_device_data
1818
from roborock.exceptions import RoborockException
1919
from roborock.roborock_message import (
@@ -235,7 +235,7 @@ def _on_message(self, message: RoborockMessage) -> None:
235235
return
236236

237237
# Only process messages that can contain protocol updates
238-
# RPC_RESPONSE (102), GENERAL_REQUEST (4), and GENERAL_RESPONSE (5)
238+
# RPC_RESPONSE (102), and GENERAL_RESPONSE (5)
239239
if message.protocol not in {
240240
RoborockMessageProtocol.RPC_RESPONSE,
241241
RoborockMessageProtocol.GENERAL_RESPONSE,
@@ -246,7 +246,7 @@ def _on_message(self, message: RoborockMessage) -> None:
246246
return
247247

248248
try:
249-
payload = json.loads(message.payload.decode())
249+
payload = json.loads(message.payload.decode("utf-8"))
250250
dps = payload.get("dps", {})
251251

252252
if not dps:
@@ -260,7 +260,7 @@ def _on_message(self, message: RoborockMessage) -> None:
260260

261261
try:
262262
data_protocol = RoborockDataProtocol(int(data_point_number))
263-
self._logger.debug(f"Got device update for {data_protocol.name}: {data_point}")
263+
self._logger.debug("Got device update for %s: %s", data_protocol.name, data_point)
264264
self._handle_protocol_update(data_protocol, data_point)
265265
except ValueError:
266266
# Unknown protocol number
@@ -269,7 +269,7 @@ def _on_message(self, message: RoborockMessage) -> None:
269269
f"This may allow for faster updates in the future."
270270
)
271271
except (json.JSONDecodeError, UnicodeDecodeError, KeyError) as ex:
272-
self._logger.debug(f"Failed to parse protocol message: {ex}")
272+
self._logger.debug("Failed to parse protocol message: %s", ex)
273273

274274
def _handle_protocol_update(self, protocol: RoborockDataProtocol, data_point: Any) -> None:
275275
"""Handle a protocol update for a specific data protocol.
@@ -280,22 +280,9 @@ def _handle_protocol_update(self, protocol: RoborockDataProtocol, data_point: An
280280
"""
281281
# Handle status protocol updates
282282
if protocol in ROBOROCK_DATA_STATUS_PROTOCOL and self.v1_properties and self.v1_properties.status:
283-
# Update the specific field in the status trait
284-
match protocol:
285-
case RoborockDataProtocol.ERROR_CODE:
286-
self.v1_properties.status.error_code = RoborockErrorCode(data_point)
287-
case RoborockDataProtocol.STATE:
288-
self.v1_properties.status.state = RoborockStateCode(data_point)
289-
case RoborockDataProtocol.BATTERY:
290-
self.v1_properties.status.battery = data_point
291-
case RoborockDataProtocol.CHARGE_STATUS:
292-
self.v1_properties.status.charge_status = data_point
293-
case _:
294-
# There is also fan power and water box mode, but for now those are skipped
295-
return
296-
297-
self._logger.debug("Updated status.%s to %s", protocol.name.lower(), data_point)
298-
self.v1_properties.status.notify_update()
283+
if self.v1_properties.status.handle_protocol_update(protocol, data_point):
284+
self._logger.debug("Updated status.%s to %s", protocol.name.lower(), data_point)
285+
self.v1_properties.status.notify_update()
299286

300287
def diagnostic_data(self) -> dict[str, Any]:
301288
"""Return diagnostics information about the device."""

roborock/devices/traits/v1/status.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Self
1+
from typing import Any, Self
22

3-
from roborock.data import HomeDataProduct, ModelStatus, S7MaxVStatus, Status
3+
from roborock.data import HomeDataProduct, ModelStatus, RoborockErrorCode, RoborockStateCode, S7MaxVStatus, Status
44
from roborock.devices.traits.v1 import common
5+
from roborock.roborock_message import RoborockDataProtocol
56
from roborock.roborock_typing import RoborockCommand
67

78

@@ -22,3 +23,19 @@ def _parse_response(self, response: common.V1ResponseData) -> Self:
2223
if isinstance(response, dict):
2324
return status_type.from_dict(response)
2425
raise ValueError(f"Unexpected status format: {response!r}")
26+
27+
def handle_protocol_update(self, protocol: RoborockDataProtocol, data_point: Any) -> bool:
28+
"""Handle a protocol update for a specific data protocol."""
29+
match protocol:
30+
case RoborockDataProtocol.ERROR_CODE:
31+
self.error_code = RoborockErrorCode(data_point)
32+
case RoborockDataProtocol.STATE:
33+
self.state = RoborockStateCode(data_point)
34+
case RoborockDataProtocol.BATTERY:
35+
self.battery = data_point
36+
case RoborockDataProtocol.CHARGE_STATUS:
37+
self.charge_status = data_point
38+
case _:
39+
# There is also fan power and water box mode, but for now those are skipped
40+
return False
41+
return True

0 commit comments

Comments
 (0)