-
Notifications
You must be signed in to change notification settings - Fork 74
feat: Implement L01 protocol #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
da21a0f
chore: init try based on Homey logic
Lash-L 95308ed
fix: some small changes
Lash-L 99d3252
fix: some small changes
Lash-L 67f9ed6
fix: timestamp
Lash-L d7ce9ba
fix: some misc bug changes
Lash-L 8d8ff89
fix: make sure we are connected on message send
Lash-L f926359
fix: bug fixes for 1.0
Lash-L 89a0eed
fix: potentially fix ping?
Lash-L b4b6730
chore: remove debug
Lash-L 3a5874e
fix: add version to ping
Lash-L 3362aac
fix: remove excluding ping from id check
Lash-L 8af494c
chore: some potential clean up
Lash-L fbaf426
chore: comments
Lash-L File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,28 +9,15 @@ | |
| from .. import CommandVacuumError, DeviceData, RoborockCommand | ||
| from ..api import RoborockClient | ||
| from ..exceptions import RoborockConnectionException, RoborockException, VacuumError | ||
| from ..protocol import Decoder, Encoder, create_local_decoder, create_local_encoder | ||
| from ..protocol import create_local_decoder, create_local_encoder | ||
| from ..protocols.v1_protocol import RequestMessage | ||
| from ..roborock_message import RoborockMessage, RoborockMessageProtocol | ||
| from ..util import RoborockLoggerAdapter | ||
| from ..util import RoborockLoggerAdapter, get_next_int | ||
| from .roborock_client_v1 import CLOUD_REQUIRED, RoborockClientV1 | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| _HELLO_REQUEST_MESSAGE = RoborockMessage( | ||
| protocol=RoborockMessageProtocol.HELLO_REQUEST, | ||
| seq=1, | ||
| random=22, | ||
| ) | ||
|
|
||
| _PING_REQUEST_MESSAGE = RoborockMessage( | ||
| protocol=RoborockMessageProtocol.PING_REQUEST, | ||
| seq=2, | ||
| random=23, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class _LocalProtocol(asyncio.Protocol): | ||
| """Callbacks for the Roborock local client transport.""" | ||
|
|
@@ -50,7 +37,7 @@ def connection_lost(self, exc: Exception | None) -> None: | |
| class RoborockLocalClientV1(RoborockClientV1, RoborockClient): | ||
| """Roborock local client for v1 devices.""" | ||
|
|
||
| def __init__(self, device_data: DeviceData, queue_timeout: int = 4): | ||
| def __init__(self, device_data: DeviceData, queue_timeout: int = 4, version: str | None = None): | ||
| """Initialize the Roborock local client.""" | ||
| if device_data.host is None: | ||
| raise RoborockException("Host is required") | ||
|
|
@@ -63,8 +50,10 @@ def __init__(self, device_data: DeviceData, queue_timeout: int = 4): | |
| RoborockClientV1.__init__(self, device_data, security_data=None) | ||
| RoborockClient.__init__(self, device_data) | ||
| self._local_protocol = _LocalProtocol(self._data_received, self._connection_lost) | ||
| self._encoder: Encoder = create_local_encoder(device_data.device.local_key) | ||
| self._decoder: Decoder = create_local_decoder(device_data.device.local_key) | ||
| self._version = version | ||
| self._connect_nonce = get_next_int(10000, 32767) | ||
| self._ack_nonce: int | None = None | ||
| self._set_encoder_decoder() | ||
| self.queue_timeout = queue_timeout | ||
| self._logger = RoborockLoggerAdapter(device_data.device.name, _LOGGER) | ||
|
|
||
|
|
@@ -121,20 +110,56 @@ async def async_disconnect(self) -> None: | |
| async with self._mutex: | ||
| self._sync_disconnect() | ||
|
|
||
| async def hello(self): | ||
| def _set_encoder_decoder(self): | ||
| """Updates the encoder decoder. For L01 these are updated with nonces after the first hello.""" | ||
|
Lash-L marked this conversation as resolved.
Outdated
|
||
| self._encoder = create_local_encoder(self.device_info.device.local_key, self._connect_nonce, self._ack_nonce) | ||
| self._decoder = create_local_decoder(self.device_info.device.local_key, self._connect_nonce, self._ack_nonce) | ||
|
|
||
| async def _do_hello(self, version: str) -> bool: | ||
| """Perform the initial handshaking.""" | ||
| self._logger.debug(f"Attempting to use the {version} protocol for client {self.device_info.device.duid}...") | ||
|
Lash-L marked this conversation as resolved.
Outdated
|
||
| request = RoborockMessage( | ||
| protocol=RoborockMessageProtocol.HELLO_REQUEST, | ||
| version=version.encode(), | ||
| random=self._connect_nonce, | ||
| seq=1, | ||
| ) | ||
| try: | ||
| return await self._send_message( | ||
| roborock_message=_HELLO_REQUEST_MESSAGE, | ||
| request_id=_HELLO_REQUEST_MESSAGE.seq, | ||
| response = await self._send_message( | ||
| roborock_message=request, | ||
| request_id=request.seq, | ||
| response_protocol=RoborockMessageProtocol.HELLO_RESPONSE, | ||
| ) | ||
| except Exception as e: | ||
| self._logger.error(e) | ||
| self._ack_nonce = response.random | ||
| self._set_encoder_decoder() | ||
| self._version = version | ||
| self._logger.debug(f"Client {self.device_info.device.duid} speaks the {version} protocol.") | ||
| return True | ||
| except RoborockException as e: | ||
| self._logger.debug( | ||
| f"Client {self.device_info.device.duid} did not respond or does not speak the {version} protocol. {e}" | ||
| ) | ||
| return False | ||
|
|
||
| async def hello(self): | ||
| """Send hello to the device to negotiate protocol.""" | ||
| if self._version: | ||
| # version is forced | ||
| if not await self._do_hello(self._version): | ||
| raise RoborockException(f"Failed to connect to device with protocol {self._version}") | ||
| else: | ||
| # try 1.0, then L01 | ||
| if not await self._do_hello("1.0"): | ||
| if not await self._do_hello("L01"): | ||
| raise RoborockException("Failed to connect to device with any known protocol") | ||
|
|
||
| async def ping(self) -> None: | ||
| # Realistically, this should be set here, but this is to be safe and for typing. | ||
| version = b"1.0" if self._version is None else self._version.encode() | ||
|
Lash-L marked this conversation as resolved.
Outdated
|
||
| ping_message = RoborockMessage(protocol=RoborockMessageProtocol.PING_REQUEST, version=version) | ||
| await self._send_message( | ||
| roborock_message=_PING_REQUEST_MESSAGE, | ||
| request_id=_PING_REQUEST_MESSAGE.seq, | ||
| roborock_message=ping_message, | ||
| request_id=ping_message.seq, | ||
| response_protocol=RoborockMessageProtocol.PING_RESPONSE, | ||
| ) | ||
|
Comment on lines
+166
to
194
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to set ping request statically like we did. I know we talked about this before but I wasn't fully sure. This works |
||
|
|
||
|
|
@@ -154,7 +179,9 @@ async def _send_command( | |
| if method in CLOUD_REQUIRED: | ||
| raise RoborockException(f"Method {method} is not supported over local connection") | ||
| request_message = RequestMessage(method=method, params=params) | ||
| roborock_message = request_message.encode_message(RoborockMessageProtocol.GENERAL_REQUEST) | ||
| roborock_message = request_message.encode_message( | ||
| RoborockMessageProtocol.GENERAL_REQUEST, version=self._version if self._version is not None else "1.0" | ||
| ) | ||
| self._logger.debug("Building message id %s for method %s", request_message.request_id, method) | ||
| return await self._send_message( | ||
| roborock_message, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.