Skip to content

Commit ea8e55a

Browse files
authored
fix: Update local API protocol broken during refactoring and add additional tests for API calls (#293)
* test: add an additional local API test and fix bug in test fixture * test: fix formatting * fix: Update local API
1 parent 16ab4ff commit ea8e55a

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

roborock/local_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class _LocalProtocol(asyncio.Protocol):
2525
messages_cb: Callable[[bytes], None]
2626
connection_lost_cb: Callable[[Exception | None], None]
2727

28+
def data_received(self, bytes) -> None:
29+
"""Called when data is received from the transport."""
30+
self.messages_cb(bytes)
31+
32+
def connection_lost(self, exc: Exception | None) -> None:
33+
"""Called when the transport connection is lost."""
34+
self.connection_lost_cb(exc)
35+
2836

2937
class RoborockLocalClient(RoborockClient, ABC):
3038
"""Roborock local client base class."""

tests/test_local_api_v1.py

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
"""Tests for the Roborock Local Client V1."""
22

3+
import json
4+
from collections.abc import AsyncGenerator
35
from queue import Queue
6+
from typing import Any
7+
from unittest.mock import patch
48

9+
import pytest
10+
11+
from roborock.containers import RoomMapping
512
from roborock.protocol import MessageParser
613
from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol
714
from roborock.version_1_apis import RoborockLocalClientV1
815

916
from .mock_data import LOCAL_KEY
1017

1118

12-
def build_rpc_response(protocol: RoborockMessageProtocol, seq: int) -> bytes:
19+
def build_rpc_response(seq: int, message: dict[str, Any]) -> bytes:
20+
"""Build an encoded RPC response message."""
21+
return build_raw_response(
22+
protocol=RoborockMessageProtocol.GENERAL_REQUEST,
23+
seq=seq,
24+
payload=json.dumps(
25+
{
26+
"dps": {102: json.dumps(message)},
27+
}
28+
).encode(),
29+
)
30+
31+
32+
def build_raw_response(protocol: RoborockMessageProtocol, seq: int, payload: bytes) -> bytes:
1333
"""Build an encoded RPC response message."""
1434
message = RoborockMessage(
1535
protocol=protocol,
1636
random=23,
1737
seq=seq,
18-
payload=b"ignored",
38+
payload=payload,
1939
)
2040
return MessageParser.build(message, local_key=LOCAL_KEY)
2141

@@ -26,12 +46,50 @@ async def test_async_connect(
2646
response_queue: Queue,
2747
):
2848
"""Test that we can connect to the Roborock device."""
29-
response_queue.put(build_rpc_response(RoborockMessageProtocol.HELLO_RESPONSE, 1))
30-
response_queue.put(build_rpc_response(RoborockMessageProtocol.PING_RESPONSE, 2))
49+
response_queue.put(build_raw_response(RoborockMessageProtocol.HELLO_RESPONSE, 1, b"ignored"))
50+
response_queue.put(build_raw_response(RoborockMessageProtocol.PING_RESPONSE, 2, b"ignored"))
3151

3252
await local_client.async_connect()
3353
assert local_client.is_connected()
3454
assert received_requests.qsize() == 2
3555

3656
await local_client.async_disconnect()
3757
assert not local_client.is_connected()
58+
59+
60+
@pytest.fixture(name="connected_local_client")
61+
async def connected_local_client_fixture(
62+
response_queue: Queue,
63+
local_client: RoborockLocalClientV1,
64+
) -> AsyncGenerator[RoborockLocalClientV1, None]:
65+
response_queue.put(build_raw_response(RoborockMessageProtocol.HELLO_RESPONSE, 1, b"ignored"))
66+
response_queue.put(build_raw_response(RoborockMessageProtocol.PING_RESPONSE, 2, b"ignored"))
67+
await local_client.async_connect()
68+
yield local_client
69+
70+
71+
async def test_get_room_mapping(
72+
received_requests: Queue,
73+
response_queue: Queue,
74+
connected_local_client: RoborockLocalClientV1,
75+
) -> None:
76+
"""Test sending an arbitrary MQTT message and parsing the response."""
77+
78+
test_request_id = 5050
79+
80+
message = build_rpc_response(
81+
seq=test_request_id,
82+
message={
83+
"id": test_request_id,
84+
"result": [[16, "2362048"], [17, "2362044"]],
85+
},
86+
)
87+
response_queue.put(message)
88+
89+
with patch("roborock.version_1_apis.roborock_client_v1.get_next_int", return_value=test_request_id):
90+
room_mapping = await connected_local_client.get_room_mapping()
91+
92+
assert room_mapping == [
93+
RoomMapping(segment_id=16, iot_id="2362048"),
94+
RoomMapping(segment_id=17, iot_id="2362044"),
95+
]

0 commit comments

Comments
 (0)