@@ -62,7 +62,14 @@ def __init__(self, host: str, local_key: str):
6262 LocalChannelParams (local_key = local_key , connect_nonce = get_next_int (10000 , 32767 ), ack_nonce = None )
6363 )
6464
65- def _update_encoder_decoder (self , params : LocalChannelParams ):
65+ def _update_encoder_decoder (self , params : LocalChannelParams ) -> None :
66+ """Update the encoder and decoder with new parameters.
67+
68+ This is invoked once with an initial set of values used for protocol
69+ negotiation. Once negotiation completes, it is updated again to set the
70+ correct nonces for the follow up communications and updates the encoder
71+ and decoder functions accordingly.
72+ """
6673 self ._params = params
6774 self ._encoder = create_local_encoder (
6875 local_key = params .local_key , connect_nonce = params .connect_nonce , ack_nonce = params .ack_nonce
@@ -71,7 +78,7 @@ def _update_encoder_decoder(self, params: LocalChannelParams):
7178 local_key = params .local_key , connect_nonce = params .connect_nonce , ack_nonce = params .ack_nonce
7279 )
7380 # Callback to decode messages and dispatch to subscribers
74- self ._data_received : Callable [[ bytes ], None ] = decoder_callback (self ._decoder , self ._subscribers , _LOGGER )
81+ self ._dispatch = decoder_callback (self ._decoder , self ._subscribers , _LOGGER )
7582
7683 async def _do_hello (self , local_protocol_version : LocalProtocolVersion ) -> LocalChannelParams | None :
7784 """Perform the initial handshaking and return encoder params if successful."""
@@ -125,6 +132,13 @@ async def _hello(self):
125132
126133 raise RoborockException ("Failed to connect to device with any known protocol" )
127134
135+ @property
136+ def protocol_version (self ) -> LocalProtocolVersion :
137+ """Return the negotiated local protocol version, or a sensible default."""
138+ if self ._local_protocol_version is not None :
139+ return self ._local_protocol_version
140+ return LocalProtocolVersion .V1
141+
128142 @property
129143 def is_connected (self ) -> bool :
130144 """Check if the channel is currently connected."""
@@ -157,6 +171,10 @@ async def connect(self) -> None:
157171 self .close ()
158172 raise
159173
174+ def _data_received (self , data : bytes ) -> None :
175+ """Invoked when data is received on the stream."""
176+ self ._dispatch (data )
177+
160178 def close (self ) -> None :
161179 """Disconnect from the device."""
162180 if self ._transport :
0 commit comments