Skip to content

Commit 3f34e20

Browse files
committed
feat: improve B01 support
1 parent 2a800c2 commit 3f34e20

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

roborock/devices/b01_channel.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
from __future__ import annotations
44

55
import logging
6-
from typing import Any
76

87
from roborock.protocols.b01_protocol import (
98
CommandType,
109
ParamsType,
11-
decode_rpc_response,
1210
encode_mqtt_payload,
1311
)
1412

@@ -22,9 +20,8 @@ async def send_decoded_command(
2220
dps: int,
2321
command: CommandType,
2422
params: ParamsType,
25-
) -> dict[int, Any]:
23+
) -> None:
2624
"""Send a command on the MQTT channel and get a decoded response."""
2725
_LOGGER.debug("Sending MQTT command: %s", params)
2826
roborock_message = encode_mqtt_payload(dps, command, params)
29-
response = await mqtt_channel.send_message(roborock_message)
30-
return decode_rpc_response(response) # type: ignore[return-value]
27+
await mqtt_channel.send_message_no_wait(roborock_message)

roborock/devices/device_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ def device_creator(device: HomeDataDevice, product: HomeDataProduct) -> Roborock
162162
case _:
163163
raise NotImplementedError(f"Device {device.name} has unsupported category {product.category}")
164164
case DeviceVersion.B01:
165-
mqtt_channel = create_mqtt_channel(user_data, mqtt_params, mqtt_session, device)
166-
traits.append(B01PropsApi(mqtt_channel))
165+
channel = create_mqtt_channel(user_data, mqtt_params, mqtt_session, device)
166+
traits.append(B01PropsApi(channel))
167167
case _:
168168
raise NotImplementedError(f"Device {device.name} has unsupported version {device.pv}")
169169
return RoborockDevice(device, channel, traits)

roborock/devices/mqtt_channel.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def message_handler(payload: bytes) -> None:
6969
return
7070
for message in messages:
7171
_LOGGER.debug("Received message: %s", message)
72-
asyncio.create_task(self._resolve_future_with_lock(message))
72+
if message.version == b"V1":
73+
asyncio.create_task(self._resolve_future_with_lock(message))
7374
try:
7475
callback(message)
7576
except Exception as e:
@@ -95,6 +96,15 @@ async def _resolve_future_with_lock(self, message: RoborockMessage) -> None:
9596
else:
9697
_LOGGER.debug("Received message with no waiting handler: request_id=%s", request_id)
9798

99+
async def send_message_no_wait(self, message: RoborockMessage) -> None:
100+
"""Send a command message without waiting for a response."""
101+
try:
102+
encoded_msg = self._encoder(message)
103+
await self._mqtt_session.publish(self._publish_topic, encoded_msg)
104+
except Exception:
105+
logging.exception("Uncaught error sending command")
106+
raise
107+
98108
async def send_message(self, message: RoborockMessage, timeout: float = 10.0) -> RoborockMessage:
99109
"""Send a command message and wait for the response message.
100110

roborock/devices/traits/b01/props.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import logging
4-
from typing import Any
54

65
from roborock import RoborockB01Methods
76
from roborock.roborock_message import RoborockB01Props
@@ -26,6 +25,8 @@ def __init__(self, channel: MqttChannel) -> None:
2625
"""Initialize the B01Props API."""
2726
self._channel = channel
2827

29-
async def query_values(self, props: list[RoborockB01Props]) -> dict[int, Any]:
28+
async def query_values(self, props: list[RoborockB01Props]) -> None:
3029
"""Query the device for the values of the given Dyad protocols."""
31-
return await send_decoded_command(self._channel, dps=10000, command=RoborockB01Methods.GET_PROP, params=props)
30+
return await send_decoded_command(
31+
self._channel, dps=10000, command=RoborockB01Methods.GET_PROP, params={"property": props}
32+
)

roborock/protocols/b01_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
def encode_mqtt_payload(dps: int, command: CommandType, params: ParamsType) -> RoborockMessage:
2525
"""Encode payload for B01 commands over MQTT."""
26-
dps_data = {"dps": {dps: {"method": command, "params": params or []}}}
26+
dps_data = {"dps": {dps: {"method": str(command), "msgId": "1751755654575", "params": params or []}}}
2727
payload = pad(json.dumps(dps_data).encode("utf-8"), AES.block_size)
2828
return RoborockMessage(
2929
protocol=RoborockMessageProtocol.RPC_REQUEST,

0 commit comments

Comments
 (0)