1313from ..protocols .v1_protocol import LocalProtocolVersion
1414from ..util import get_next_int
1515from .channel import Channel
16+ from .logger import DeviceLoggerAdapter
1617
1718_LOGGER = logging .getLogger (__name__ )
1819_PORT = 58867
@@ -52,11 +53,12 @@ class LocalChannel(Channel):
5253 format most parsing to higher-level components.
5354 """
5455
55- def __init__ (self , host : str , local_key : str ) :
56+ def __init__ (self , host : str , local_key : str , device_uid : str ) -> None :
5657 self ._host = host
58+ self ._logger = DeviceLoggerAdapter (_LOGGER , device_uid )
5759 self ._transport : asyncio .Transport | None = None
5860 self ._protocol : _LocalProtocol | None = None
59- self ._subscribers : CallbackList [RoborockMessage ] = CallbackList (_LOGGER )
61+ self ._subscribers : CallbackList [RoborockMessage ] = CallbackList (self . _logger )
6062 self ._is_connected = False
6163 self ._local_protocol_version : LocalProtocolVersion | None = None
6264 self ._keep_alive_task : asyncio .Task [None ] | None = None
@@ -80,11 +82,11 @@ def _update_encoder_decoder(self, params: LocalChannelParams) -> None:
8082 local_key = params .local_key , connect_nonce = params .connect_nonce , ack_nonce = params .ack_nonce
8183 )
8284 # Callback to decode messages and dispatch to subscribers
83- self ._dispatch = decoder_callback (self ._decoder , self ._subscribers , _LOGGER )
85+ self ._dispatch = decoder_callback (self ._decoder , self ._subscribers , self . _logger )
8486
8587 async def _do_hello (self , local_protocol_version : LocalProtocolVersion ) -> LocalChannelParams | None :
8688 """Perform the initial handshaking and return encoder params if successful."""
87- _LOGGER .debug (
89+ self . _logger .debug (
8890 "Attempting to use the %s protocol for client %s..." ,
8991 local_protocol_version ,
9092 self ._host ,
@@ -101,7 +103,7 @@ async def _do_hello(self, local_protocol_version: LocalProtocolVersion) -> Local
101103 request_id = request .seq ,
102104 response_protocol = RoborockMessageProtocol .HELLO_RESPONSE ,
103105 )
104- _LOGGER .debug (
106+ self . _logger .debug (
105107 "Client %s speaks the %s protocol." ,
106108 self ._host ,
107109 local_protocol_version ,
@@ -110,7 +112,7 @@ async def _do_hello(self, local_protocol_version: LocalProtocolVersion) -> Local
110112 local_key = self ._params .local_key , connect_nonce = self ._params .connect_nonce , ack_nonce = response .random
111113 )
112114 except RoborockException as e :
113- _LOGGER .debug (
115+ self . _logger .debug (
114116 "Client %s did not respond or does not speak the %s protocol. %s" ,
115117 self ._host ,
116118 local_protocol_version ,
@@ -153,7 +155,7 @@ async def _keep_alive_loop(self) -> None:
153155 except asyncio .CancelledError :
154156 break
155157 except Exception :
156- _LOGGER .debug ("Keep-alive ping failed" , exc_info = True )
158+ self . _logger .debug ("Keep-alive ping failed" , exc_info = True )
157159 # Retry next interval
158160
159161 @property
@@ -176,9 +178,9 @@ def is_local_connected(self) -> bool:
176178 async def connect (self ) -> None :
177179 """Connect to the device and negotiate protocol."""
178180 if self ._is_connected :
179- _LOGGER .debug ("Unexpected call to connect when already connected" )
181+ self . _logger .debug ("Unexpected call to connect when already connected" )
180182 return
181- _LOGGER .debug ("Connecting to %s:%s" , self ._host , _PORT )
183+ self . _logger .debug ("Connecting to %s:%s" , self ._host , _PORT )
182184 loop = asyncio .get_running_loop ()
183185 protocol = _LocalProtocol (self ._data_received , self ._connection_lost )
184186 try :
@@ -208,13 +210,13 @@ def close(self) -> None:
208210 if self ._transport :
209211 self ._transport .close ()
210212 else :
211- _LOGGER .warning ("Close called but transport is already None" )
213+ self . _logger .warning ("Close called but transport is already None" )
212214 self ._transport = None
213215 self ._is_connected = False
214216
215217 def _connection_lost (self , exc : Exception | None ) -> None :
216218 """Handle connection loss."""
217- _LOGGER .debug ("Connection lost to %s" , self ._host , exc_info = exc )
219+ self . _logger .debug ("Connection lost to %s" , self ._host , exc_info = exc )
218220 if self ._keep_alive_task :
219221 self ._keep_alive_task .cancel ()
220222 self ._keep_alive_task = None
@@ -236,12 +238,12 @@ async def publish(self, message: RoborockMessage) -> None:
236238 try :
237239 encoded_msg = self ._encoder (message )
238240 except Exception as err :
239- _LOGGER .exception ("Error encoding MQTT message: %s" , err )
241+ self . _logger .exception ("Error encoding MQTT message: %s" , err )
240242 raise RoborockException (f"Failed to encode MQTT message: { err } " ) from err
241243 try :
242244 self ._transport .write (encoded_msg )
243245 except Exception as err :
244- logging .exception ("Uncaught error sending command" )
246+ self . _logger .exception ("Uncaught error sending command" )
245247 raise RoborockException (f"Failed to send message: { message } " ) from err
246248
247249 async def _send_message (
@@ -276,7 +278,7 @@ def find_response(response_message: RoborockMessage) -> None:
276278LocalSession = Callable [[str ], LocalChannel ]
277279
278280
279- def create_local_session (local_key : str ) -> LocalSession :
281+ def create_local_session (local_key : str , device_uid : str ) -> LocalSession :
280282 """Creates a local session which can create local channels.
281283
282284 This plays a role similar to the MqttSession but is really just a factory
@@ -285,6 +287,6 @@ def create_local_session(local_key: str) -> LocalSession:
285287
286288 def create_local_channel (host : str ) -> LocalChannel :
287289 """Create a LocalChannel instance for the given host."""
288- return LocalChannel (host , local_key )
290+ return LocalChannel (host , local_key , device_uid )
289291
290292 return create_local_channel
0 commit comments