1717from .roborock_future import RoborockFuture
1818from .roborock_message import RoborockMessage , RoborockMessageProtocol
1919from .roborock_typing import RoborockCommand
20+ from .util import RoborockLoggerAdapter
2021
2122_LOGGER = logging .getLogger (__name__ )
2223CONNECT_REQUEST_ID = 0
@@ -34,6 +35,7 @@ def __init__(self, user_data: UserData, device_info: DeviceData) -> None:
3435 endpoint = base64 .b64encode (Utils .md5 (rriot .k .encode ())[8 :14 ]).decode ()
3536 RoborockClient .__init__ (self , endpoint , device_info )
3637 mqtt .Client .__init__ (self , protocol = mqtt .MQTTv5 )
38+ self ._logger = RoborockLoggerAdapter (device_info .device .name , _LOGGER )
3739 self ._mqtt_user = rriot .u
3840 self ._hashed_user = md5hex (self ._mqtt_user + ":" + rriot .k )[2 :10 ]
3941 url = urlparse (rriot .r .m )
@@ -57,20 +59,20 @@ def on_connect(self, *args, **kwargs):
5759 connection_queue = self ._waiting_queue .get (CONNECT_REQUEST_ID )
5860 if rc != mqtt .MQTT_ERR_SUCCESS :
5961 message = f"Failed to connect ({ mqtt .error_string (rc )} )"
60- _LOGGER .error (message )
62+ self . _logger .error (message )
6163 if connection_queue :
6264 connection_queue .resolve ((None , VacuumError (message )))
6365 return
64- _LOGGER .info (f"Connected to mqtt { self ._mqtt_host } :{ self ._mqtt_port } " )
66+ self . _logger .info (f"Connected to mqtt { self ._mqtt_host } :{ self ._mqtt_port } " )
6567 topic = f"rr/m/o/{ self ._mqtt_user } /{ self ._hashed_user } /{ self .device_info .device .duid } "
6668 (result , mid ) = self .subscribe (topic )
6769 if result != 0 :
6870 message = f"Failed to subscribe ({ mqtt .error_string (rc )} )"
69- _LOGGER .error (message )
71+ self . _logger .error (message )
7072 if connection_queue :
7173 connection_queue .resolve ((None , VacuumError (message )))
7274 return
73- _LOGGER .info (f"Subscribed to topic { topic } " )
75+ self . _logger .info (f"Subscribed to topic { topic } " )
7476 if connection_queue :
7577 connection_queue .resolve ((True , None ))
7678
@@ -80,7 +82,7 @@ def on_message(self, *args, **kwargs):
8082 messages , _ = MessageParser .parse (msg .payload , local_key = self .device_info .device .local_key )
8183 super ().on_message_received (messages )
8284 except Exception as ex :
83- _LOGGER .exception (ex )
85+ self . _logger .exception (ex )
8486
8587 def on_disconnect (self , * args , ** kwargs ):
8688 _ , __ , rc , ___ = args
@@ -93,26 +95,26 @@ def on_disconnect(self, *args, **kwargs):
9395 if connection_queue :
9496 connection_queue .resolve ((True , None ))
9597 except Exception as ex :
96- _LOGGER .exception (ex )
98+ self . _logger .exception (ex )
9799
98100 def update_client_id (self ):
99101 self ._client_id = mqtt .base62 (uuid .uuid4 ().int , padding = 22 )
100102
101103 def sync_stop_loop (self ) -> None :
102104 if self ._thread :
103- _LOGGER .info ("Stopping mqtt loop" )
105+ self . _logger .info ("Stopping mqtt loop" )
104106 super ().loop_stop ()
105107
106108 def sync_start_loop (self ) -> None :
107109 if not self ._thread or not self ._thread .is_alive ():
108110 self .sync_stop_loop ()
109- _LOGGER .info ("Starting mqtt loop" )
111+ self . _logger .info ("Starting mqtt loop" )
110112 super ().loop_start ()
111113
112114 def sync_disconnect (self ) -> bool :
113115 rc = mqtt .MQTT_ERR_AGAIN
114116 if self .is_connected ():
115- _LOGGER .info ("Disconnecting from mqtt" )
117+ self . _logger .info ("Disconnecting from mqtt" )
116118 rc = super ().disconnect ()
117119 if rc not in [mqtt .MQTT_ERR_SUCCESS , mqtt .MQTT_ERR_NO_CONN ]:
118120 raise RoborockException (f"Failed to disconnect ({ mqtt .error_string (rc )} )" )
@@ -123,7 +125,7 @@ def sync_connect(self) -> bool:
123125 if should_connect :
124126 if self ._mqtt_port is None or self ._mqtt_host is None :
125127 raise RoborockException ("Mqtt information was not entered. Cannot connect." )
126- _LOGGER .info ("Connecting to mqtt" )
128+ self . _logger .info ("Connecting to mqtt" )
127129 super ().connect (host = self ._mqtt_host , port = self ._mqtt_port , keepalive = KEEPALIVE )
128130 self .sync_start_loop ()
129131 return should_connect
@@ -162,7 +164,7 @@ async def send_message(self, roborock_message: RoborockMessage):
162164
163165 local_key = self .device_info .device .local_key
164166 msg = MessageParser .build (roborock_message , local_key , False )
165- _LOGGER .debug (f"id={ request_id } Requesting method { method } with { params } " )
167+ self . _logger .debug (f"id={ request_id } Requesting method { method } with { params } " )
166168 self ._send_msg_raw (msg )
167169 (response , err ) = await self ._async_response (request_id , response_protocol )
168170 self ._diagnostic_data [method if method is not None else "unknown" ] = {
@@ -173,9 +175,9 @@ async def send_message(self, roborock_message: RoborockMessage):
173175 if err :
174176 raise CommandVacuumError (method , err ) from err
175177 if response_protocol == RoborockMessageProtocol .MAP_RESPONSE :
176- _LOGGER .debug (f"id={ request_id } Response from { method } : { len (response )} bytes" )
178+ self . _logger .debug (f"id={ request_id } Response from { method } : { len (response )} bytes" )
177179 else :
178- _LOGGER .debug (f"id={ request_id } Response from { method } : { response } " )
180+ self . _logger .debug (f"id={ request_id } Response from { method } : { response } " )
179181 return response
180182
181183 async def _send_command (
0 commit comments