From 90caf4f24af06577d96f376c395c2ac6b79f47de Mon Sep 17 00:00:00 2001 From: Harry Coureau Date: Sun, 15 Nov 2026 01:38:07 +0000 Subject: [PATCH] feat(v1): add carpet boost switch get/set_carpet_mode had no trait. The setter needs the motor tuning values (current_integral, current_high, current_low, stall_time) along with the enable flag, so a plain on/off command is not enough. Add CarpetBoostTrait (device.v1_properties.carpet_boost), gated on is_carpet_supported. Named "carpet boost" to keep it apart from the separate carpet clean mode. Only the on/off state is public; the tuning values from the last read are kept privately and sent back unchanged on enable() and disable(). A write before any refresh reads them first. Empty or incomplete replies raise a parsing error and keep the previously read values. Co-Authored-By: Claude Opus 5.5 --- roborock/data/v1/v1_containers.py | 18 ++ roborock/devices/traits/v1/__init__.py | 4 + roborock/devices/traits/v1/carpet_boost.py | 84 ++++++++++ tests/devices/traits/v1/test_carpet_boost.py | 163 +++++++++++++++++++ 4 files changed, 269 insertions(+) create mode 100644 roborock/devices/traits/v1/carpet_boost.py create mode 100644 tests/devices/traits/v1/test_carpet_boost.py diff --git a/roborock/data/v1/v1_containers.py b/roborock/data/v1/v1_containers.py index 91dc98f0b..a304bed1e 100644 --- a/roborock/data/v1/v1_containers.py +++ b/roborock/data/v1/v1_containers.py @@ -611,3 +611,21 @@ class FlowLedStatus(RoborockBase): @dataclass class LedStatus(RoborockBase): status: int = 0 + + +@dataclass +class CarpetBoost(RoborockBase): + """Carpet boost (`get_carpet_mode`) on/off state.""" + + enabled: int | None = None + + +@dataclass +class CarpetBoostSettings(RoborockBase): + """Full `get_carpet_mode`/`set_carpet_mode` payload: the boost flag plus motor tuning values.""" + + enable: int + current_integral: int + current_high: int + current_low: int + stall_time: int diff --git a/roborock/devices/traits/v1/__init__.py b/roborock/devices/traits/v1/__init__.py index 1c33dcbb8..27bab524f 100644 --- a/roborock/devices/traits/v1/__init__.py +++ b/roborock/devices/traits/v1/__init__.py @@ -70,6 +70,7 @@ from . import ( camera, + carpet_boost, child_lock, clean_summary, command, @@ -96,6 +97,7 @@ wash_towel_mode, ) from .camera import CameraTrait +from .carpet_boost import CarpetBoostTrait from .child_lock import ChildLockTrait from .clean_summary import CleanSummaryTrait from .command import CommandTrait @@ -126,6 +128,7 @@ __all__ = [ "PropertiesApi", "camera", + "carpet_boost", "child_lock", "clean_summary", "command", @@ -177,6 +180,7 @@ class PropertiesApi(Trait): # Optional features that may not be supported on all devices camera: CameraTrait | None = None + carpet_boost: CarpetBoostTrait | None = None child_lock: ChildLockTrait | None = None led_status: LedStatusTrait | None = None flow_led_status: FlowLedStatusTrait | None = None diff --git a/roborock/devices/traits/v1/carpet_boost.py b/roborock/devices/traits/v1/carpet_boost.py new file mode 100644 index 000000000..845688a86 --- /dev/null +++ b/roborock/devices/traits/v1/carpet_boost.py @@ -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)]) + # Optimistic update to avoid an extra refresh + self._settings = settings + self.enabled = enable diff --git a/tests/devices/traits/v1/test_carpet_boost.py b/tests/devices/traits/v1/test_carpet_boost.py new file mode 100644 index 000000000..694215e69 --- /dev/null +++ b/tests/devices/traits/v1/test_carpet_boost.py @@ -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