Skip to content

Commit b6acd57

Browse files
committed
fix: Update where the string conversion happens
1 parent 70ec12f commit b6acd57

File tree

4 files changed

+15
-35
lines changed

4 files changed

+15
-35
lines changed

roborock/devices/traits/a01/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, channel: MqttChannel) -> None:
2121

2222
async def query_values(self, protocols: list[RoborockDyadDataProtocol]) -> dict[RoborockDyadDataProtocol, Any]:
2323
"""Query the device for the values of the given Dyad protocols."""
24-
params = {RoborockDyadDataProtocol.ID_QUERY: [int(p) for p in protocols]}
24+
params = {RoborockDyadDataProtocol.ID_QUERY: str([int(p) for p in protocols])}
2525
return await send_decoded_command(self._channel, params)
2626

2727
async def set_value(self, protocol: RoborockDyadDataProtocol, value: Any) -> dict[RoborockDyadDataProtocol, Any]:
@@ -41,7 +41,7 @@ def __init__(self, channel: MqttChannel) -> None:
4141

4242
async def query_values(self, protocols: list[RoborockZeoProtocol]) -> dict[RoborockZeoProtocol, Any]:
4343
"""Query the device for the values of the given protocols."""
44-
params = {RoborockZeoProtocol.ID_QUERY: [int(p) for p in protocols]}
44+
params = {RoborockZeoProtocol.ID_QUERY: str([int(p) for p in protocols])}
4545
return await send_decoded_command(self._channel, params)
4646

4747
async def set_value(self, protocol: RoborockZeoProtocol, value: Any) -> dict[RoborockZeoProtocol, Any]:

roborock/protocols/a01_protocol.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ def encode_mqtt_payload(
2626
| dict[RoborockDyadDataProtocol | RoborockZeoProtocol, Any],
2727
) -> RoborockMessage:
2828
"""Encode payload for A01 commands over MQTT."""
29-
# The A01 protocol generally expects values to be encoded as strings.
30-
# We use json.dumps for non-string types to ensure valid JSON formatting
31-
# (e.g. [1, 2] -> "[1, 2]", True -> "true", 123 -> "123").
32-
dps_data = {"dps": {key: json.dumps(value) for key, value in data.items()}}
29+
dps_data = {"dps": data}
3330
payload = pad(json.dumps(dps_data).encode("utf-8"), AES.block_size)
3431
return RoborockMessage(
3532
protocol=RoborockMessageProtocol.RPC_REQUEST,

tests/devices/test_a01_channel.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ async def test_id_query(mock_mqtt_channel: FakeChannel):
4545
result = await send_decoded_command(mock_mqtt_channel, params) # type: ignore[call-overload]
4646

4747
# Assertions
48-
assert result == {RoborockDyadDataProtocol.WARM_LEVEL: 101, RoborockDyadDataProtocol.POWER: 75}
48+
assert result == {
49+
RoborockDyadDataProtocol.WARM_LEVEL: 101,
50+
RoborockDyadDataProtocol.POWER: 75,
51+
}
4952
mock_mqtt_channel.publish.assert_awaited_once()
5053
mock_mqtt_channel.subscribe.assert_awaited_once()

tests/protocols/test_a01_protocol.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ def test_encode_mqtt_payload_basic():
3535
assert len(result.payload) % 16 == 0 # Should be padded to AES block size
3636

3737
# Decode the payload to verify structure
38-
# With general stringification, numbers are converted to strings: 42 -> "42"
3938
decoded_data = decode_rpc_response(result)
40-
assert decoded_data == {200: '{"test": "data", "number": 42}'}
39+
assert decoded_data == {200: {"test": "data", "number": 42}}
4140

4241

4342
def test_encode_mqtt_payload_empty_data():
@@ -55,21 +54,6 @@ def test_encode_mqtt_payload_empty_data():
5554
assert decoded_data == {}
5655

5756

58-
def test_encode_mqtt_payload_list_conversion():
59-
"""Test that lists are converted to string representation (Fix validity)."""
60-
# This verifies the fix where lists must be encoded as strings
61-
data: dict[RoborockDyadDataProtocol | RoborockZeoProtocol, Any] = {RoborockDyadDataProtocol.ID_QUERY: [101, 102]}
62-
63-
result = encode_mqtt_payload(data)
64-
65-
# Decode manually to check the raw JSON structure
66-
decoded_json = json.loads(unpad(result.payload, AES.block_size).decode())
67-
68-
# ID_QUERY (10000) should be a string "[101, 102]", not a list [101, 102]
69-
assert decoded_json["dps"]["10000"] == "[101, 102]"
70-
assert isinstance(decoded_json["dps"]["10000"], str)
71-
72-
7357
def test_encode_mqtt_payload_complex_data():
7458
"""Test encoding with complex nested data."""
7559
data: dict[RoborockDyadDataProtocol | RoborockZeoProtocol, Any] = {
@@ -92,17 +76,13 @@ def test_encode_mqtt_payload_complex_data():
9276
# Decode the payload to verify structure
9377
decoded_data = decode_rpc_response(result)
9478
assert decoded_data == {
95-
201: json.dumps(
96-
{
97-
"nested": {"deep": {"value": 123}},
98-
# Note: The list inside the dictionary is NOT converted because
99-
# our fix only targets top-level list values in the dps map
100-
"list": [1, 2, 3, "test"],
101-
"boolean": True,
102-
"null": None,
103-
}
104-
),
105-
204: '"simple_value"',
79+
201: {
80+
"nested": {"deep": {"value": 123}},
81+
"list": [1, 2, 3, "test"],
82+
"boolean": True,
83+
"null": None,
84+
},
85+
204: "simple_value",
10686
}
10787

10888

0 commit comments

Comments
 (0)