11"""Tests for the Roborock Local Client V1."""
22
3+ import json
4+ from collections .abc import AsyncGenerator
35from 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
512from roborock .protocol import MessageParser
613from roborock .roborock_message import RoborockMessage , RoborockMessageProtocol
714from roborock .version_1_apis import RoborockLocalClientV1
815
916from .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