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
23 changes: 20 additions & 3 deletions roborock/data/v1/v1_clean_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

class VacuumModes(RoborockModeEnum):
GENTLE = ("gentle", 105)
OFF = ("off", 105)
# OFF has no wire code of its own: the app sends 105 (CleanModeZero) for
# mop-only cleaning, so OFF shares its wire code with GENTLE. It keeps a
# distinct enum code so code lookups are unambiguous; use
# vacuum_mode_wire_code() when sending a code to the device.
OFF = ("off", 107)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[Priority 1 & 3 — Breaking Consumer Contract & Wire-Code Collision Risk]

VacuumModes.OFF and VacuumModes.GENTLE already have distinct .value strings ("off" vs "gentle"), so they are not Python StrEnum value aliases (unlike the WaterModes bug addressed by display_name in #990). Sharing .code = 105 reflects the real V1 wire protocol (CleanModeZero = 105 in com.roborock.tanos) and matches the exact pattern used in WaterModes below (LOW/MILD = 201, MEDIUM/STANDARD = 202, HIGH/INTENSE = 203).

Assigning a synthetic code 107 has two concrete downsides:

  1. Breaks downstream consumers: Any caller that reads mode.code from StatusTrait.fan_speed_options to call SET_CUSTOM_MODE, or compares status.fan_power == VacuumModes.OFF / VacuumModes.OFF.code, will now send invalid wire code 107 (or fail equality against 105) unless updated to call vacuum_mode_wire_code().
  2. Future firmware misclassification: 107 is an unassigned slot in Roborock's active 101–110 fan_power range. If future firmware assigns 107 to a new suction mode, VacuumModes.from_code_optional(107) and _resolve_clean_mode(107, features) will silently misclassify it as VacuumModes.OFF instead of returning None.

Consider keeping OFF = ("off", 105) (matching WaterModes) and documenting that code 105 requires DeviceFeatures context (_resolve_clean_mode / StatusTrait.fan_speed_mapping) to disambiguate, rather than inventing a synthetic wire code.

QUIET = ("quiet", 101)
BALANCED = ("balanced", 102)
TURBO = ("turbo", 103)
Expand Down Expand Up @@ -259,6 +263,17 @@ def get_mop_only_vacuum_mode(features: DeviceFeatures) -> VacuumModes:
return VacuumModes.OFF


def vacuum_mode_wire_code(mode: VacuumModes) -> int:
"""Return the wire code to send for a vacuum mode.

OFF has no wire code of its own: the app sends 105 (CleanModeZero) for
mop-only cleaning, so OFF shares its wire code with GENTLE.
"""
if mode is VacuumModes.OFF:
return VacuumModes.GENTLE.code
return mode.code


def _get_default_mopping_water_mode(features: DeviceFeatures) -> WaterModes:
"""Pick a sensible default water mode when mopping for the device."""
# Water-slide devices use a disjoint set of water codes; pick a mid-flow
Expand Down Expand Up @@ -306,7 +321,7 @@ def get_cleaning_mode_parameters(cleaning_mode: CleaningMode, features: DeviceFe
raise RoborockUnsupportedFeature(f"Cleaning mode {cleaning_mode.value!r} is not supported")

fan_power, water_box_mode, mop_mode = _get_clean_motor_mode_params(cleaning_mode, features)
params: dict[str, int] = {"fan_power": fan_power.code, "water_box_mode": water_box_mode.code}
params: dict[str, int] = {"fan_power": vacuum_mode_wire_code(fan_power), "water_box_mode": water_box_mode.code}
if features.is_clean_route_setting_supported:
params["mop_mode"] = mop_mode.code
return [params]
Expand All @@ -325,7 +340,9 @@ def _resolve_clean_mode(value: int | VacuumModes | None, features: DeviceFeature
"""Resolve a vacuum mode code, accounting for feature-specific code aliases."""
if value is None or isinstance(value, VacuumModes):
return value
if value == VacuumModes.OFF.code:
if value == VacuumModes.GENTLE.code:
# Code 105 is shared on the wire: gentle suction, or vacuum off
# (mop-only) on pure-mop devices. The app sends the same code for both.
if features.is_pure_clean_mop_supported:
return get_mop_only_vacuum_mode(features)
return VacuumModes.GENTLE
Expand Down
4 changes: 2 additions & 2 deletions roborock/devices/traits/v1/customize_clean_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import asyncio

from roborock.data import CustomizeCleanModes, SegmentCleanMode
from roborock.data.v1.v1_clean_modes import CleanRoutes, VacuumModes, WaterModes
from roborock.data.v1.v1_clean_modes import CleanRoutes, VacuumModes, WaterModes, vacuum_mode_wire_code
from roborock.data.v1.v1_containers import SEQ_TYPE_CLEAN_THEN_MOP
from roborock.devices.traits.v1 import common
from roborock.devices.traits.v1.device_features import DeviceFeaturesTrait
Expand Down Expand Up @@ -169,7 +169,7 @@ async def set_clean_then_mop(
seq_type = SEQ_TYPE_CLEAN_THEN_MOP if enabled else 0
new_entry = {
"segment": segment,
"fan_power": fan_power.code if isinstance(fan_power, VacuumModes) else fan_power,
"fan_power": (vacuum_mode_wire_code(fan_power) if isinstance(fan_power, VacuumModes) else fan_power),
"water_box_mode": water_box_mode.code if isinstance(water_box_mode, WaterModes) else water_box_mode,
"mop_mode": mop_mode.code if isinstance(mop_mode, CleanRoutes) else mop_mode,
"repeat": repeat,
Expand Down
5 changes: 4 additions & 1 deletion roborock/devices/traits/v1/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_water_modes,
get_water_slide_mode_name,
resolve_cleaning_mode,
vacuum_mode_wire_code,
)
from roborock.devices.traits.common import DpsDataConverter, TraitUpdateListener
from roborock.roborock_message import RoborockDataProtocol
Expand Down Expand Up @@ -89,7 +90,9 @@ def fan_speed_options(self) -> list[VacuumModes]:

@cached_property
def fan_speed_mapping(self) -> dict[int, str]:
return {fan.code: fan.value for fan in self.fan_speed_options}
# Keyed by wire code: OFF shares its wire code (105) with GENTLE,
# so the mapping must use the code the device actually reports.
return {vacuum_mode_wire_code(fan): fan.value for fan in self.fan_speed_options}

@cached_property
def water_mode_options(self) -> list[WaterModes]:
Expand Down
39 changes: 39 additions & 0 deletions tests/data/v1/test_v1_clean_modes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Tests for the V1 clean mode enums and wire-code helpers."""

from roborock.data.v1.v1_clean_modes import VacuumModes, vacuum_mode_wire_code


def test_vacuum_modes_have_distinct_codes() -> None:
"""Every VacuumModes member must keep a distinct code."""
codes = [mode.code for mode in VacuumModes]
assert len(codes) == len(set(codes))


def test_vacuum_modes_gentle_and_off_have_distinct_codes() -> None:
"""GENTLE and OFF must not share a code."""
assert VacuumModes.GENTLE.code != VacuumModes.OFF.code


def test_vacuum_modes_code_lookup_is_unambiguous() -> None:
"""Each code must resolve to exactly one member."""
assert VacuumModes.from_code(105) is VacuumModes.GENTLE
assert VacuumModes.from_code(VacuumModes.OFF.code) is VacuumModes.OFF
assert VacuumModes.from_code_optional(105) is VacuumModes.GENTLE
assert VacuumModes.from_code_optional(VacuumModes.OFF.code) is VacuumModes.OFF


def test_vacuum_modes_int_equality_is_unambiguous() -> None:
"""Only GENTLE equals the shared wire code 105."""
assert VacuumModes.GENTLE == 105
assert VacuumModes.OFF != 105
assert VacuumModes.OFF == VacuumModes.OFF.code


def test_vacuum_mode_wire_code() -> None:
"""OFF sends the shared wire code 105; every other mode sends its own code."""
assert vacuum_mode_wire_code(VacuumModes.OFF) == 105
assert vacuum_mode_wire_code(VacuumModes.GENTLE) == 105
for mode in VacuumModes:
if mode is VacuumModes.OFF:
continue
assert vacuum_mode_wire_code(mode) == mode.code
30 changes: 30 additions & 0 deletions tests/devices/traits/v1/test_customize_clean_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,36 @@ async def test_set_clean_then_mop_accepts_typed_modes(
}


@pytest.mark.parametrize("device_info", [SAROS_10R])
async def test_set_clean_then_mop_sends_wire_code_for_off(
customize_clean_mode_trait: CustomizeCleanModeTrait | None,
mock_rpc_channel: AsyncMock,
device_info: HomeDataDevice,
) -> None:
"""VacuumModes.OFF must send the shared wire code 105, not its enum code."""
assert customize_clean_mode_trait is not None
mock_rpc_channel.send_command.side_effect = [CUSTOMIZE_CLEAN_MODE_DATA, "ok", CUSTOMIZE_CLEAN_MODE_DATA]

await customize_clean_mode_trait.set_clean_then_mop(
7,
True,
fan_power=VacuumModes.OFF,
water_box_mode=WaterModes.HIGH,
mop_mode=CleanRoutes.DEEP,
)
sent_params = mock_rpc_channel.send_command.mock_calls[1].kwargs["params"]
segment_7 = next(entry for entry in sent_params if entry["segment"] == 7)
assert segment_7 == {
"segment": 7,
# 105 is the wire code shared by VacuumModes.OFF and VacuumModes.GENTLE.
"fan_power": 105,
"water_box_mode": WaterModes.HIGH.code,
"mop_mode": CleanRoutes.DEEP.code,
"repeat": 1,
"seq_type": 1,
}


@pytest.mark.parametrize("device_info", [SAROS_10R])
async def test_set_clean_then_mop_preserves_other_rooms(
customize_clean_mode_trait: CustomizeCleanModeTrait | None,
Expand Down
32 changes: 27 additions & 5 deletions tests/devices/traits/v1/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ def test_cleaning_mode_options() -> None:
CleaningMode.VACUUM,
),
(
VacuumModes.OFF.code,
# 105 is the wire code the device reports for mop-only cleaning,
# shared by VacuumModes.OFF and VacuumModes.GENTLE.
105,
WaterModes.STANDARD.code,
CleanRoutes.STANDARD.code,
CleaningMode.MOP,
Expand Down Expand Up @@ -473,7 +475,9 @@ def test_current_cleaning_mode_with_main_brush_lift() -> None:
is_support_main_brush_up_down_supported=True,
is_water_slide_mode_supported=True,
)
status_trait.fan_power = VacuumModes.OFF.code
# 105 is the wire code the device reports for mop-only cleaning,
# shared by VacuumModes.OFF and VacuumModes.GENTLE.
status_trait.fan_power = 105
status_trait.water_box_mode = 235
status_trait.mop_mode = CleanRoutes.STANDARD.code

Expand Down Expand Up @@ -590,7 +594,7 @@ def test_get_cleaning_mode_parameters_qrevo_edge_2() -> None:

assert get_cleaning_mode_parameters(CleaningMode.MOP, status_trait._device_features_trait) == [
{
"fan_power": VacuumModes.OFF.code,
"fan_power": 105, # Wire code shared by VacuumModes.OFF and VacuumModes.GENTLE
"water_box_mode": 235,
"mop_mode": CleanRoutes.STANDARD.code,
}
Expand Down Expand Up @@ -620,7 +624,7 @@ def test_get_cleaning_mode_parameters_without_clean_route_setting() -> None:
]
assert get_cleaning_mode_parameters(CleaningMode.MOP, status_trait._device_features_trait) == [
{
"fan_power": VacuumModes.OFF.code,
"fan_power": 105, # Wire code shared by VacuumModes.OFF and VacuumModes.GENTLE
"water_box_mode": WaterModes.STANDARD.code,
}
]
Expand All @@ -646,7 +650,7 @@ def test_get_cleaning_mode_parameters_water_slide_device() -> None:
]
assert get_cleaning_mode_parameters(CleaningMode.MOP, status_trait._device_features_trait) == [
{
"fan_power": VacuumModes.OFF.code,
"fan_power": 105, # Wire code shared by VacuumModes.OFF and VacuumModes.GENTLE
"water_box_mode": 235,
"mop_mode": CleanRoutes.STANDARD.code,
}
Expand Down Expand Up @@ -812,6 +816,24 @@ def test_current_cleaning_mode_gentle_not_mop_without_pure_mop() -> None:
assert status_trait.current_cleaning_mode == CleaningMode.VAC_AND_MOP


def test_fan_speed_name_off_on_pure_mop_device() -> None:
"""Code 105 reports as off on pure-mop devices, where OFF is the offered mode."""
status_trait = _create_cleaning_mode_status_trait()
status_trait.fan_power = 105

assert status_trait.fan_speed_mapping[105] == "off"
assert status_trait.fan_speed_name == "off"


def test_fan_speed_name_gentle_without_pure_mop() -> None:
"""Code 105 reports as gentle on devices without pure mop, where GENTLE is offered."""
status_trait = _create_cleaning_mode_status_trait(is_pure_clean_mop_supported=False)
status_trait.fan_power = 105

assert status_trait.fan_speed_mapping[105] == "gentle"
assert status_trait.fan_speed_name == "gentle"


def test_water_slide_mode_mapping() -> None:
"""Test feature-aware water mode mapping for water slide mode devices."""
short_model = mock_data.A114_PRODUCT_DATA["model"].split(".")[-1]
Expand Down
Loading