1111from roborock .roborock_message import RoborockMessage , RoborockMessageProtocol
1212
1313from ..protocols .v1_protocol import LocalProtocolVersion
14- from ..util import get_next_int
14+ from ..util import RoborockLoggerAdapter , get_next_int
1515from .channel import Channel
1616
1717_LOGGER = logging .getLogger (__name__ )
@@ -52,11 +52,12 @@ class LocalChannel(Channel):
5252 format most parsing to higher-level components.
5353 """
5454
55- def __init__ (self , host : str , local_key : str ) :
55+ def __init__ (self , host : str , local_key : str , device_uid : str ) -> None :
5656 self ._host = host
57+ self ._logger = RoborockLoggerAdapter (device_uid , _LOGGER )
5758 self ._transport : asyncio .Transport | None = None
5859 self ._protocol : _LocalProtocol | None = None
59- self ._subscribers : CallbackList [RoborockMessage ] = CallbackList (_LOGGER )
60+ self ._subscribers : CallbackList [RoborockMessage ] = CallbackList (self . _logger )
6061 self ._is_connected = False
6162 self ._local_protocol_version : LocalProtocolVersion | None = None
6263 self ._keep_alive_task : asyncio .Task [None ] | None = None
@@ -80,11 +81,11 @@ def _update_encoder_decoder(self, params: LocalChannelParams) -> None:
8081 local_key = params .local_key , connect_nonce = params .connect_nonce , ack_nonce = params .ack_nonce
8182 )
8283 # Callback to decode messages and dispatch to subscribers
83- self ._dispatch = decoder_callback (self ._decoder , self ._subscribers , _LOGGER )
84+ self ._dispatch = decoder_callback (self ._decoder , self ._subscribers , self . _logger )
8485
8586 async def _do_hello (self , local_protocol_version : LocalProtocolVersion ) -> LocalChannelParams | None :
8687 """Perform the initial handshaking and return encoder params if successful."""
87- _LOGGER .debug (
88+ self . _logger .debug (
8889 "Attempting to use the %s protocol for client %s..." ,
8990 local_protocol_version ,
9091 self ._host ,
@@ -101,7 +102,7 @@ async def _do_hello(self, local_protocol_version: LocalProtocolVersion) -> Local
101102 request_id = request .seq ,
102103 response_protocol = RoborockMessageProtocol .HELLO_RESPONSE ,
103104 )
104- _LOGGER .debug (
105+ self . _logger .debug (
105106 "Client %s speaks the %s protocol." ,
106107 self ._host ,
107108 local_protocol_version ,
@@ -110,7 +111,7 @@ async def _do_hello(self, local_protocol_version: LocalProtocolVersion) -> Local
110111 local_key = self ._params .local_key , connect_nonce = self ._params .connect_nonce , ack_nonce = response .random
111112 )
112113 except RoborockException as e :
113- _LOGGER .debug (
114+ self . _logger .debug (
114115 "Client %s did not respond or does not speak the %s protocol. %s" ,
115116 self ._host ,
116117 local_protocol_version ,
@@ -153,7 +154,7 @@ async def _keep_alive_loop(self) -> None:
153154 except asyncio .CancelledError :
154155 break
155156 except Exception :
156- _LOGGER .debug ("Keep-alive ping failed" , exc_info = True )
157+ self . _logger .debug ("Keep-alive ping failed" , exc_info = True )
157158 # Retry next interval
158159
159160 @property
@@ -176,9 +177,9 @@ def is_local_connected(self) -> bool:
176177 async def connect (self ) -> None :
177178 """Connect to the device and negotiate protocol."""
178179 if self ._is_connected :
179- _LOGGER .debug ("Unexpected call to connect when already connected" )
180+ self . _logger .debug ("Unexpected call to connect when already connected" )
180181 return
181- _LOGGER .debug ("Connecting to %s:%s" , self ._host , _PORT )
182+ self . _logger .debug ("Connecting to %s:%s" , self ._host , _PORT )
182183 loop = asyncio .get_running_loop ()
183184 protocol = _LocalProtocol (self ._data_received , self ._connection_lost )
184185 try :
@@ -208,13 +209,13 @@ def close(self) -> None:
208209 if self ._transport :
209210 self ._transport .close ()
210211 else :
211- _LOGGER .warning ("Close called but transport is already None" )
212+ self . _logger .warning ("Close called but transport is already None" )
212213 self ._transport = None
213214 self ._is_connected = False
214215
215216 def _connection_lost (self , exc : Exception | None ) -> None :
216217 """Handle connection loss."""
217- _LOGGER .debug ("Connection lost to %s" , self ._host , exc_info = exc )
218+ self . _logger .debug ("Connection lost to %s" , self ._host , exc_info = exc )
218219 if self ._keep_alive_task :
219220 self ._keep_alive_task .cancel ()
220221 self ._keep_alive_task = None
@@ -236,12 +237,12 @@ async def publish(self, message: RoborockMessage) -> None:
236237 try :
237238 encoded_msg = self ._encoder (message )
238239 except Exception as err :
239- _LOGGER .exception ("Error encoding MQTT message: %s" , err )
240+ self . _logger .exception ("Error encoding MQTT message: %s" , err )
240241 raise RoborockException (f"Failed to encode MQTT message: { err } " ) from err
241242 try :
242243 self ._transport .write (encoded_msg )
243244 except Exception as err :
244- logging .exception ("Uncaught error sending command" )
245+ self . _logger .exception ("Uncaught error sending command" )
245246 raise RoborockException (f"Failed to send message: { message } " ) from err
246247
247248 async def _send_message (
@@ -276,7 +277,7 @@ def find_response(response_message: RoborockMessage) -> None:
276277LocalSession = Callable [[str ], LocalChannel ]
277278
278279
279- def create_local_session (local_key : str ) -> LocalSession :
280+ def create_local_session (local_key : str , device_uid : str ) -> LocalSession :
280281 """Creates a local session which can create local channels.
281282
282283 This plays a role similar to the MqttSession but is really just a factory
@@ -285,6 +286,6 @@ def create_local_session(local_key: str) -> LocalSession:
285286
286287 def create_local_channel (host : str ) -> LocalChannel :
287288 """Create a LocalChannel instance for the given host."""
288- return LocalChannel (host , local_key )
289+ return LocalChannel (host , local_key , device_uid )
289290
290291 return create_local_channel
0 commit comments