88import async_timeout
99
1010from .api import COMMANDS_SECURED , QUEUE_TIMEOUT , RoborockClient
11- from .containers import RoborockLocalDeviceInfo
11+ from .containers import RoborockDeviceInfo
1212from .exceptions import CommandVacuumError , RoborockConnectionException , RoborockException
13- from .roborock_message import AP_CONFIG , RoborockMessage , RoborockParser
13+ from .protocol import AP_CONFIG , MessageParser
14+ from .roborock_message import RoborockMessage
1415from .roborock_typing import CommandInfoMap , RoborockCommand
1516from .util import get_running_loop_or_create_one
1617
1718_LOGGER = logging .getLogger (__name__ )
1819
1920
2021class RoborockLocalClient (RoborockClient , asyncio .Protocol ):
21- def __init__ (self , device_info : RoborockLocalDeviceInfo ):
22+ def __init__ (self , device_info : RoborockDeviceInfo , ip : str ):
2223 super ().__init__ ("abc" , device_info )
2324 self .loop = get_running_loop_or_create_one ()
24- self .ip = device_info . network_info . ip
25+ self .ip = ip
2526 self ._batch_structs : list [RoborockMessage ] = []
2627 self ._executing = False
2728 self .remaining = b""
@@ -32,8 +33,7 @@ def data_received(self, message):
3233 if self .remaining :
3334 message = self .remaining + message
3435 self .remaining = b""
35- (parser_msg , remaining ) = RoborockParser .decode (message , self .device_info .device .local_key )
36- self .remaining = remaining
36+ parser_msg , self .remaining = MessageParser .parse (message , local_key = self .device_info .device .local_key )
3737 self .on_message_received (parser_msg )
3838
3939 def connection_lost (self , exc : Optional [Exception ]):
@@ -92,11 +92,16 @@ async def send_command(self, method: RoborockCommand, params: Optional[list | di
9292 return (await self .send_message (roborock_message ))[0 ]
9393
9494 async def async_local_response (self , roborock_message : RoborockMessage ):
95- request_id = roborock_message .get_request_id ()
96- if request_id is None :
95+ method = roborock_message .get_method ()
96+ request_id : int | None
97+ if method and not method .startswith ("get" ):
9798 request_id = roborock_message .seq
98- # response_protocol = 5 if roborock_message.prefix == secured_prefix else 4
99- response_protocol = 4
99+ response_protocol = 5
100+ else :
101+ request_id = roborock_message .get_request_id ()
102+ response_protocol = 4
103+ if request_id is None :
104+ raise RoborockException (f"Failed build message { roborock_message } " )
100105 (response , err ) = await self ._async_response (request_id , response_protocol )
101106 if err :
102107 raise CommandVacuumError ("" , err ) from err
@@ -116,7 +121,7 @@ async def send_message(self, roborock_messages: list[RoborockMessage] | Roborock
116121 if isinstance (roborock_messages , RoborockMessage ):
117122 roborock_messages = [roborock_messages ]
118123 local_key = self .device_info .device .local_key
119- msg = RoborockParser . encode (roborock_messages , local_key )
124+ msg = MessageParser . build (roborock_messages , local_key = local_key )
120125 # Send the command to the Roborock device
121126 _LOGGER .debug (f"Requesting device with { roborock_messages } " )
122127 self ._send_msg_raw (msg )
0 commit comments