|
31 | 31 | from datetime import UTC, datetime, time |
32 | 32 | from typing import Any, Generic, TypeVar |
33 | 33 |
|
34 | | -from roborock.data import DyadProductInfo, DyadScheduleTask, DyadSndState, HomeDataProduct, RoborockCategory |
| 34 | +from roborock.data import ( |
| 35 | + DyadProductInfo, |
| 36 | + DyadScheduleTask, |
| 37 | + DyadSndState, |
| 38 | + HomeDataProduct, |
| 39 | + RoborockBase, |
| 40 | + RoborockCategory, |
| 41 | +) |
35 | 42 | from roborock.data.dyad.dyad_code_mappings import ( |
36 | 43 | DyadBrushSpeed, |
37 | 44 | DyadCleanAssistPower, |
|
72 | 79 | from roborock.devices.traits.common import TraitUpdateListener |
73 | 80 | from roborock.devices.transport.mqtt_channel import MqttChannel |
74 | 81 | from roborock.exceptions import RoborockException |
75 | | -from roborock.protocols.a01_protocol import decode_rpc_response |
| 82 | +from roborock.protocols.a01_protocol import decode_datapoint_value, decode_rpc_response |
76 | 83 | from roborock.roborock_message import ( |
77 | 84 | RoborockDyadDataProtocol, |
78 | 85 | RoborockMessage, |
|
89 | 96 | ] |
90 | 97 |
|
91 | 98 |
|
92 | | -def _decode_dyad_schedule(value: str) -> list[DyadScheduleTask]: |
93 | | - """Decode the schedule data point, which is a JSON list or an empty string.""" |
| 99 | +def _decode_dyad_schedule(value: Any) -> list[DyadScheduleTask]: |
| 100 | + """Decode the schedule data point, a list of tasks or an empty string when there are none.""" |
94 | 101 | if not value: |
95 | 102 | return [] |
96 | | - return [DyadScheduleTask.from_dict(task) for task in json.loads(value)] |
| 103 | + if not isinstance(value, list): |
| 104 | + raise ValueError(f"Unexpected schedule value: {value!r}") |
| 105 | + return [DyadScheduleTask.from_dict(task) for task in value] |
97 | 106 |
|
98 | 107 |
|
99 | 108 | DYAD_PROTOCOL_ENTRIES: dict[RoborockDyadDataProtocol, Callable] = { |
@@ -191,6 +200,17 @@ def convert_zeo_value(protocol_value: RoborockZeoProtocol, value: Any) -> Any: |
191 | 200 | return None |
192 | 201 |
|
193 | 202 |
|
| 203 | +def _diagnostic_value(value: Any) -> Any: |
| 204 | + """Convert a decoded value to plain data for diagnostics.""" |
| 205 | + if isinstance(value, RoborockBase): |
| 206 | + return value.as_dict() |
| 207 | + if isinstance(value, list): |
| 208 | + return [_diagnostic_value(item) for item in value] |
| 209 | + if isinstance(value, time): |
| 210 | + return value.isoformat() |
| 211 | + return value |
| 212 | + |
| 213 | + |
194 | 214 | _DYAD_PROTOCOL_VALUES = frozenset(protocol.value for protocol in RoborockDyadDataProtocol) |
195 | 215 | _ZEO_PROTOCOL_VALUES = frozenset(protocol.value for protocol in RoborockZeoProtocol) |
196 | 216 |
|
@@ -225,6 +245,10 @@ def values(self) -> dict[_P, Any]: |
225 | 245 | """ |
226 | 246 | return dict(self._values) |
227 | 247 |
|
| 248 | + def as_dict(self) -> dict[str, Any]: |
| 249 | + """Return the latest known state as plain data for diagnostics.""" |
| 250 | + return {protocol.name: _diagnostic_value(value) for protocol, value in self._values.items()} |
| 251 | + |
228 | 252 | @property |
229 | 253 | def last_message_time(self) -> datetime | None: |
230 | 254 | """Time the last message was received from the device. |
@@ -459,11 +483,11 @@ async def set_value(self, protocol: RoborockZeoProtocol, value: Any) -> dict[Rob |
459 | 483 |
|
460 | 484 |
|
461 | 485 | def _parse_device_status(device_status: dict | None) -> dict[int, Any] | None: |
462 | | - """Normalize the cloud home data status snapshot to integer datapoint codes.""" |
| 486 | + """Normalize the cloud home data status snapshot to integer datapoint codes and decoded values.""" |
463 | 487 | if not device_status: |
464 | 488 | return None |
465 | 489 | try: |
466 | | - return {int(code): value for code, value in device_status.items()} |
| 490 | + return {int(code): decode_datapoint_value(value) for code, value in device_status.items()} |
467 | 491 | except (TypeError, ValueError): |
468 | 492 | _LOGGER.debug("Ignoring malformed device status snapshot: %s", device_status) |
469 | 493 | return None |
|
0 commit comments