|
| 1 | +"""Clean history trait for Q10 B01 devices. |
| 2 | +
|
| 3 | +Unlike the Q7 (which exposes a synchronous ``service.get_record_list`` RPC), the |
| 4 | +Q10 is push-driven: :meth:`CleanHistoryTrait.refresh` sends an ``{"op": "list"}`` |
| 5 | +query for ``dpCleanRecord`` (DP 52) over the ``dpCommon`` channel, and the device |
| 6 | +publishes its clean-record list back on the subscribe stream, which |
| 7 | +:meth:`CleanHistoryTrait.update_from_dps` then decodes. |
| 8 | +
|
| 9 | +Wire parsing is separated from state management: :class:`CleanRecordConverter` turns |
| 10 | +a ``dpCleanRecord`` envelope into a :class:`CleanRecordPush`, and the trait applies it. |
| 11 | +""" |
| 12 | + |
| 13 | +import logging |
| 14 | +from dataclasses import dataclass, field |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +from roborock.data.b01_q10.b01_q10_code_mappings import ( |
| 18 | + B01_Q10_DP, |
| 19 | + YXCleaningResult, |
| 20 | + YXCleanScope, |
| 21 | + YXCleanType, |
| 22 | + YXStartMethod, |
| 23 | +) |
| 24 | +from roborock.data.b01_q10.b01_q10_containers import Q10CleanRecord |
| 25 | + |
| 26 | +from .command import CommandTrait |
| 27 | +from .common import UpdatableTrait |
| 28 | + |
| 29 | +__all__ = [ |
| 30 | + "CleanHistoryTrait", |
| 31 | + "CleanRecordConverter", |
| 32 | + "CleanRecordPush", |
| 33 | +] |
| 34 | + |
| 35 | +_LOGGER = logging.getLogger(__name__) |
| 36 | + |
| 37 | +_RECORD_FIELD_COUNT = 12 |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class CleanRecordPush: |
| 42 | + """A parsed ``dpCleanRecord`` push: the records it carries and how to apply them. |
| 43 | +
|
| 44 | + ``replace`` is ``True`` for an ``{"op": "list"}`` reply (the full history, which |
| 45 | + replaces the current state) and ``False`` for an ``{"op": "notify"}`` push (a |
| 46 | + single just-finished record, which is upserted). |
| 47 | + """ |
| 48 | + |
| 49 | + records: list[Q10CleanRecord] = field(default_factory=list) |
| 50 | + replace: bool = False |
| 51 | + |
| 52 | + |
| 53 | +class CleanRecordConverter: |
| 54 | + """Converts a raw ``dpCleanRecord`` (DP 52) envelope into a :class:`CleanRecordPush`. |
| 55 | +
|
| 56 | + Mirrors the converter-per-object pattern used by the other Q10 traits: parsing the |
| 57 | + wire payload lives here, separate from the trait that manages the record list. |
| 58 | + """ |
| 59 | + |
| 60 | + def parse(self, envelope: dict[str, Any]) -> CleanRecordPush | None: |
| 61 | + """Parse a decoded ``dpCleanRecord`` envelope into a :class:`CleanRecordPush`. |
| 62 | +
|
| 63 | + Returns ``None`` for an envelope that carries nothing usable (so the trait can |
| 64 | + ignore it without changing state). Malformed individual records are skipped. |
| 65 | + """ |
| 66 | + if envelope.get("op") == "notify": |
| 67 | + record = CleanRecordConverter.parse_record(envelope.get("id")) |
| 68 | + return CleanRecordPush([record], replace=False) if record is not None else None |
| 69 | + data = envelope.get("data") |
| 70 | + if not isinstance(data, list): |
| 71 | + return None |
| 72 | + records = [record for item in data if (record := CleanRecordConverter.parse_record(item)) is not None] |
| 73 | + return CleanRecordPush(records, replace=True) |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def parse_record(raw: Any | None) -> Q10CleanRecord | None: |
| 77 | + """Decode one underscore-delimited clean-record string into a :class:`Q10CleanRecord`. |
| 78 | +
|
| 79 | + The device joins 12 values with ``_``: recordId, startTime (Unix s), cleanTime |
| 80 | + (min), cleanArea (m2), mapLen, pathLen, virtualLen, cleanMode, workMode, |
| 81 | + cleaningResult, startMethod, collectDustCount. Returns ``None`` for anything but |
| 82 | + a well-formed 12-field string; an unmapped enum code resolves to ``None`` on its |
| 83 | + field (``raw`` keeps the original). |
| 84 | + """ |
| 85 | + if not isinstance(raw, str): |
| 86 | + return None |
| 87 | + parts = raw.split("_") |
| 88 | + if len(parts) != _RECORD_FIELD_COUNT: |
| 89 | + return None |
| 90 | + try: |
| 91 | + return Q10CleanRecord( |
| 92 | + raw=raw, |
| 93 | + record_id=parts[0], |
| 94 | + start_time=int(parts[1]), |
| 95 | + clean_time=int(parts[2]), |
| 96 | + clean_area=int(parts[3]), |
| 97 | + map_len=int(parts[4]), |
| 98 | + path_len=int(parts[5]), |
| 99 | + virtual_len=int(parts[6]), |
| 100 | + clean_mode=YXCleanScope.from_code_optional(int(parts[7])), |
| 101 | + work_mode=YXCleanType.from_code_optional(int(parts[8])), |
| 102 | + cleaning_result=YXCleaningResult.from_code_optional(int(parts[9])), |
| 103 | + start_method=YXStartMethod.from_code_optional(int(parts[10])), |
| 104 | + collect_dust_count=int(parts[11]), |
| 105 | + ) |
| 106 | + except ValueError: |
| 107 | + return None |
| 108 | + |
| 109 | + |
| 110 | +class CleanHistoryTrait(UpdatableTrait): |
| 111 | + """Access to the Q10 clean-record history (``dpCleanRecord``, DP 52). |
| 112 | +
|
| 113 | + A read-model trait updated from the DPS stream like the others, but it overrides |
| 114 | + :meth:`update_from_dps` because the payload is a structured push (a record list, |
| 115 | + or a single ``op:"notify"`` record) rather than a flat data-point-to-field map. |
| 116 | + """ |
| 117 | + |
| 118 | + def __init__(self, command: CommandTrait) -> None: |
| 119 | + """Initialize the clean history trait.""" |
| 120 | + UpdatableTrait.__init__(self, command, _LOGGER) |
| 121 | + self._converter = CleanRecordConverter() |
| 122 | + self.records: list[Q10CleanRecord] = [] |
| 123 | + """Decoded clean records, most recent first.""" |
| 124 | + |
| 125 | + @property |
| 126 | + def last_record(self) -> Q10CleanRecord | None: |
| 127 | + """The most recent clean record, or ``None`` if there are none.""" |
| 128 | + return self.records[0] if self.records else None |
| 129 | + |
| 130 | + async def refresh(self) -> None: |
| 131 | + """Request the clean-record list from the device. |
| 132 | +
|
| 133 | + This sends the query and returns immediately; the records arrive |
| 134 | + asynchronously on the device stream and populate :attr:`records` once |
| 135 | + :meth:`update_from_dps` processes the ``dpCleanRecord`` push. |
| 136 | + """ |
| 137 | + if self._command is None: |
| 138 | + raise ValueError("Trait is read-only; no command channel was provided") |
| 139 | + await self._command.send( |
| 140 | + B01_Q10_DP.COMMON, |
| 141 | + params={str(B01_Q10_DP.CLEAN_RECORD.code): {"op": "list"}}, |
| 142 | + ) |
| 143 | + |
| 144 | + def update_from_dps(self, decoded_dps: dict[B01_Q10_DP, Any]) -> None: |
| 145 | + """Apply a ``dpCleanRecord`` push (a full list reply or a single notify).""" |
| 146 | + envelope = decoded_dps.get(B01_Q10_DP.CLEAN_RECORD) |
| 147 | + if not isinstance(envelope, dict): |
| 148 | + return |
| 149 | + push = self._converter.parse(envelope) |
| 150 | + if push is None: |
| 151 | + return |
| 152 | + self._apply(push) |
| 153 | + |
| 154 | + def _apply(self, push: CleanRecordPush) -> None: |
| 155 | + """Merge or replace the records from ``push``, then sort newest-first and notify.""" |
| 156 | + if push.replace: |
| 157 | + records = list(push.records) |
| 158 | + else: |
| 159 | + updated_ids = {record.record_id for record in push.records} |
| 160 | + records = [record for record in self.records if record.record_id not in updated_ids] |
| 161 | + records.extend(push.records) |
| 162 | + records.sort(key=lambda record: record.start_time or 0, reverse=True) |
| 163 | + self.records = records |
| 164 | + self._notify_update() |
0 commit comments