Repository navigation
feat(v1): add carpet boost switch #1086
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| """Trait for the carpet boost setting.""" | ||
|
|
||
| from dataclasses import asdict, replace | ||
|
|
||
| from roborock.data import CarpetBoost, CarpetBoostSettings | ||
| from roborock.devices.traits.v1 import common | ||
| from roborock.exceptions import RoborockParsingException | ||
| from roborock.roborock_typing import RoborockCommand | ||
|
|
||
|
|
||
| class CarpetBoostConverter(common.V1TraitDataConverter): | ||
| """Converter for CarpetBoostSettings.""" | ||
|
|
||
| def convert(self, response: common.V1ResponseData) -> CarpetBoostSettings: | ||
| """Parse the response from the device into a CarpetBoostSettings.""" | ||
| if isinstance(response, list): | ||
| if not response: | ||
| raise ValueError("Empty CarpetBoostSettings response") | ||
| response = response[0] | ||
| if not isinstance(response, dict): | ||
| raise ValueError(f"Unexpected CarpetBoostSettings response format: {response!r}") | ||
| settings = CarpetBoostSettings.from_dict(response) | ||
| if None in asdict(settings).values(): | ||
| raise ValueError(f"Incomplete CarpetBoostSettings response: {response!r}") | ||
| return settings | ||
|
|
||
|
|
||
| _CONVERTER = CarpetBoostConverter() | ||
|
|
||
|
|
||
| class CarpetBoostTrait(CarpetBoost, common.V1TraitMixin, common.RoborockSwitchBase): | ||
| """Trait for turning carpet boost on or off. | ||
|
|
||
| The device expects its motor tuning values with every write, so they are | ||
| kept privately from the last read and sent back unchanged. | ||
| """ | ||
|
|
||
| command = RoborockCommand.GET_CARPET_MODE | ||
| converter = _CONVERTER | ||
| requires_feature = "is_carpet_supported" | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__() | ||
| common.V1TraitMixin.__init__(self) | ||
| self._settings: CarpetBoostSettings | None = None | ||
|
|
||
| @property | ||
| def is_on(self) -> bool: | ||
| """Return whether carpet boost is enabled.""" | ||
| return self.enabled == 1 | ||
|
|
||
| async def refresh(self) -> None: | ||
| """Refresh the boost state and the tuning values needed for writes.""" | ||
| settings = await self._read_settings() | ||
| self._settings = settings | ||
| self.enabled = settings.enable | ||
|
|
||
| async def enable(self) -> None: | ||
| """Enable carpet boost.""" | ||
| await self._set_enable(1) | ||
|
|
||
| async def disable(self) -> None: | ||
| """Disable carpet boost.""" | ||
| await self._set_enable(0) | ||
|
|
||
| async def _read_settings(self) -> CarpetBoostSettings: | ||
| response = await self.rpc_channel.send_command(self.command) | ||
| try: | ||
| return _CONVERTER.convert(response) | ||
| except (TypeError, ValueError) as err: | ||
| raise RoborockParsingException( | ||
| trait_name=type(self).__name__, | ||
| command=self.command, | ||
| payload=response, | ||
| inner_error=err, | ||
| ) from err | ||
|
|
||
| async def _set_enable(self, enable: int) -> None: | ||
| current = self._settings if self._settings is not None else await self._read_settings() | ||
| settings = replace(current, enable=enable) | ||
| await self.rpc_channel.send_command(RoborockCommand.SET_CARPET_MODE, params=[asdict(settings)]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question on the write payload: this always sends all five fields ( What I found on a Saros 10R: So sending only The single read before the first write is fine under AGENTS.md (lazy init when the cache is |
||
| # Optimistic update to avoid an extra refresh | ||
| self._settings = settings | ||
| self.enabled = enable | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| """Tests for the CarpetBoostTrait class.""" | ||
|
|
||
| from dataclasses import replace | ||
| from unittest.mock import AsyncMock, call | ||
|
|
||
| import pytest | ||
|
|
||
| from roborock.data import HomeDataDevice | ||
| from roborock.devices.device import RoborockDevice | ||
| from roborock.devices.traits.v1.carpet_boost import CarpetBoostTrait | ||
| from roborock.exceptions import RoborockParsingException, RoborockTimeout | ||
| from roborock.roborock_typing import RoborockCommand | ||
| from tests import mock_data | ||
|
|
||
| # The Saros 10R reports is_carpet_supported (robot_new_features bit 512); the copy has only that bit cleared. | ||
| SAROS_10R = mock_data.DEVICE_PRODUCT_PAIRS["home_data_device_saros_10r.json"][0] | ||
| SAROS_10R_WITHOUT_CARPET = replace(SAROS_10R, new_feature_set="0005075028ED3EDDCFFF8F7F7EFEFDFF") | ||
|
|
||
| # Captured from a Qrevo 2 Pro (roborock.vacuum.a294) via get_carpet_mode | ||
| CARPET_MODE_RESPONSE = [ | ||
| {"enable": 1, "current_integral": 1000, "current_high": 500, "current_low": 1000, "stall_time": 10} | ||
| ] | ||
| TUNING = {"current_integral": 1000, "current_high": 500, "current_low": 1000, "stall_time": 10} | ||
|
|
||
|
|
||
| @pytest.fixture(name="carpet_boost") | ||
| def carpet_boost_trait( | ||
| device: RoborockDevice, | ||
| discover_features_fixture: None, | ||
| ) -> CarpetBoostTrait | None: | ||
| """Return the trait created by feature discovery.""" | ||
| assert device.v1_properties | ||
| return device.v1_properties.carpet_boost | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [SAROS_10R_WITHOUT_CARPET]) | ||
| async def test_unsupported_without_carpet_feature( | ||
| carpet_boost: CarpetBoostTrait | None, | ||
| device_info: HomeDataDevice, | ||
| ) -> None: | ||
| """The trait is not created when the device does not report carpet support.""" | ||
| assert carpet_boost is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [SAROS_10R]) | ||
| async def test_refresh( | ||
| carpet_boost: CarpetBoostTrait | None, | ||
| device_info: HomeDataDevice, | ||
| mock_rpc_channel: AsyncMock, | ||
| ) -> None: | ||
| """Refreshing reads the boost setting; the motor tuning values are not exposed.""" | ||
| assert carpet_boost is not None | ||
| assert not carpet_boost.is_on | ||
| mock_rpc_channel.send_command.return_value = CARPET_MODE_RESPONSE | ||
|
|
||
| await carpet_boost.refresh() | ||
|
|
||
| mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CARPET_MODE) | ||
| assert carpet_boost.is_on | ||
| assert carpet_boost.as_dict() == {"enabled": 1} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [SAROS_10R]) | ||
| @pytest.mark.parametrize( | ||
| "response", | ||
| [ | ||
| pytest.param([{"enable": 1, "current_integral": 1000}], id="missing-field"), | ||
| pytest.param([{**CARPET_MODE_RESPONSE[0], "current_integral": None}], id="null-field"), | ||
| pytest.param([], id="empty-list"), | ||
| ], | ||
| ) | ||
| async def test_refresh_malformed_response( | ||
| carpet_boost: CarpetBoostTrait | None, | ||
| device_info: HomeDataDevice, | ||
| mock_rpc_channel: AsyncMock, | ||
| response: list, | ||
| ) -> None: | ||
| """A malformed response is rejected and the previously read values are kept for writes.""" | ||
| assert carpet_boost is not None | ||
| mock_rpc_channel.send_command.return_value = CARPET_MODE_RESPONSE | ||
| await carpet_boost.refresh() | ||
|
|
||
| mock_rpc_channel.send_command.return_value = response | ||
| with pytest.raises(RoborockParsingException): | ||
| await carpet_boost.refresh() | ||
| assert carpet_boost.is_on | ||
|
|
||
| mock_rpc_channel.send_command.reset_mock() | ||
| await carpet_boost.disable() | ||
| mock_rpc_channel.send_command.assert_called_once_with( | ||
| RoborockCommand.SET_CARPET_MODE, params=[{"enable": 0, **TUNING}] | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [SAROS_10R]) | ||
| async def test_disable_and_enable( | ||
| carpet_boost: CarpetBoostTrait | None, | ||
| device_info: HomeDataDevice, | ||
| mock_rpc_channel: AsyncMock, | ||
| ) -> None: | ||
| """Writes send the boost setting with the motor tuning values from the last refresh.""" | ||
| assert carpet_boost is not None | ||
| mock_rpc_channel.send_command.return_value = CARPET_MODE_RESPONSE | ||
| await carpet_boost.refresh() | ||
| mock_rpc_channel.send_command.reset_mock() | ||
|
|
||
| await carpet_boost.disable() | ||
| assert not carpet_boost.is_on | ||
| await carpet_boost.enable() | ||
| assert carpet_boost.is_on | ||
|
|
||
| assert mock_rpc_channel.send_command.call_args_list == [ | ||
| call(RoborockCommand.SET_CARPET_MODE, params=[{"enable": 0, **TUNING}]), | ||
| call(RoborockCommand.SET_CARPET_MODE, params=[{"enable": 1, **TUNING}]), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [SAROS_10R]) | ||
| async def test_first_write_reads_tuning( | ||
| carpet_boost: CarpetBoostTrait | None, | ||
| device_info: HomeDataDevice, | ||
| mock_rpc_channel: AsyncMock, | ||
| ) -> None: | ||
| """A write before any refresh reads the tuning values first, then writes.""" | ||
| assert carpet_boost is not None | ||
| mock_rpc_channel.send_command.side_effect = [CARPET_MODE_RESPONSE, {}, {}] | ||
|
|
||
| await carpet_boost.disable() | ||
| assert not carpet_boost.is_on | ||
| await carpet_boost.enable() | ||
| assert carpet_boost.is_on | ||
|
|
||
| assert mock_rpc_channel.send_command.call_args_list == [ | ||
| call(RoborockCommand.GET_CARPET_MODE), | ||
| call(RoborockCommand.SET_CARPET_MODE, params=[{"enable": 0, **TUNING}]), | ||
| call(RoborockCommand.SET_CARPET_MODE, params=[{"enable": 1, **TUNING}]), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("device_info", [SAROS_10R]) | ||
| @pytest.mark.parametrize( | ||
| ("side_effect", "expected_error"), | ||
| [ | ||
| pytest.param(RoborockTimeout("timeout"), RoborockTimeout, id="timeout"), | ||
| pytest.param([[]], RoborockParsingException, id="malformed"), | ||
| ], | ||
| ) | ||
| async def test_first_write_failed_read( | ||
| carpet_boost: CarpetBoostTrait | None, | ||
| device_info: HomeDataDevice, | ||
| mock_rpc_channel: AsyncMock, | ||
| side_effect: Exception | list, | ||
| expected_error: type[Exception], | ||
| ) -> None: | ||
| """If the initial tuning read fails, the error propagates and nothing is written.""" | ||
| assert carpet_boost is not None | ||
| mock_rpc_channel.send_command.side_effect = side_effect | ||
|
|
||
| with pytest.raises(expected_error): | ||
| await carpet_boost.enable() | ||
|
|
||
| mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_CARPET_MODE) | ||
| assert not carpet_boost.is_on |
Uh oh!
There was an error while loading. Please reload this page.
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.
Answering the open question in the description: bit 512 looks too narrow as the only gate.
The S5 Max (
s5e,tests/testdata/home_data_device_s5e.json) and S6 MaxV (a10,device_info.yaml) both have bit 512 cleared, so they would get nocarpet_boosttrait, although they are the models you named as having the feature. Bit 512 describes carpet detection; compared with the Roborock app plugin, the boost switch is offered on every model except RubyPlus (short modelst4,s4), which is a model exclusion rather than a feature bit. On a Saros 10R (bit 512 set) the read side behaves as this PR expects.DeviceFeaturesalready expresses "everything except this model" withmodel_blacklist(is_custom_mode_supported,is_collect_dust_mode_supported):That keeps the switch hidden where it does not exist, which was the concern about exposing a broken switch. Test follow-ups:
test_unsupported_without_carpet_feature(bit 512 cleared) would become "present", so its "absent" case should use RubyPlus (aDeviceFeaturesunit test is enough, there is no RubyPlus fixture); and theDeviceFeaturessnapshot intests/__snapshots__/test_supported_features.ambrneeds updating.