11"""Tests for the Roborock Local Client V1."""
22
33from queue import Queue
4+ from unittest .mock import patch
5+ import json
6+ from typing import Any
7+ from collections .abc import AsyncGenerator
8+
9+ import pytest
410
511from roborock .protocol import MessageParser
612from roborock .roborock_message import RoborockMessage , RoborockMessageProtocol
713from roborock .version_1_apis import RoborockLocalClientV1
14+ from roborock .containers import DeviceData , RoomMapping , S7MaxVStatus
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+ def build_raw_response (protocol : RoborockMessageProtocol , seq : int , payload : bytes ) -> bytes :
1332 """Build an encoded RPC response message."""
1433 message = RoborockMessage (
1534 protocol = protocol ,
1635 random = 23 ,
1736 seq = seq ,
18- payload = b"ignored" ,
37+ payload = payload ,
1938 )
2039 return MessageParser .build (message , local_key = LOCAL_KEY )
2140
@@ -26,12 +45,50 @@ async def test_async_connect(
2645 response_queue : Queue ,
2746):
2847 """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 ))
48+ response_queue .put (build_raw_response (RoborockMessageProtocol .HELLO_RESPONSE , 1 , b"ignored" ))
49+ response_queue .put (build_raw_response (RoborockMessageProtocol .PING_RESPONSE , 2 , b"ignored" ))
3150
3251 await local_client .async_connect ()
3352 assert local_client .is_connected ()
3453 assert received_requests .qsize () == 2
3554
3655 await local_client .async_disconnect ()
3756 assert not local_client .is_connected ()
57+
58+
59+
60+ @pytest .fixture (name = "connected_local_client" )
61+ async def connected_local_client_fixture (
62+ response_queue : Queue , local_client : RoborockLocalClientV1 ,
63+ ) -> AsyncGenerator [RoborockLocalClientV1 , None ]:
64+ response_queue .put (build_raw_response (RoborockMessageProtocol .HELLO_RESPONSE , 1 , b"ignored" ))
65+ response_queue .put (build_raw_response (RoborockMessageProtocol .PING_RESPONSE , 2 , b"ignored" ))
66+ await local_client .async_connect ()
67+ yield local_client
68+
69+
70+ async def test_get_room_mapping (
71+ received_requests : Queue ,
72+ response_queue : Queue ,
73+ connected_local_client : RoborockLocalClientV1 ,
74+ ) -> None :
75+ """Test sending an arbitrary MQTT message and parsing the response."""
76+
77+ test_request_id = 5050
78+
79+ message = build_rpc_response (
80+ seq = test_request_id ,
81+ message = {
82+ "id" : test_request_id ,
83+ "result" : [[16 , "2362048" ], [17 , "2362044" ]],
84+ },
85+ )
86+ response_queue .put (message )
87+
88+ with patch ("roborock.version_1_apis.roborock_client_v1.get_next_int" , return_value = test_request_id ):
89+ room_mapping = await connected_local_client .get_room_mapping ()
90+
91+ assert room_mapping == [
92+ RoomMapping (segment_id = 16 , iot_id = "2362048" ),
93+ RoomMapping (segment_id = 17 , iot_id = "2362044" ),
94+ ]
0 commit comments