Skip to content

Commit ca23e47

Browse files
committed
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.
1 parent 109f433 commit ca23e47

2 files changed

Lines changed: 61 additions & 20 deletions

File tree

‎roborock/devices/traits/v1/clean_summary.py‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from dataclasses import replace
23

34
from roborock.data import CleanRecord, CleanSummaryWithDetail, RoborockBase
45
from roborock.devices.traits.v1 import common
@@ -9,6 +10,12 @@
910
_LOGGER = logging.getLogger(__name__)
1011

1112

13+
def _sum_reported(values: list[int | None]) -> int | None:
14+
"""Sum the values reported by each phase, or None if no phase reported one."""
15+
reported = [value for value in values if value is not None]
16+
return sum(reported) if reported else None
17+
18+
1219
class CleanSummaryConverter(common.V1TraitDataConverter):
1320
"""Converter for CleanSummaryWithDetail objects."""
1421

@@ -41,26 +48,19 @@ def convert(self, response: common.V1ResponseData) -> CleanRecord:
4148
if isinstance(response, list):
4249
if not response:
4350
raise ValueError("Empty clean record response")
44-
if isinstance(response[-1], dict):
51+
if any(isinstance(rec, dict) for rec in response):
52+
if not all(isinstance(rec, dict) for rec in response):
53+
raise ValueError("Mixed clean record formats")
4554
records = [CleanRecord.from_dict(rec) for rec in response]
46-
final_record = records[-1]
47-
try:
48-
# This code is semi-presumptuous - so it is put in a try finally to be safe.
49-
final_record.begin = records[0].begin
50-
final_record.begin_datetime = records[0].begin_datetime
51-
final_record.start_type = records[0].start_type
52-
for rec in records[0:-1]:
53-
final_record.duration = (final_record.duration or 0) + (rec.duration or 0)
54-
final_record.area = (final_record.area or 0) + (rec.area or 0)
55-
final_record.avoid_count = (final_record.avoid_count or 0) + (rec.avoid_count or 0)
56-
final_record.wash_count = (final_record.wash_count or 0) + (rec.wash_count or 0)
57-
final_record.square_meter_area = (final_record.square_meter_area or 0) + (
58-
rec.square_meter_area or 0
59-
)
60-
return final_record
61-
except Exception: # noqa: BLE001
62-
# Return final record when an exception occurred
63-
return final_record
55+
return replace(
56+
records[-1],
57+
begin=records[0].begin,
58+
start_type=records[0].start_type,
59+
duration=_sum_reported([rec.duration for rec in records]),
60+
area=_sum_reported([rec.area for rec in records]),
61+
avoid_count=_sum_reported([rec.avoid_count for rec in records]),
62+
wash_count=_sum_reported([rec.wash_count for rec in records]),
63+
)
6464
# There are still a few unknown variables in this.
6565
begin, end, duration, area = unpack_list(response, 4)
6666
return CleanRecord(begin=begin, end=end, duration=duration, area=area)

‎tests/devices/traits/v1/test_clean_summary.py‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from roborock.data import CleanSummary
7+
from roborock.data import CleanRecord, CleanSummary, RoborockStartType
88
from roborock.devices.device import RoborockDevice
99
from roborock.devices.traits.v1.clean_summary import CleanSummaryTrait
1010
from roborock.exceptions import RoborockException, RoborockParsingException
@@ -245,6 +245,47 @@ async def test_get_clean_record_structured_responses(
245245
assert record.end == response[-1]["end"]
246246

247247

248+
async def test_get_clean_record_multi_phase_totals(
249+
clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock
250+
) -> None:
251+
"""Sum phase totals, keep the first phase's start and the last phase's outcome."""
252+
mock_rpc_channel.send_command.return_value = [
253+
{"begin": 1700000000, "end": 1700000100, "duration": 100, "area": 1000000, "start_type": 2, "complete": 0},
254+
{"begin": 1700000200, "end": 1700000300, "duration": 100, "area": 1000000, "avoid_count": 3, "complete": 0},
255+
{"begin": 1700000400, "end": 1700000500, "duration": 50, "area": 500000, "start_type": 3, "complete": 1},
256+
]
257+
258+
record = await clean_summary_trait.get_clean_record(1700000000)
259+
260+
assert record == CleanRecord(
261+
begin=1700000000,
262+
end=1700000500,
263+
duration=250,
264+
area=2500000,
265+
complete=1,
266+
start_type=RoborockStartType.app,
267+
avoid_count=3,
268+
wash_count=None,
269+
)
270+
271+
272+
@pytest.mark.parametrize(
273+
"response",
274+
[[{"begin": 1700000000}, 42], [42, {"begin": 1700000000}]],
275+
ids=["dict-first", "dict-last"],
276+
)
277+
async def test_get_clean_record_mixed_phase_formats(
278+
clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock, response: list
279+
) -> None:
280+
"""Reject a record mixing structured and list phases."""
281+
mock_rpc_channel.send_command.return_value = response
282+
283+
with pytest.raises(RoborockParsingException, match="GET_CLEAN_RECORD") as exc:
284+
await clean_summary_trait.get_clean_record(1700000000)
285+
286+
assert isinstance(exc.value.__cause__, ValueError)
287+
288+
248289
async def test_get_clean_record_dict_response(
249290
clean_summary_trait: CleanSummaryTrait,
250291
mock_rpc_channel: AsyncMock,

0 commit comments

Comments
 (0)