1414from .containers import DeviceData , UserData
1515from .exceptions import RoborockException , VacuumError
1616from .protocol import MessageParser , md5hex
17- from .roborock_future import RoborockFuture
17+ from .roborock_future import RequestKey
1818
1919_LOGGER = logging .getLogger (__name__ )
2020CONNECT_REQUEST_ID = 0
@@ -72,12 +72,11 @@ def __init__(self, user_data: UserData, device_info: DeviceData) -> None:
7272 self ._mqtt_password = rriot .s
7373 self ._hashed_password = md5hex (self ._mqtt_password + ":" + rriot .k )[16 :]
7474 self ._mqtt_client .username_pw_set (self ._hashed_user , self ._hashed_password )
75- self ._waiting_queue : dict [int , RoborockFuture ] = {}
7675 self ._mutex = Lock ()
7776
7877 def _mqtt_on_connect (self , * args , ** kwargs ):
7978 _ , __ , ___ , rc , ____ = args
80- connection_queue = self ._waiting_queue .get ( CONNECT_REQUEST_ID )
79+ connection_queue = self ._waiting_queue .safe_pop ( RequestKey ( CONNECT_REQUEST_ID ) )
8180 if rc != mqtt .MQTT_ERR_SUCCESS :
8281 message = f"Failed to connect ({ mqtt .error_string (rc )} )"
8382 self ._logger .error (message )
@@ -98,6 +97,8 @@ def _mqtt_on_connect(self, *args, **kwargs):
9897 self ._logger .info (f"Subscribed to topic { topic } " )
9998 if connection_queue :
10099 connection_queue .set_result (True )
100+ else :
101+ self ._logger .debug ("Connected but no connect future" )
101102
102103 def _mqtt_on_message (self , * args , ** kwargs ):
103104 client , __ , msg = args
@@ -112,9 +113,11 @@ def _mqtt_on_disconnect(self, *args, **kwargs):
112113 try :
113114 exc = RoborockException (mqtt .error_string (rc )) if rc != mqtt .MQTT_ERR_SUCCESS else None
114115 super ().on_connection_lost (exc )
115- connection_queue = self ._waiting_queue .get ( DISCONNECT_REQUEST_ID )
116+ connection_queue = self ._waiting_queue .safe_pop ( RequestKey ( DISCONNECT_REQUEST_ID ) )
116117 if connection_queue :
117118 connection_queue .set_result (True )
119+ else :
120+ self ._logger .debug ("Disconnected but no disconnect future" )
118121 except Exception as ex :
119122 self ._logger .exception (ex )
120123
@@ -124,10 +127,11 @@ def is_connected(self) -> bool:
124127
125128 def _sync_disconnect (self ) -> Any :
126129 if not self .is_connected ():
130+ self ._logger .debug ("Already disconnected from mqtt" )
127131 return None
128132
129133 self ._logger .info ("Disconnecting from mqtt" )
130- disconnected_future = self ._async_response (DISCONNECT_REQUEST_ID )
134+ disconnected_future = self ._async_response (RequestKey ( DISCONNECT_REQUEST_ID ) )
131135 rc = self ._mqtt_client .disconnect ()
132136
133137 if rc == mqtt .MQTT_ERR_NO_CONN :
@@ -149,7 +153,7 @@ def _sync_connect(self) -> Any:
149153 raise RoborockException ("Mqtt information was not entered. Cannot connect." )
150154
151155 self ._logger .debug ("Connecting to mqtt" )
152- connected_future = self ._async_response (CONNECT_REQUEST_ID )
156+ connected_future = self ._async_response (RequestKey ( CONNECT_REQUEST_ID ) )
153157 self ._mqtt_client .connect (host = self ._mqtt_host , port = self ._mqtt_port , keepalive = KEEPALIVE )
154158 self ._mqtt_client .maybe_restart_loop ()
155159 return connected_future
0 commit comments