13
13
from .containers import DeviceData , UserData
14
14
from .exceptions import RoborockException , VacuumError
15
15
from .protocol import MessageParser , md5hex
16
- from .roborock_future import RoborockFuture
16
+ from .roborock_future import RequestKey
17
17
18
18
_LOGGER = logging .getLogger (__name__ )
19
19
CONNECT_REQUEST_ID = 0
@@ -71,12 +71,11 @@ def __init__(self, user_data: UserData, device_info: DeviceData, queue_timeout:
71
71
self ._mqtt_password = rriot .s
72
72
self ._hashed_password = md5hex (self ._mqtt_password + ":" + rriot .k )[16 :]
73
73
self ._mqtt_client .username_pw_set (self ._hashed_user , self ._hashed_password )
74
- self ._waiting_queue : dict [int , RoborockFuture ] = {}
75
74
self ._mutex = Lock ()
76
75
77
76
def _mqtt_on_connect (self , * args , ** kwargs ):
78
77
_ , __ , ___ , rc , ____ = args
79
- connection_queue = self ._waiting_queue .get ( CONNECT_REQUEST_ID )
78
+ connection_queue = self ._waiting_queue .safe_pop ( RequestKey ( CONNECT_REQUEST_ID ) )
80
79
if rc != mqtt .MQTT_ERR_SUCCESS :
81
80
message = f"Failed to connect ({ mqtt .error_string (rc )} )"
82
81
self ._logger .error (message )
@@ -95,6 +94,8 @@ def _mqtt_on_connect(self, *args, **kwargs):
95
94
self ._logger .info (f"Subscribed to topic { topic } " )
96
95
if connection_queue :
97
96
connection_queue .set_result (True )
97
+ else :
98
+ self ._logger .debug ("Connected but no connect future" )
98
99
99
100
def _mqtt_on_message (self , * args , ** kwargs ):
100
101
client , __ , msg = args
@@ -109,9 +110,11 @@ def _mqtt_on_disconnect(self, *args, **kwargs):
109
110
try :
110
111
exc = RoborockException (mqtt .error_string (rc )) if rc != mqtt .MQTT_ERR_SUCCESS else None
111
112
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 ) )
113
114
if connection_queue :
114
115
connection_queue .set_result (True )
116
+ else :
117
+ self ._logger .debug ("Disconnected but no disconnect future" )
115
118
except Exception as ex :
116
119
self ._logger .exception (ex )
117
120
@@ -121,10 +124,11 @@ def is_connected(self) -> bool:
121
124
122
125
def sync_disconnect (self ) -> Any :
123
126
if not self .is_connected ():
127
+ self ._logger .debug ("Already disconnected from mqtt" )
124
128
return None
125
129
126
130
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 ) )
128
132
rc = self ._mqtt_client .disconnect ()
129
133
130
134
if rc == mqtt .MQTT_ERR_NO_CONN :
@@ -146,7 +150,7 @@ def sync_connect(self) -> Any:
146
150
raise RoborockException ("Mqtt information was not entered. Cannot connect." )
147
151
148
152
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 ) )
150
154
self ._mqtt_client .connect (host = self ._mqtt_host , port = self ._mqtt_port , keepalive = KEEPALIVE )
151
155
self ._mqtt_client .maybe_restart_loop ()
152
156
return connected_future
0 commit comments