|
| 1 | +"""Customized room-cleaning controls for Q10 devices.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from collections.abc import Sequence |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP, YXCleanType, YXDeviceCleanTask |
| 9 | +from roborock.data.b01_q10.b01_q10_containers import Q10RoomCleanSettings |
| 10 | +from roborock.data.containers import RoborockBase |
| 11 | +from roborock.exceptions import RoborockException |
| 12 | +from roborock.protocols.b01_q10_protocol import decode_room_clean_settings, encode_room_clean_settings |
| 13 | + |
| 14 | +from .command import CommandTrait |
| 15 | +from .common import UpdatableTrait |
| 16 | + |
| 17 | +_LOGGER = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class RoomCleaning(RoborockBase): |
| 22 | + """Current customized room-cleaning state.""" |
| 23 | + |
| 24 | + settings: tuple[Q10RoomCleanSettings, ...] = () |
| 25 | + settings_available: bool = False |
| 26 | + |
| 27 | + |
| 28 | +class RoomCleaningTrait(RoomCleaning, UpdatableTrait): |
| 29 | + """Read and control Q10 per-room cleaning settings.""" |
| 30 | + |
| 31 | + def __init__(self, command: CommandTrait) -> None: |
| 32 | + RoomCleaning.__init__(self) |
| 33 | + UpdatableTrait.__init__(self, command, _LOGGER) |
| 34 | + self._command: CommandTrait = command |
| 35 | + |
| 36 | + def settings_for_room(self, room_id: int) -> Q10RoomCleanSettings | None: |
| 37 | + """Return the latest reported settings for one room.""" |
| 38 | + return next((room for room in self.settings if room.room_id == room_id), None) |
| 39 | + |
| 40 | + async def refresh(self) -> None: |
| 41 | + """Request the complete customized-room settings.""" |
| 42 | + await self._command.send(B01_Q10_DP.COMMON, {str(B01_Q10_DP.CUSTOMER_CLEAN_REQUEST.code): 0}) |
| 43 | + |
| 44 | + async def set_settings(self, settings: Sequence[Q10RoomCleanSettings]) -> None: |
| 45 | + """Publish customized settings, then request a complete settings push.""" |
| 46 | + payload = encode_room_clean_settings(settings) |
| 47 | + await self._command.send(B01_Q10_DP.COMMON, {str(B01_Q10_DP.CUSTOMER_CLEAN.code): payload}) |
| 48 | + await self.refresh() |
| 49 | + |
| 50 | + async def clean(self, settings: Sequence[Q10RoomCleanSettings]) -> None: |
| 51 | + """Publish settings, select customized mode, and start the selected rooms.""" |
| 52 | + selected = tuple(settings) |
| 53 | + payload = encode_room_clean_settings(selected) |
| 54 | + await self._command.send(B01_Q10_DP.COMMON, {str(B01_Q10_DP.CUSTOMER_CLEAN.code): payload}) |
| 55 | + await self._command.send(B01_Q10_DP.CLEAN_MODE, YXCleanType.CUSTOMIZED.code) |
| 56 | + await self._command.send( |
| 57 | + B01_Q10_DP.START_CLEAN, |
| 58 | + {"cmd": YXDeviceCleanTask.ELECTORAL.code, "clean_paramters": [room.room_id for room in selected]}, |
| 59 | + ) |
| 60 | + |
| 61 | + def update_from_dps(self, decoded_dps: dict[B01_Q10_DP, Any]) -> None: |
| 62 | + """Replace full settings or upsert the rooms in a compact push.""" |
| 63 | + payload = decoded_dps.get(B01_Q10_DP.CUSTOMER_CLEAN) |
| 64 | + if not isinstance(payload, str): |
| 65 | + return |
| 66 | + try: |
| 67 | + update = decode_room_clean_settings(payload) |
| 68 | + except RoborockException: |
| 69 | + _LOGGER.debug("Ignoring malformed Q10 customized-room settings", exc_info=True) |
| 70 | + return |
| 71 | + if update.complete: |
| 72 | + settings = update.settings |
| 73 | + else: |
| 74 | + by_room = {room.room_id: room for room in self.settings} |
| 75 | + by_room.update((room.room_id, room) for room in update.settings) |
| 76 | + settings = tuple(by_room.values()) |
| 77 | + changed = settings != self.settings or not self.settings_available |
| 78 | + self.settings = settings |
| 79 | + self.settings_available = True |
| 80 | + if changed: |
| 81 | + self._notify_update() |
0 commit comments