@@ -30,6 +30,7 @@ def __init__(self, device_data: DeviceData):
3030 self .remaining = b""
3131 self .transport : Transport | None = None
3232 self ._mutex = Lock ()
33+ self .keep_alive_func ()
3334
3435 def data_received (self , message ):
3536 if self .remaining :
@@ -45,6 +46,13 @@ def connection_lost(self, exc: Optional[Exception]):
4546 def is_connected (self ):
4647 return self .transport and self .transport .is_reading ()
4748
49+ def keep_alive_func (self , _ = None ):
50+ keep_alive_task = asyncio .gather (
51+ asyncio .sleep (10 ),
52+ self .ping (),
53+ )
54+ keep_alive_task .add_done_callback (self .keep_alive_func )
55+
4856 async def async_connect (self ) -> None :
4957 async with self ._mutex :
5058 try :
@@ -87,7 +95,18 @@ def build_roborock_message(self, method: RoborockCommand, params: Optional[list
8795 )
8896
8997 async def ping (self ):
90- return await self .send_command (RoborockCommand .APP_WAKEUP_ROBOT )
98+ request_id = 2
99+ _LOGGER .debug (f"id={ request_id } Requesting method ping with None" )
100+ try :
101+ return await self .send_message (RoborockMessage (
102+ protocol = 2 ,
103+ payload = None ,
104+ seq = request_id ,
105+ version = b'1.0' ,
106+ random = 23
107+ ))
108+ except Exception as e :
109+ _LOGGER .error (e )
91110
92111 async def send_command (self , method : RoborockCommand , params : Optional [list | dict ] = None ):
93112 roborock_message = self .build_roborock_message (method , params )
@@ -96,9 +115,9 @@ async def send_command(self, method: RoborockCommand, params: Optional[list | di
96115 async def async_local_response (self , roborock_message : RoborockMessage ):
97116 method = roborock_message .get_method ()
98117 request_id : int | None
99- if method and not method .startswith ("get" ):
118+ if not method or not method .startswith ("get" ):
100119 request_id = roborock_message .seq
101- response_protocol = 5
120+ response_protocol = request_id + 1
102121 else :
103122 request_id = roborock_message .get_request_id ()
104123 response_protocol = 4
@@ -107,7 +126,8 @@ async def async_local_response(self, roborock_message: RoborockMessage):
107126 (response , err ) = await self ._async_response (request_id , response_protocol )
108127 if err :
109128 raise CommandVacuumError ("" , err ) from err
110- _LOGGER .debug (f"id={ request_id } Response from { roborock_message .get_method ()} : { response } " )
129+ method = roborock_message .get_method () if roborock_message .protocol != 2 else "ping"
130+ _LOGGER .debug (f"id={ request_id } Response from method { method } : { response } " )
111131 return response
112132
113133 def _send_msg_raw (self , data : bytes ):
0 commit comments