Skip to content

Commit 7a391ee

Browse files
committed
fix(v1): ignore non-integer water codes in pushes
1 parent a661416 commit 7a391ee

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎roborock/devices/traits/v1/status.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@ def _drop_unresolvable_water_level(decoded_dps: dict[RoborockDataProtocol, Any])
4242
Water-slide devices push legacy codes (201-203, 208) that get_status never
4343
returns. Only slider codes, off, smart water, smart mode and custom are kept.
4444
"""
45-
code = decoded_dps.get(RoborockDataProtocol.WATER_BOX_MODE)
46-
if code is None or code in _WATER_MODE_CODES_KEPT or get_water_slide_mode_name(code) is not None:
45+
if RoborockDataProtocol.WATER_BOX_MODE not in decoded_dps:
46+
return decoded_dps
47+
# The pushed value is raw JSON, so narrow it to an integer before resolving it.
48+
raw = decoded_dps[RoborockDataProtocol.WATER_BOX_MODE]
49+
code: int | None = None
50+
if isinstance(raw, int) and not isinstance(raw, bool):
51+
code = raw
52+
elif isinstance(raw, str):
53+
try:
54+
code = int(raw)
55+
except ValueError:
56+
code = None
57+
if code is not None and (code in _WATER_MODE_CODES_KEPT or get_water_slide_mode_name(code) is not None):
4758
return decoded_dps
4859
return {key: value for key, value in decoded_dps.items() if key != RoborockDataProtocol.WATER_BOX_MODE}
4960

‎tests/devices/traits/v1/test_init.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ async def slow_refresh() -> None:
216216
assert api.status.mop_mode == 2
217217

218218

219+
async def test_malformed_water_code_does_not_stop_push_handling(
220+
props: tuple[v1.PropertiesApi, DpsListener], read_back: asyncio.Event
221+
) -> None:
222+
"""A malformed water code does not stop the consumables update or the read-back."""
223+
api, push = props
224+
api.device_features.is_water_slide_mode_supported = True # type: ignore[attr-defined]
225+
updated = Mock()
226+
api.consumables.add_update_listener(updated)
227+
228+
push({RoborockDataProtocol.WATER_BOX_MODE: "abc", RoborockDataProtocol.MAIN_BRUSH_WORK_TIME: 1234})
229+
230+
await asyncio.wait_for(read_back.wait(), timeout=TIMEOUT)
231+
updated.assert_called_once_with()
232+
assert api.consumables.main_brush_work_time == 1234
233+
234+
219235
async def test_read_back_notifies_status_listeners(props: tuple[v1.PropertiesApi, DpsListener]) -> None:
220236
"""Status listeners hear about values changed by the read-back."""
221237
api, push = props

‎tests/devices/traits/v1/test_status.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,28 @@ def test_update_from_dps_water_slide_keeps_other_water_codes(pushed: int) -> Non
944944
assert status_trait.water_box_mode == pushed
945945

946946

947+
def test_update_from_dps_water_slide_applies_numeric_string_code() -> None:
948+
"""A stringified slider code is resolved like the integer."""
949+
status_trait = _create_cleaning_mode_status_trait(is_water_slide_mode_supported=True)
950+
status_trait.water_box_mode = 225
951+
952+
status_trait.update_from_dps({RoborockDataProtocol.WATER_BOX_MODE: "235"})
953+
954+
assert status_trait.water_box_mode == 235
955+
956+
957+
@pytest.mark.parametrize("pushed", ["abc", "", True, 1.5, None, [235], {"code": 235}])
958+
def test_update_from_dps_water_slide_ignores_non_integer_codes(pushed: object) -> None:
959+
"""Malformed water codes are dropped without raising and other fields still apply."""
960+
status_trait = _create_cleaning_mode_status_trait(is_water_slide_mode_supported=True)
961+
status_trait.water_box_mode = 235
962+
963+
status_trait.update_from_dps({RoborockDataProtocol.WATER_BOX_MODE: pushed, RoborockDataProtocol.FAN_POWER: 101})
964+
965+
assert status_trait.water_box_mode == 235
966+
assert status_trait.fan_power == 101
967+
968+
947969
def test_update_from_dps_water_slide_still_applies_other_fields() -> None:
948970
"""Ignoring a legacy water level does not drop other fields in the same push."""
949971
status_trait = _create_cleaning_mode_status_trait(is_water_slide_mode_supported=True)

0 commit comments

Comments
 (0)