Repository navigation
feat(v1): add CleanSequenceTrait for the room cleaning order #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cptntrps
wants to merge
2
commits into
Python-roborock:main
Choose a base branch
from
cptntrps:feat/clean-sequence-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Trait for the room cleaning order of Roborock devices.""" | ||
|
|
||
| from roborock.data import CleanSequence | ||
| from roborock.devices.traits.v1 import common | ||
| from roborock.roborock_typing import RoborockCommand | ||
|
|
||
| from .common import V1ResponseData | ||
|
|
||
|
|
||
| class CleanSequenceConverter(common.V1TraitDataConverter): | ||
| """Converts get_clean_sequence responses to CleanSequence.""" | ||
|
|
||
| def convert(self, response: V1ResponseData) -> CleanSequence: | ||
| """Parse the list of segment ids returned by the device.""" | ||
| if not isinstance(response, list) or not all(isinstance(room, int) for room in response): | ||
| raise ValueError(f"Unexpected CleanSequence response format: {response!r}") | ||
| return CleanSequence(rooms=list(response)) | ||
|
|
||
|
|
||
| class CleanSequenceTrait(CleanSequence, common.V1TraitMixin): | ||
| """Trait for reading and setting the order in which rooms are cleaned. | ||
|
|
||
| The device keeps this order and uses it for room cleaning | ||
| (`app_segment_clean`), whatever order the segments are sent in. | ||
| """ | ||
|
|
||
| command = RoborockCommand.GET_CLEAN_SEQUENCE | ||
| converter = CleanSequenceConverter() | ||
| requires_feature = "is_order_clean_supported" | ||
|
|
||
| async def set_order(self, rooms: list[int]) -> None: | ||
| """Set the room cleaning order. | ||
|
|
||
| An empty list clears the custom order, and the device then chooses the | ||
| order itself (the app's reset). A device rejects a new order with an | ||
| invalid status error while it is cleaning or moving; set it while the | ||
| device is idle or docked. | ||
| """ | ||
| if len(set(rooms)) != len(rooms): | ||
| raise ValueError(f"rooms must contain distinct segment ids: {rooms!r}") | ||
| await self.rpc_channel.send_command(RoborockCommand.SET_CLEAN_SEQUENCE, params=list(rooms)) | ||
| # Optimistic update to avoid an extra refresh | ||
| self.rooms = list(rooms) | ||
|
|
||
| async def reset_order(self) -> None: | ||
| """Clear the custom room order so the device uses its automatic order.""" | ||
| await self.set_order([]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """Tests for the CleanSequenceTrait class.""" | ||
|
|
||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from roborock.data import HomeDataDevice | ||
| from roborock.devices.device import RoborockDevice | ||
| from roborock.devices.traits.v1.clean_sequence import CleanSequenceTrait | ||
| from roborock.exceptions import RoborockParsingException | ||
| from roborock.roborock_typing import RoborockCommand | ||
| from tests import mock_data | ||
|
|
||
| S5E = mock_data.DEVICE_PRODUCT_PAIRS["home_data_device_s5e.json"][0] | ||
|
|
||
|
|
||
| @pytest.fixture(name="clean_sequence_trait") | ||
| def clean_sequence_trait_fixture( | ||
| device: RoborockDevice, | ||
| discover_features_fixture: None, | ||
| ) -> CleanSequenceTrait | None: | ||
| """Return the discovered CleanSequenceTrait from the device.""" | ||
| assert device.v1_properties | ||
| return device.v1_properties.clean_sequence | ||
|
|
||
|
|
||
| def test_requires_feature() -> None: | ||
| """The trait is only created on devices that report order cleaning (robot feature 123).""" | ||
| assert CleanSequenceTrait.requires_feature == "is_order_clean_supported" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [S5E]) | ||
| async def test_refresh( | ||
| clean_sequence_trait: CleanSequenceTrait | None, | ||
| mock_rpc_channel: AsyncMock, | ||
| device_info: HomeDataDevice, | ||
| ) -> None: | ||
| """The response is the list of segment ids in cleaning order.""" | ||
| assert clean_sequence_trait is not None | ||
| mock_rpc_channel.send_command.side_effect = [[8, 3, 7, 10, 1, 9]] | ||
|
|
||
| await clean_sequence_trait.refresh() | ||
|
|
||
| mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CLEAN_SEQUENCE) | ||
| assert clean_sequence_trait.rooms == [8, 3, 7, 10, 1, 9] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [S5E]) | ||
| @pytest.mark.parametrize("response", [{"unexpected": "shape"}, ["7"], [7, None]]) | ||
| async def test_refresh_bad_format( | ||
| clean_sequence_trait: CleanSequenceTrait | None, | ||
| mock_rpc_channel: AsyncMock, | ||
| device_info: HomeDataDevice, | ||
| response: object, | ||
| ) -> None: | ||
| """A response that is not a list of segment ids raises a parsing error.""" | ||
| assert clean_sequence_trait is not None | ||
| mock_rpc_channel.send_command.side_effect = [response] | ||
|
|
||
| with pytest.raises(RoborockParsingException): | ||
| await clean_sequence_trait.refresh() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [S5E]) | ||
| async def test_set_order( | ||
| clean_sequence_trait: CleanSequenceTrait | None, | ||
| mock_rpc_channel: AsyncMock, | ||
| device_info: HomeDataDevice, | ||
| ) -> None: | ||
| """set_order sends the list as is and updates the trait.""" | ||
| assert clean_sequence_trait is not None | ||
| mock_rpc_channel.send_command.side_effect = [["ok"]] | ||
|
|
||
| await clean_sequence_trait.set_order([7, 8, 3, 10, 1, 9]) | ||
|
|
||
| mock_rpc_channel.send_command.assert_called_once_with( | ||
| RoborockCommand.SET_CLEAN_SEQUENCE, params=[7, 8, 3, 10, 1, 9] | ||
| ) | ||
| assert clean_sequence_trait.rooms == [7, 8, 3, 10, 1, 9] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [S5E]) | ||
| async def test_set_order_rejects_repeated_room( | ||
| clean_sequence_trait: CleanSequenceTrait | None, | ||
| mock_rpc_channel: AsyncMock, | ||
| device_info: HomeDataDevice, | ||
| ) -> None: | ||
| """A repeated room is refused before anything is sent.""" | ||
| assert clean_sequence_trait is not None | ||
|
|
||
| with pytest.raises(ValueError): | ||
| await clean_sequence_trait.set_order([7, 7]) | ||
| mock_rpc_channel.send_command.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [S5E]) | ||
| async def test_refresh_empty( | ||
| clean_sequence_trait: CleanSequenceTrait | None, | ||
| mock_rpc_channel: AsyncMock, | ||
| device_info: HomeDataDevice, | ||
| ) -> None: | ||
| """A device with no custom order answers an empty list.""" | ||
| assert clean_sequence_trait is not None | ||
| mock_rpc_channel.send_command.side_effect = [[]] | ||
|
|
||
| await clean_sequence_trait.refresh() | ||
|
|
||
| assert clean_sequence_trait.rooms == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [S5E]) | ||
| @pytest.mark.parametrize("use_reset", [True, False]) | ||
| async def test_reset_order( | ||
| clean_sequence_trait: CleanSequenceTrait | None, | ||
| mock_rpc_channel: AsyncMock, | ||
| device_info: HomeDataDevice, | ||
| use_reset: bool, | ||
| ) -> None: | ||
| """reset_order() and set_order([]) send an empty list, which restores the automatic order.""" | ||
| assert clean_sequence_trait is not None | ||
| clean_sequence_trait.rooms = [8, 3, 7] | ||
| mock_rpc_channel.send_command.side_effect = [["ok"]] | ||
|
|
||
| if use_reset: | ||
| await clean_sequence_trait.reset_order() | ||
| else: | ||
| await clean_sequence_trait.set_order([]) | ||
|
|
||
| mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.SET_CLEAN_SEQUENCE, params=[]) | ||
| assert clean_sequence_trait.rooms == [] | ||
|
|
||
|
|
||
| async def test_unsupported_clean_sequence(clean_sequence_trait: CleanSequenceTrait | None) -> None: | ||
| """The trait is not created on a device without order cleaning (the default S7 MaxV fixture).""" | ||
| assert clean_sequence_trait is None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests — Behavior-driven feature-gating & empty-list coverage
Per
AGENTS.md(Behavior-Driven Assertions) and sibling optional-trait suites (test_customize_clean_mode.py,test_mop_dryer.py,test_dust_collection_mode.py), consider testing end-to-end discovery on an unsupported device (the default S7 MaxV fixture hasis_order_clean_supported=False):It would also be helpful to cover
refresh()when the device returns[](no custom cleaning order set), and updatetest_set_order_rejects_bad_inputifset_order([])is allowed to clear the order.