diff --git a/roborock/data/v1/v1_containers.py b/roborock/data/v1/v1_containers.py index 65bcba64a..7ff1f97fd 100644 --- a/roborock/data/v1/v1_containers.py +++ b/roborock/data/v1/v1_containers.py @@ -342,6 +342,7 @@ def __repr__(self) -> str: return _attr_repr(self) +@dataclass(repr=False) class CleanSummaryWithDetail(CleanSummary): """CleanSummary with the last CleanRecord included.""" diff --git a/roborock/devices/traits/v1/clean_summary.py b/roborock/devices/traits/v1/clean_summary.py index 80d0d2a2c..ebabee46c 100644 --- a/roborock/devices/traits/v1/clean_summary.py +++ b/roborock/devices/traits/v1/clean_summary.py @@ -1,6 +1,6 @@ import logging -from roborock.data import CleanRecord, CleanSummaryWithDetail, RoborockBase +from roborock.data import CleanRecord, CleanSummary, CleanSummaryWithDetail, RoborockBase from roborock.devices.traits.v1 import common from roborock.exceptions import RoborockParsingException from roborock.roborock_typing import RoborockCommand @@ -10,22 +10,26 @@ class CleanSummaryConverter(common.V1TraitDataConverter): - """Converter for CleanSummaryWithDetail objects.""" + """Converter for CleanSummary objects. + + The summary response carries no clean record, so merging the result leaves + the trait's last clean record untouched. + """ def convert(self, response: common.V1ResponseData) -> RoborockBase: """Parse the response from the device into a CleanSummary.""" if isinstance(response, dict): - return CleanSummaryWithDetail.from_dict(response) + return CleanSummary.from_dict(response) elif isinstance(response, list): clean_time, clean_area, clean_count, records = unpack_list(response, 4) - return CleanSummaryWithDetail( + return CleanSummary( clean_time=clean_time, clean_area=clean_area, clean_count=clean_count, records=records, ) elif isinstance(response, int): - return CleanSummaryWithDetail(clean_time=response) + return CleanSummary(clean_time=response) raise ValueError(f"Unexpected clean summary format: {response!r}") @@ -77,7 +81,8 @@ class CleanSummaryTrait(CleanSummaryWithDetail, common.V1TraitMixin): async def refresh(self) -> None: """Refresh the clean summary data and last clean record. - Assumes that the clean summary has already been fetched. + Assumes that the clean summary has already been fetched. The previous + clean record is kept if fetching the latest record fails. """ await super().refresh() if not self.records: diff --git a/tests/data/v1/test_v1_containers.py b/tests/data/v1/test_v1_containers.py index 6e0a95cd3..8ade27b90 100644 --- a/tests/data/v1/test_v1_containers.py +++ b/tests/data/v1/test_v1_containers.py @@ -20,6 +20,7 @@ AppInitStatus, CleanRecord, CleanSummary, + CleanSummaryWithDetail, Consumable, DnDTimer, StatusV2, @@ -266,6 +267,15 @@ def test_clean_summary(): assert cs.records[1] == 1672458041 +def test_clean_summary_with_detail(): + record = CleanRecord.from_dict(CLEAN_RECORD) + summary = CleanSummaryWithDetail(clean_time=74382, records=[1672543330], last_clean_record=record) + + data = summary.as_dict() + assert data["lastCleanRecord"]["begin"] == 1672543330 + assert CleanSummaryWithDetail.from_dict(data) == summary + + def test_clean_record(): cr = CleanRecord.from_dict(CLEAN_RECORD) assert cr.begin == 1672543330 diff --git a/tests/devices/__snapshots__/test_v1_device.ambr b/tests/devices/__snapshots__/test_v1_device.ambr index bc0d8fdb3..4c40e010f 100644 --- a/tests/devices/__snapshots__/test_v1_device.ambr +++ b/tests/devices/__snapshots__/test_v1_device.ambr @@ -171,6 +171,12 @@ 'cleanArea': 24258125000, 'cleanCount': 296, 'cleanTime': 1442559, + 'lastCleanRecord': dict({ + 'area': 81122500, + 'begin': 1738864366, + 'duration': 4358, + 'end': 1738868964, + }), 'records': list([ 1756848207, 1754930385, diff --git a/tests/devices/traits/v1/test_clean_summary.py b/tests/devices/traits/v1/test_clean_summary.py index 35d9b71a5..04fe28b77 100644 --- a/tests/devices/traits/v1/test_clean_summary.py +++ b/tests/devices/traits/v1/test_clean_summary.py @@ -304,3 +304,19 @@ async def test_get_clean_summary_no_records( # Verify the RPC call was made correctly mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CLEAN_SUMMARY) + + +async def test_get_clean_summary_keeps_last_record_when_record_fetch_fails( + clean_summary_trait: CleanSummaryTrait, mock_rpc_channel: AsyncMock +) -> None: + """Keep the previous clean record when fetching the latest record fails.""" + mock_rpc_channel.send_command.side_effect = [CLEAN_SUMMARY_DATA, CLEAN_RECORD_DATA] + await clean_summary_trait.refresh() + previous_record = clean_summary_trait.last_clean_record + assert previous_record is not None + + mock_rpc_channel.send_command.side_effect = [CLEAN_SUMMARY_DATA, RoborockException("Communication error")] + with pytest.raises(RoborockException, match="Communication error"): + await clean_summary_trait.refresh() + + assert clean_summary_trait.last_clean_record == previous_record