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
18 changes: 18 additions & 0 deletions roborock/data/v1/v1_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions roborock/devices/traits/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

from . import (
camera,
carpet_boost,
child_lock,
clean_summary,
command,
Expand All @@ -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
Expand Down Expand Up @@ -126,6 +128,7 @@
__all__ = [
"PropertiesApi",
"camera",
"carpet_boost",
"child_lock",
"clean_summary",
"command",
Expand Down Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions roborock/devices/traits/v1/carpet_boost.py
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"

@infy1995 infy1995 Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 no carpet_boost trait, 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 models t4, 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.

DeviceFeatures already expresses "everything except this model" with model_blacklist (is_custom_mode_supported, is_collect_dust_mode_supported):

# roborock/device_features.py
is_carpet_boost_supported: bool = field(metadata={"model_blacklist": [RoborockProductNickname.RUBYPLUS]})
# this file
requires_feature = "is_carpet_boost_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 (a DeviceFeatures unit test is enough, there is no RubyPlus fixture); and the DeviceFeatures snapshot in tests/__snapshots__/test_supported_features.ambr needs updating.


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)])

@infy1995 infy1995 Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question on the write payload: this always sends all five fields (enable, current_integral, current_high, current_low, stall_time).

What I found on a Saros 10R: get_carpet_mode returns [{"enable": 1, "current_integral": 1000, "current_high": 500, "current_low": 1000, "stall_time": 10}], so the five fields this PR models are right for reading. That robot sets is_no_need_carpet_press_set_supported, and compared with the Roborock app plugin, the app sends only {"enable": n} for robots with that flag (generic models replay the read values). I tried exactly that: set_carpet_mode [{"enable": 0}] was accepted, get_carpet_mode then read enable 0 with the four tuning values unchanged, and the value stayed after changing the carpet mode and back. I did not try the five-field form, so I cannot say whether it is accepted there.

So sending only enable when is_no_need_carpet_press_set_supported is set (and replaying the full read otherwise) is known to work on that robot and avoids writing back stale thresholds. Alternatively, keep the replay and note in the PR which robot a full-payload write was tried on.

The single read before the first write is fine under AGENTS.md (lazy init when the cache is None).

# Optimistic update to avoid an extra refresh
self._settings = settings
self.enabled = enable
163 changes: 163 additions & 0 deletions tests/devices/traits/v1/test_carpet_boost.py
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
Loading