|
| 1 | +import dataclasses |
| 2 | +import json |
| 3 | +import typing |
| 4 | +from collections.abc import Callable |
| 5 | +from datetime import time |
| 6 | + |
| 7 | +from Crypto.Cipher import AES |
| 8 | +from Crypto.Util.Padding import unpad |
| 9 | + |
| 10 | +from roborock import DeviceData |
| 11 | +from roborock.api import RoborockClient |
| 12 | +from roborock.code_mappings import ( |
| 13 | + DyadBrushSpeed, |
| 14 | + DyadCleanMode, |
| 15 | + DyadError, |
| 16 | + DyadSelfCleanLevel, |
| 17 | + DyadSelfCleanMode, |
| 18 | + DyadSuction, |
| 19 | + DyadWarmLevel, |
| 20 | + DyadWaterLevel, |
| 21 | + RoborockDyadStateCode, |
| 22 | +) |
| 23 | +from roborock.containers import DyadProductInfo, DyadSndState |
| 24 | +from roborock.roborock_message import ( |
| 25 | + RoborockDyadDataProtocol, |
| 26 | + RoborockMessage, |
| 27 | + RoborockMessageProtocol, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +@dataclasses.dataclass |
| 32 | +class DyadProtocolCacheEntry: |
| 33 | + post_process_fn: Callable |
| 34 | + value: typing.Any | None = None |
| 35 | + |
| 36 | + |
| 37 | +# Right now this cache is not active, it was too much complexity for the initial addition of dyad. |
| 38 | +protocol_entries = { |
| 39 | + RoborockDyadDataProtocol.STATUS: DyadProtocolCacheEntry(lambda val: RoborockDyadStateCode(val).name), |
| 40 | + RoborockDyadDataProtocol.SELF_CLEAN_MODE: DyadProtocolCacheEntry(lambda val: DyadSelfCleanMode(val).name), |
| 41 | + RoborockDyadDataProtocol.SELF_CLEAN_LEVEL: DyadProtocolCacheEntry(lambda val: DyadSelfCleanLevel(val).name), |
| 42 | + RoborockDyadDataProtocol.WARM_LEVEL: DyadProtocolCacheEntry(lambda val: DyadWarmLevel(val).name), |
| 43 | + RoborockDyadDataProtocol.CLEAN_MODE: DyadProtocolCacheEntry(lambda val: DyadCleanMode(val).name), |
| 44 | + RoborockDyadDataProtocol.SUCTION: DyadProtocolCacheEntry(lambda val: DyadSuction(val).name), |
| 45 | + RoborockDyadDataProtocol.WATER_LEVEL: DyadProtocolCacheEntry(lambda val: DyadWaterLevel(val).name), |
| 46 | + RoborockDyadDataProtocol.BRUSH_SPEED: DyadProtocolCacheEntry(lambda val: DyadBrushSpeed(val).name), |
| 47 | + RoborockDyadDataProtocol.POWER: DyadProtocolCacheEntry(lambda val: int(val)), |
| 48 | + RoborockDyadDataProtocol.AUTO_DRY: DyadProtocolCacheEntry(lambda val: bool(val)), |
| 49 | + RoborockDyadDataProtocol.MESH_LEFT: DyadProtocolCacheEntry(lambda val: int(360000 - val * 60)), |
| 50 | + RoborockDyadDataProtocol.BRUSH_LEFT: DyadProtocolCacheEntry(lambda val: int(360000 - val * 60)), |
| 51 | + RoborockDyadDataProtocol.ERROR: DyadProtocolCacheEntry(lambda val: DyadError(val).name), |
| 52 | + RoborockDyadDataProtocol.VOLUME_SET: DyadProtocolCacheEntry(lambda val: int(val)), |
| 53 | + RoborockDyadDataProtocol.STAND_LOCK_AUTO_RUN: DyadProtocolCacheEntry(lambda val: bool(val)), |
| 54 | + RoborockDyadDataProtocol.AUTO_DRY_MODE: DyadProtocolCacheEntry(lambda val: bool(val)), |
| 55 | + RoborockDyadDataProtocol.SILENT_DRY_DURATION: DyadProtocolCacheEntry(lambda val: int(val)), # in minutes |
| 56 | + RoborockDyadDataProtocol.SILENT_MODE: DyadProtocolCacheEntry(lambda val: bool(val)), |
| 57 | + RoborockDyadDataProtocol.SILENT_MODE_START_TIME: DyadProtocolCacheEntry( |
| 58 | + lambda val: time(hour=int(val / 60), minute=val % 60) |
| 59 | + ), # in minutes since 00:00 |
| 60 | + RoborockDyadDataProtocol.SILENT_MODE_END_TIME: DyadProtocolCacheEntry( |
| 61 | + lambda val: time(hour=int(val / 60), minute=val % 60) |
| 62 | + ), # in minutes since 00:00 |
| 63 | + RoborockDyadDataProtocol.RECENT_RUN_TIME: DyadProtocolCacheEntry( |
| 64 | + lambda val: [int(v) for v in val.split(",")] |
| 65 | + ), # minutes of cleaning in past few days. |
| 66 | + RoborockDyadDataProtocol.TOTAL_RUN_TIME: DyadProtocolCacheEntry(lambda val: int(val)), |
| 67 | + RoborockDyadDataProtocol.SND_STATE: DyadProtocolCacheEntry(lambda val: DyadSndState.from_dict(val)), |
| 68 | + RoborockDyadDataProtocol.PRODUCT_INFO: DyadProtocolCacheEntry(lambda val: DyadProductInfo.from_dict(val)), |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +class RoborockClientA01(RoborockClient): |
| 73 | + def __init__(self, endpoint: str, device_info: DeviceData): |
| 74 | + super().__init__(endpoint, device_info) |
| 75 | + |
| 76 | + def on_message_received(self, messages: list[RoborockMessage]) -> None: |
| 77 | + for message in messages: |
| 78 | + protocol = message.protocol |
| 79 | + if message.payload and protocol in [ |
| 80 | + RoborockMessageProtocol.RPC_RESPONSE, |
| 81 | + RoborockMessageProtocol.GENERAL_REQUEST, |
| 82 | + ]: |
| 83 | + payload = message.payload |
| 84 | + try: |
| 85 | + payload = unpad(payload, AES.block_size) |
| 86 | + except Exception: |
| 87 | + continue |
| 88 | + payload_json = json.loads(payload.decode()) |
| 89 | + for data_point_number, data_point in payload_json.get("dps").items(): |
| 90 | + data_point_protocol = RoborockDyadDataProtocol(int(data_point_number)) |
| 91 | + if data_point_protocol in protocol_entries: |
| 92 | + # Auto convert into data struct we want. |
| 93 | + converted_response = protocol_entries[data_point_protocol].post_process_fn(data_point) |
| 94 | + queue = self._waiting_queue.get(int(data_point_number)) |
| 95 | + if queue and queue.protocol == protocol: |
| 96 | + queue.resolve((converted_response, None)) |
| 97 | + |
| 98 | + async def update_values(self, dyad_data_protocols: list[RoborockDyadDataProtocol]): |
| 99 | + """This should handle updating for each given protocol.""" |
| 100 | + raise NotImplementedError |
0 commit comments