1616import time
1717from asyncio import Lock
1818from asyncio .exceptions import TimeoutError , CancelledError
19- from typing import Any
19+ from typing import Any , Callable
2020from urllib .parse import urlparse
2121
2222import aiohttp
2323import paho .mqtt .client as mqtt
2424from Crypto .Cipher import AES
2525from Crypto .Util .Padding import pad , unpad
26+ from roborock .code_mappings import STATE_CODE_TO_STATUS
2627
2728from roborock .containers import (
2829 UserData ,
@@ -126,6 +127,7 @@ def __init__(self, user_data: UserData, device_map: dict[str, RoborockDeviceInfo
126127 self ._mutex = Lock ()
127128 self ._last_device_msg_in = mqtt .time_func ()
128129 self ._last_disconnection = mqtt .time_func ()
130+ self ._status_listeners : list [Callable [[str , str ], None ]] = []
129131
130132 def __del__ (self ) -> None :
131133 self .sync_disconnect ()
@@ -196,7 +198,10 @@ async def on_message(self, _client, _, msg, __=None) -> None:
196198 f"id={ request_id } Ignoring response: { data_point_response } "
197199 )
198200 elif data_point_number == "121" :
199- _LOGGER .debug (f"Remote control { data_point } " )
201+ status = STATE_CODE_TO_STATUS .get (data_point )
202+ _LOGGER .debug (f"Status updated to { status } " )
203+ for listener in self ._status_listeners :
204+ listener (device_id , status )
200205 else :
201206 _LOGGER .debug (
202207 f"Unknown data point number received { data_point_number } with { data_point } "
@@ -222,8 +227,7 @@ async def on_message(self, _client, _, msg, __=None) -> None:
222227 @run_in_executor ()
223228 async def on_disconnect (self , _client : mqtt .Client , _ , rc , __ = None ) -> None :
224229 try :
225- async with self ._mutex :
226- self ._last_disconnection = mqtt .time_func ()
230+ self ._last_disconnection = mqtt .time_func ()
227231 message = f"Roborock mqtt client disconnected (rc: { rc } )"
228232 _LOGGER .warning (message )
229233 connection_queue = self ._waiting_queue .get (1 )
@@ -248,6 +252,9 @@ async def _async_check_keepalive(self) -> None:
248252 if now - self ._last_disconnection > self ._keepalive ** 2 and now - self ._last_device_msg_in > self ._keepalive :
249253 self ._ping_t = self ._last_device_msg_in
250254
255+ def add_status_listener (self , callback : Callable [[str , str ], None ]):
256+ self ._status_listeners .append (callback )
257+
251258 def _check_keepalive (self ) -> None :
252259 self ._async_check_keepalive ()
253260 super ()._check_keepalive ()
0 commit comments