1313from .containers import DeviceData , UserData
1414from .exceptions import RoborockException , VacuumError
1515from .protocol import MessageParser , md5hex
16- from .roborock_future import RoborockFuture
16+ from .roborock_future import RequestKey
1717
1818_LOGGER = logging .getLogger (__name__ )
1919CONNECT_REQUEST_ID = 0
@@ -71,12 +71,11 @@ def __init__(self, user_data: UserData, device_info: DeviceData, queue_timeout:
7171 self ._mqtt_password = rriot .s
7272 self ._hashed_password = md5hex (self ._mqtt_password + ":" + rriot .k )[16 :]
7373 self ._mqtt_client .username_pw_set (self ._hashed_user , self ._hashed_password )
74- self ._waiting_queue : dict [int , RoborockFuture ] = {}
7574 self ._mutex = Lock ()
7675
7776 def _mqtt_on_connect (self , * args , ** kwargs ):
7877 _ , __ , ___ , rc , ____ = args
79- connection_queue = self ._waiting_queue .get ( CONNECT_REQUEST_ID )
78+ connection_queue = self ._waiting_queue .safe_pop ( RequestKey ( CONNECT_REQUEST_ID ) )
8079 if rc != mqtt .MQTT_ERR_SUCCESS :
8180 message = f"Failed to connect ({ mqtt .error_string (rc )} )"
8281 self ._logger .error (message )
@@ -95,6 +94,8 @@ def _mqtt_on_connect(self, *args, **kwargs):
9594 self ._logger .info (f"Subscribed to topic { topic } " )
9695 if connection_queue :
9796 connection_queue .set_result (True )
97+ else :
98+ self ._logger .debug ("Connected but no connect future" )
9899
99100 def _mqtt_on_message (self , * args , ** kwargs ):
100101 client , __ , msg = args
@@ -109,9 +110,11 @@ def _mqtt_on_disconnect(self, *args, **kwargs):
109110 try :
110111 exc = RoborockException (mqtt .error_string (rc )) if rc != mqtt .MQTT_ERR_SUCCESS else None
111112 super ().on_connection_lost (exc )
112- connection_queue = self ._waiting_queue .get ( DISCONNECT_REQUEST_ID )
113+ connection_queue = self ._waiting_queue .safe_pop ( RequestKey ( DISCONNECT_REQUEST_ID ) )
113114 if connection_queue :
114115 connection_queue .set_result (True )
116+ else :
117+ self ._logger .debug ("Disconnected but no disconnect future" )
115118 except Exception as ex :
116119 self ._logger .exception (ex )
117120
@@ -121,10 +124,11 @@ def is_connected(self) -> bool:
121124
122125 def sync_disconnect (self ) -> Any :
123126 if not self .is_connected ():
127+ self ._logger .debug ("Already disconnected from mqtt" )
124128 return None
125129
126130 self ._logger .info ("Disconnecting from mqtt" )
127- disconnected_future = self ._async_response (DISCONNECT_REQUEST_ID )
131+ disconnected_future = self ._async_response (RequestKey ( DISCONNECT_REQUEST_ID ) )
128132 rc = self ._mqtt_client .disconnect ()
129133
130134 if rc == mqtt .MQTT_ERR_NO_CONN :
@@ -146,7 +150,7 @@ def sync_connect(self) -> Any:
146150 raise RoborockException ("Mqtt information was not entered. Cannot connect." )
147151
148152 self ._logger .debug ("Connecting to mqtt" )
149- connected_future = self ._async_response (CONNECT_REQUEST_ID )
153+ connected_future = self ._async_response (RequestKey ( CONNECT_REQUEST_ID ) )
150154 self ._mqtt_client .connect (host = self ._mqtt_host , port = self ._mqtt_port , keepalive = KEEPALIVE )
151155 self ._mqtt_client .maybe_restart_loop ()
152156 return connected_future
0 commit comments