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
25 changes: 18 additions & 7 deletions roborock/devices/traits/a01/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from roborock.devices.traits.a01.device_feature import (
build_feature_dp_list,
build_force_load_dp_list,
supports_dp,
supports_uv_light,
)
from roborock.devices.traits.common import TraitUpdateListener
Expand Down Expand Up @@ -442,13 +443,23 @@ def supports(self, feature: ZeoFeatureBits) -> bool:
return bool(self._feature_bits & (1 << feature.value))

async def query_values(self, protocols: list[RoborockZeoProtocol]) -> dict[RoborockZeoProtocol, Any]:
"""Query the device for the values of the given protocols."""
response = await send_decoded_command(
self._channel,
{RoborockZeoProtocol.ID_QUERY: protocols},
value_encoder=json.dumps,
)
values = {protocol: convert_zeo_value(protocol, response.get(protocol)) for protocol in protocols}
"""Query the device for the values of the given protocols.

Protocols the device model is known not to support are not sent to the
device, since it would never answer them, and are returned as None.
"""
supported = [protocol for protocol in protocols if supports_dp(self._model, protocol)]
response: dict[RoborockZeoProtocol, Any] = {}
if supported:
response = await send_decoded_command(
self._channel,
{RoborockZeoProtocol.ID_QUERY: supported},
value_encoder=json.dumps,
)
Comment on lines +452 to +458

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.

When supported is empty, falling through to self._merge_query_response(values) still updates self._last_message_time (when unsubscribed), even though no message was sent or received from the device. Returning early avoids a false liveness timestamp and keeps the control flow flat:

Suggested change
response: dict[RoborockZeoProtocol, Any] = {}
if supported:
response = await send_decoded_command(
self._channel,
{RoborockZeoProtocol.ID_QUERY: supported},
value_encoder=json.dumps,
)
if not supported:
return dict.fromkeys(protocols)
response = await send_decoded_command(
self._channel,
{RoborockZeoProtocol.ID_QUERY: supported},
value_encoder=json.dumps,
)

values = {
protocol: convert_zeo_value(protocol, response[protocol]) if protocol in response else None
for protocol in protocols
}
self._merge_query_response(values)
return values

Expand Down
32 changes: 24 additions & 8 deletions roborock/devices/traits/a01/device_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@
RoborockZeoProtocol.F_C, # 10001
]

# DPs only answered by models with a softener compartment.
_SOFTENER_DPS: list[RoborockZeoProtocol] = [
RoborockZeoProtocol.SOFTENER_SET, # 212
RoborockZeoProtocol.SOFTENER_TYPE, # 214
RoborockZeoProtocol.DEFAULT_SETTING, # 225
RoborockZeoProtocol.SOFTENER_EMPTY, # 227
]


# ── Public helpers ──────────────────────────────────────────────────────

Expand Down Expand Up @@ -259,6 +267,21 @@ def supports_default_setting(model: str | None) -> bool:
return model not in _UNSUPPORTED_DEFAULT_SETTING


def supports_dp(model: str | None, dp: RoborockZeoProtocol) -> bool:
"""Return False if *model* is known to never answer a query for *dp*.

A query only completes once every requested DP is answered, so asking a
device for a DP it lacks makes the whole query time out.
"""
if dp == RoborockZeoProtocol.FEATURE_BITS:
return supports_feature_bits(model)
if dp == RoborockZeoProtocol.DEFAULT_SETTING and not supports_default_setting(model):
return False
if dp in _SOFTENER_DPS:
return has_softener_compartment(model)
return True


def supports_remote_control(model: str | None) -> bool:
"""Remote control (DP 232) is supported on all overseas models."""
if model is None:
Expand Down Expand Up @@ -291,14 +314,7 @@ def build_force_load_dp_list(model: str | None) -> list[RoborockZeoProtocol]:

# ── Softener block (212, 214, 225, 227) ──
if has_softener_compartment(model):
base.extend(
[
RoborockZeoProtocol.SOFTENER_SET, # 212
RoborockZeoProtocol.SOFTENER_TYPE, # 214
RoborockZeoProtocol.DEFAULT_SETTING, # 225
RoborockZeoProtocol.SOFTENER_EMPTY, # 227
]
)
base.extend(_SOFTENER_DPS)

# ── Smart-hosting DPs (always queried when FEATURE_BITS is supported) ──
if supports_feature_bits(model):
Expand Down
35 changes: 35 additions & 0 deletions tests/devices/traits/a01/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,41 @@ async def test_dyad_action_commands(
assert payload_data["dps"] == expected_dps


async def test_zeo_api_query_values_skips_unsupported_dps(fake_channel: FakeChannel):
"""Zeo Mini (a93) has no softener compartment and never answers the softener DPs."""
zeo_api = ZeoApi(fake_channel, model="roborock.wm.a93") # type: ignore[arg-type]
fake_channel.response_queue.append(build_a01_message({203: 4, 226: 1}))

result = await zeo_api.query_values(
[
RoborockZeoProtocol.STATE,
RoborockZeoProtocol.DETERGENT_EMPTY,
RoborockZeoProtocol.SOFTENER_TYPE,
RoborockZeoProtocol.SOFTENER_EMPTY,
]
)

assert result == {
RoborockZeoProtocol.STATE: "washing",
RoborockZeoProtocol.DETERGENT_EMPTY: True,
RoborockZeoProtocol.SOFTENER_TYPE: None,
RoborockZeoProtocol.SOFTENER_EMPTY: None,
}
assert len(fake_channel.published_messages) == 1
payload_data = json.loads(unpad(fake_channel.published_messages[0].payload, AES.block_size))

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.

RoborockMessage.payload is bytes | None, so Mypy (check_untyped_defs = true) will reject passing it directly to unpad() without narrowing it first:

Suggested change
payload_data = json.loads(unpad(fake_channel.published_messages[0].payload, AES.block_size))
message = fake_channel.published_messages[0]
assert message.payload is not None
payload_data = json.loads(unpad(message.payload, AES.block_size))

assert payload_data["dps"] == {"10000": "[203, 226]"}


async def test_zeo_api_query_values_only_unsupported_dps(fake_channel: FakeChannel):
"""Nothing is sent when the device supports none of the requested DPs."""
zeo_api = ZeoApi(fake_channel, model="roborock.wm.a93") # type: ignore[arg-type]

result = await zeo_api.query_values([RoborockZeoProtocol.SOFTENER_EMPTY])

assert result == {RoborockZeoProtocol.SOFTENER_EMPTY: None}
assert not fake_channel.published_messages


async def test_zeo_api_set_value(zeo_api: ZeoApi, fake_channel: FakeChannel):
"""Test ZeoApi set_value sends correct command."""
await zeo_api.set_value(RoborockZeoProtocol.MODE, "standard")
Expand Down