Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions roborock/data/v1/v1_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ def __repr__(self) -> str:
return _attr_repr(self)


@dataclass(repr=False)
class CleanSummaryWithDetail(CleanSummary):
"""CleanSummary with the last CleanRecord included."""

Expand Down
17 changes: 11 additions & 6 deletions roborock/devices/traits/v1/clean_summary.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}")


Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/data/v1/test_v1_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
AppInitStatus,
CleanRecord,
CleanSummary,
CleanSummaryWithDetail,
Consumable,
DnDTimer,
StatusV2,
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/devices/__snapshots__/test_v1_device.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
'cleanArea': 24258125000,
'cleanCount': 296,
'cleanTime': 1442559,
'lastCleanRecord': dict({
'area': 81122500,
'begin': 1738864366,
'duration': 4358,
'end': 1738868964,
}),
'records': list([
1756848207,
1754930385,
Expand Down
16 changes: 16 additions & 0 deletions tests/devices/traits/v1/test_clean_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading