From ca23e47aa66a8468a554c08ce26701fb99bd7b16 Mon Sep 17 00:00:00 2001 From: Harry Coureau Date: Sun, 15 Nov 2026 01:37:43 +0000 Subject: [PATCH] fix(v1): aggregate multi-phase cleaning records Combining a multi-phase clean record assigned to the read-only begin_datetime property. The error was swallowed, so callers got the last phase's duration, area and counters and its start type. Build the record from the last phase with the first phase's begin and start type, and sum duration, area and counters across phases; fields no phase reports stay unset. Replies mixing record formats now raise a parsing error. --- roborock/devices/traits/v1/clean_summary.py | 38 ++++++++-------- tests/devices/traits/v1/test_clean_summary.py | 43 ++++++++++++++++++- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/roborock/devices/traits/v1/clean_summary.py b/roborock/devices/traits/v1/clean_summary.py index 80d0d2a2..ea3d7ecd 100644 --- a/roborock/devices/traits/v1/clean_summary.py +++ b/roborock/devices/traits/v1/clean_summary.py @@ -1,4 +1,5 @@ import logging +from dataclasses import replace from roborock.data import CleanRecord, CleanSummaryWithDetail, RoborockBase from roborock.devices.traits.v1 import common @@ -9,6 +10,12 @@ _LOGGER = logging.getLogger(__name__) +def _sum_reported(values: list[int | None]) -> int | None: + """Sum the values reported by each phase, or None if no phase reported one.""" + reported = [value for value in values if value is not None] + return sum(reported) if reported else None + + class CleanSummaryConverter(common.V1TraitDataConverter): """Converter for CleanSummaryWithDetail objects.""" @@ -41,26 +48,19 @@ def convert(self, response: common.V1ResponseData) -> CleanRecord: if isinstance(response, list): if not response: raise ValueError("Empty clean record response") - if isinstance(response[-1], dict): + if any(isinstance(rec, dict) for rec in response): + if not all(isinstance(rec, dict) for rec in response): + raise ValueError("Mixed clean record formats") records = [CleanRecord.from_dict(rec) for rec in response] - final_record = records[-1] - try: - # This code is semi-presumptuous - so it is put in a try finally to be safe. - final_record.begin = records[0].begin - final_record.begin_datetime = records[0].begin_datetime - final_record.start_type = records[0].start_type - for rec in records[0:-1]: - final_record.duration = (final_record.duration or 0) + (rec.duration or 0) - final_record.area = (final_record.area or 0) + (rec.area or 0) - final_record.avoid_count = (final_record.avoid_count or 0) + (rec.avoid_count or 0) - final_record.wash_count = (final_record.wash_count or 0) + (rec.wash_count or 0) - final_record.square_meter_area = (final_record.square_meter_area or 0) + ( - rec.square_meter_area or 0 - ) - return final_record - except Exception: # noqa: BLE001 - # Return final record when an exception occurred - return final_record + return replace( + records[-1], + begin=records[0].begin, + start_type=records[0].start_type, + duration=_sum_reported([rec.duration for rec in records]), + area=_sum_reported([rec.area for rec in records]), + avoid_count=_sum_reported([rec.avoid_count for rec in records]), + wash_count=_sum_reported([rec.wash_count for rec in records]), + ) # There are still a few unknown variables in this. begin, end, duration, area = unpack_list(response, 4) return CleanRecord(begin=begin, end=end, duration=duration, area=area) diff --git a/tests/devices/traits/v1/test_clean_summary.py b/tests/devices/traits/v1/test_clean_summary.py index 35d9b71a..301293a8 100644 --- a/tests/devices/traits/v1/test_clean_summary.py +++ b/tests/devices/traits/v1/test_clean_summary.py @@ -4,7 +4,7 @@ import pytest -from roborock.data import CleanSummary +from roborock.data import CleanRecord, CleanSummary, RoborockStartType from roborock.devices.device import RoborockDevice from roborock.devices.traits.v1.clean_summary import CleanSummaryTrait from roborock.exceptions import RoborockException, RoborockParsingException @@ -245,6 +245,47 @@ async def test_get_clean_record_structured_responses( assert record.end == response[-1]["end"] +async def test_get_clean_record_multi_phase_totals( + clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock +) -> None: + """Sum phase totals, keep the first phase's start and the last phase's outcome.""" + mock_rpc_channel.send_command.return_value = [ + {"begin": 1700000000, "end": 1700000100, "duration": 100, "area": 1000000, "start_type": 2, "complete": 0}, + {"begin": 1700000200, "end": 1700000300, "duration": 100, "area": 1000000, "avoid_count": 3, "complete": 0}, + {"begin": 1700000400, "end": 1700000500, "duration": 50, "area": 500000, "start_type": 3, "complete": 1}, + ] + + record = await clean_summary_trait.get_clean_record(1700000000) + + assert record == CleanRecord( + begin=1700000000, + end=1700000500, + duration=250, + area=2500000, + complete=1, + start_type=RoborockStartType.app, + avoid_count=3, + wash_count=None, + ) + + +@pytest.mark.parametrize( + "response", + [[{"begin": 1700000000}, 42], [42, {"begin": 1700000000}]], + ids=["dict-first", "dict-last"], +) +async def test_get_clean_record_mixed_phase_formats( + clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock, response: list +) -> None: + """Reject a record mixing structured and list phases.""" + mock_rpc_channel.send_command.return_value = response + + with pytest.raises(RoborockParsingException, match="GET_CLEAN_RECORD") as exc: + await clean_summary_trait.get_clean_record(1700000000) + + assert isinstance(exc.value.__cause__, ValueError) + + async def test_get_clean_record_dict_response( clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock,