|
6 | 6 |
|
7 | 7 | import asyncio |
8 | 8 | import datetime |
| 9 | +import json |
9 | 10 | import logging |
10 | 11 | from abc import ABC |
11 | 12 | from collections.abc import Callable |
12 | 13 | from typing import Any |
13 | 14 |
|
14 | 15 | from roborock.callbacks import CallbackList |
15 | | -from roborock.data import HomeDataDevice, HomeDataProduct |
| 16 | +from roborock.data import HomeDataDevice, HomeDataProduct, RoborockErrorCode, RoborockStateCode |
16 | 17 | from roborock.diagnostics import redact_device_data |
17 | 18 | from roborock.exceptions import RoborockException |
18 | | -from roborock.roborock_message import RoborockMessage |
| 19 | +from roborock.roborock_message import ( |
| 20 | + ROBOROCK_DATA_STATUS_PROTOCOL, |
| 21 | + RoborockDataProtocol, |
| 22 | + RoborockMessage, |
| 23 | + RoborockMessageProtocol, |
| 24 | +) |
19 | 25 | from roborock.util import RoborockLoggerAdapter |
20 | 26 |
|
21 | 27 | from .traits import Trait |
@@ -219,8 +225,77 @@ async def close(self) -> None: |
219 | 225 | self._unsub = None |
220 | 226 |
|
221 | 227 | def _on_message(self, message: RoborockMessage) -> None: |
222 | | - """Handle incoming messages from the device.""" |
| 228 | + """Handle incoming messages from the device. |
| 229 | +
|
| 230 | + Note: Protocol updates (data points) are only sent via cloud/MQTT, not local connection. |
| 231 | + """ |
223 | 232 | self._logger.debug("Received message from device: %s", message) |
| 233 | + if self.v1_properties is None: |
| 234 | + # Ensure we are only doing below logic for set-up V1 devices. |
| 235 | + return |
| 236 | + |
| 237 | + # Only process messages that can contain protocol updates |
| 238 | + # RPC_RESPONSE (102), GENERAL_REQUEST (4), and GENERAL_RESPONSE (5) |
| 239 | + if message.protocol not in { |
| 240 | + RoborockMessageProtocol.RPC_RESPONSE, |
| 241 | + RoborockMessageProtocol.GENERAL_RESPONSE, |
| 242 | + }: |
| 243 | + return |
| 244 | + |
| 245 | + if not message.payload: |
| 246 | + return |
| 247 | + |
| 248 | + try: |
| 249 | + payload = json.loads(message.payload.decode()) |
| 250 | + dps = payload.get("dps", {}) |
| 251 | + |
| 252 | + if not dps: |
| 253 | + return |
| 254 | + |
| 255 | + # Process each data point in the message |
| 256 | + for data_point_number, data_point in dps.items(): |
| 257 | + # Skip RPC responses (102) as they're handled by the RPC channel |
| 258 | + if data_point_number == "102": |
| 259 | + continue |
| 260 | + |
| 261 | + try: |
| 262 | + data_protocol = RoborockDataProtocol(int(data_point_number)) |
| 263 | + self._logger.debug(f"Got device update for {data_protocol.name}: {data_point}") |
| 264 | + self._handle_protocol_update(data_protocol, data_point) |
| 265 | + except ValueError: |
| 266 | + # Unknown protocol number |
| 267 | + self._logger.debug( |
| 268 | + f"Got unknown data protocol {data_point_number}, data: {data_point}. " |
| 269 | + f"This may allow for faster updates in the future." |
| 270 | + ) |
| 271 | + except (json.JSONDecodeError, UnicodeDecodeError, KeyError) as ex: |
| 272 | + self._logger.debug(f"Failed to parse protocol message: {ex}") |
| 273 | + |
| 274 | + def _handle_protocol_update(self, protocol: RoborockDataProtocol, data_point: Any) -> None: |
| 275 | + """Handle a protocol update for a specific data protocol. |
| 276 | +
|
| 277 | + Args: |
| 278 | + protocol: The data protocol number. |
| 279 | + data_point: The data value for this protocol. |
| 280 | + """ |
| 281 | + # Handle status protocol updates |
| 282 | + 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() |
224 | 299 |
|
225 | 300 | def diagnostic_data(self) -> dict[str, Any]: |
226 | 301 | """Return diagnostics information about the device.""" |
|
0 commit comments