11import asyncio
2- import json
32import logging
4- import math
5- import time
63from asyncio import Lock , TimerHandle , Transport , get_running_loop
74from collections .abc import Callable
85from dataclasses import dataclass
129from .. import CommandVacuumError , DeviceData , RoborockCommand
1310from ..api import RoborockClient
1411from ..exceptions import RoborockConnectionException , RoborockException , VacuumError
15- from ..protocol import Decoder , Encoder , create_local_decoder , create_local_encoder
12+ from ..protocol import create_local_decoder , create_local_encoder
1613from ..protocols .v1_protocol import RequestMessage
1714from ..roborock_message import RoborockMessage , RoborockMessageProtocol
1815from ..util import RoborockLoggerAdapter , get_next_int
@@ -54,13 +51,9 @@ def __init__(self, device_data: DeviceData, queue_timeout: int = 4, version: str
5451 RoborockClient .__init__ (self , device_data )
5552 self ._local_protocol = _LocalProtocol (self ._data_received , self ._connection_lost )
5653 self ._version = version
57- self ._connect_nonce : int | None = None
54+ self ._connect_nonce = get_next_int ( 10000 , 32767 )
5855 self ._ack_nonce : int | None = None
59- if version == "L01" :
60- self ._set_l01_encoder_decoder ()
61- else :
62- self ._encoder : Encoder = create_local_encoder (device_data .device .local_key )
63- self ._decoder : Decoder = create_local_decoder (device_data .device .local_key )
56+ self ._set_encoder_decoder ()
6457 self .queue_timeout = queue_timeout
6558 self ._logger = RoborockLoggerAdapter (device_data .device .name , _LOGGER )
6659
@@ -117,15 +110,14 @@ async def async_disconnect(self) -> None:
117110 async with self ._mutex :
118111 self ._sync_disconnect ()
119112
120- def _set_l01_encoder_decoder (self ):
121- """Tell the system to use the L01 encoder/decoder ."""
113+ def _set_encoder_decoder (self ):
114+ """Updates the encoder decoder. For L01 these are updated with nonces after the first hello ."""
122115 self ._encoder = create_local_encoder (self .device_info .device .local_key , self ._connect_nonce , self ._ack_nonce )
123116 self ._decoder = create_local_decoder (self .device_info .device .local_key , self ._connect_nonce , self ._ack_nonce )
124117
125118 async def _do_hello (self , version : str ) -> bool :
126119 """Perform the initial handshaking."""
127120 self ._logger .debug (f"Attempting to use the { version } protocol for client { self .device_info .device .duid } ..." )
128- self ._connect_nonce = get_next_int (10000 , 32767 )
129121 request = RoborockMessage (
130122 protocol = RoborockMessageProtocol .HELLO_REQUEST ,
131123 version = version .encode (),
@@ -138,9 +130,8 @@ async def _do_hello(self, version: str) -> bool:
138130 request_id = request .seq ,
139131 response_protocol = RoborockMessageProtocol .HELLO_RESPONSE ,
140132 )
141- if response .version .decode () == "L01" :
142- self ._ack_nonce = response .random
143- self ._set_l01_encoder_decoder ()
133+ self ._ack_nonce = response .random
134+ self ._set_encoder_decoder ()
144135 self ._version = version
145136 self ._logger .debug (f"Client { self .device_info .device .duid } speaks the { version } protocol." )
146137 return True
@@ -187,35 +178,10 @@ async def _send_command(
187178 ):
188179 if method in CLOUD_REQUIRED :
189180 raise RoborockException (f"Method { method } is not supported over local connection" )
190- if self ._version == "L01" :
191- request_id = get_next_int (10000 , 999999 )
192- dps_payload = {
193- "id" : request_id ,
194- "method" : method ,
195- "params" : params ,
196- }
197- ts = math .floor (time .time ())
198- payload = {
199- "dps" : {str (RoborockMessageProtocol .RPC_REQUEST .value ): json .dumps (dps_payload , separators = ("," , ":" ))},
200- "t" : ts ,
201- }
202- roborock_message = RoborockMessage (
203- protocol = RoborockMessageProtocol .GENERAL_REQUEST ,
204- payload = json .dumps (payload , separators = ("," , ":" )).encode ("utf-8" ),
205- version = self ._version .encode (),
206- timestamp = ts ,
207- )
208- self ._logger .debug ("Building message id %s for method %s" , request_id , method )
209- return await self ._send_message (
210- roborock_message ,
211- request_id = request_id ,
212- response_protocol = RoborockMessageProtocol .GENERAL_REQUEST ,
213- method = method ,
214- params = params ,
215- )
216-
217181 request_message = RequestMessage (method = method , params = params )
218- roborock_message = request_message .encode_message (RoborockMessageProtocol .GENERAL_REQUEST )
182+ roborock_message = request_message .encode_message (
183+ RoborockMessageProtocol .GENERAL_REQUEST , version = self ._version if self ._version is not None else "1.0"
184+ )
219185 self ._logger .debug ("Building message id %s for method %s" , request_message .request_id , method )
220186 return await self ._send_message (
221187 roborock_message ,
0 commit comments