-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Add initial Q10 support #709
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
Draft
allenporter
wants to merge
6
commits into
Python-roborock:main
Choose a base branch
from
allenporter:q10
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55e75d6
feat: Add initial Q10 support
allenporter 8e974ba
chore: fix lint errors in mqtt channel
allenporter e0305cf
chore: Apply suggestions for co-pilot code review
allenporter 7a73268
chore: Update test coverage for protocol parsing
allenporter 7b5688c
chore: Improve test coverage for invalid pessage parsing
allenporter 8fe70c0
chore: Improve cancel readability
allenporter 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Thin wrapper around the MQTT channel for Roborock B01 devices.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from collections.abc import AsyncGenerator | ||
| from typing import Any | ||
|
|
||
| from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP | ||
| from roborock.exceptions import RoborockException | ||
| from roborock.protocols.b01_q10_protocol import ( | ||
| ParamsType, | ||
| decode_rpc_response, | ||
| encode_mqtt_payload, | ||
| ) | ||
|
|
||
| from .mqtt_channel import MqttChannel | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| _TIMEOUT = 10.0 | ||
|
|
||
|
|
||
| async def send_command( | ||
| mqtt_channel: MqttChannel, | ||
| command: B01_Q10_DP, | ||
| params: ParamsType, | ||
| ) -> None: | ||
| """Send a command on the MQTT channel, without waiting for a response""" | ||
| _LOGGER.debug( | ||
| "Sending B01 MQTT command: cmd=%s params=%s", | ||
| command, | ||
| params, | ||
| ) | ||
| roborock_message = encode_mqtt_payload(command, params) | ||
| _LOGGER.debug("Sending MQTT message: %s", roborock_message) | ||
| try: | ||
| await mqtt_channel.publish(roborock_message) | ||
| except RoborockException as ex: | ||
| _LOGGER.debug( | ||
| "Error sending B01 decoded command (method=%s params=%s): %s", | ||
| command, | ||
| params, | ||
| ex, | ||
| ) | ||
| raise | ||
|
|
||
|
|
||
| async def stream_decoded_responses( | ||
| mqtt_channel: MqttChannel, | ||
| ) -> AsyncGenerator[dict[B01_Q10_DP, Any], None]: | ||
| """Stream decoded DPS messages received via MQTT.""" | ||
|
|
||
| async for response_message in mqtt_channel.subscribe_stream(): | ||
| try: | ||
| decoded_dps = decode_rpc_response(response_message) | ||
| except RoborockException as ex: | ||
| _LOGGER.debug( | ||
| "Failed to decode B01 RPC response: %s: %s", | ||
| response_message, | ||
| ex, | ||
| ) | ||
| continue | ||
| yield decoded_dps |
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 |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| """Traits for B01 devices.""" | ||
|
|
||
| from .q7 import Q7PropertiesApi | ||
| from .q10 import Q10PropertiesApi | ||
|
|
||
| __all__ = ["Q7PropertiesApi", "q7", "q10"] | ||
| __all__ = [ | ||
| "Q7PropertiesApi", | ||
| "Q10PropertiesApi", | ||
| "q7", | ||
| "q10", | ||
| ] |
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 |
|---|---|---|
| @@ -1 +1,104 @@ | ||
| """Q10""" | ||
| """Traits for Q10 B01 devices.""" | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| from roborock import B01Props | ||
| from roborock.data.b01_q10.b01_q10_code_mappings import B01_Q10_DP | ||
| from roborock.devices.b01_q10_channel import ParamsType, send_command, stream_decoded_responses | ||
| from roborock.devices.mqtt_channel import MqttChannel | ||
| from roborock.devices.traits import Trait | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| __all__ = [ | ||
| "Q10PropertiesApi", | ||
| ] | ||
|
|
||
|
|
||
| class Q10PropertiesApi(Trait): | ||
| """API for interacting with B01 devices.""" | ||
|
|
||
| def __init__(self, channel: MqttChannel) -> None: | ||
| """Initialize the B01Props API.""" | ||
| self._channel = channel | ||
| self._task: asyncio.Task | None = None | ||
|
|
||
| async def start(self) -> None: | ||
| """Start any necessary subscriptions for the trait.""" | ||
| self._task = asyncio.create_task(self._run_loop()) | ||
|
|
||
| async def close(self) -> None: | ||
| """Close any resources held by the trait.""" | ||
| if self._task is not None: | ||
| self._task.cancel() | ||
| try: | ||
| await self._task | ||
| except asyncio.CancelledError: | ||
allenporter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass # ignore cancellation errors | ||
| self._task = None | ||
|
|
||
| async def start_clean(self) -> None: | ||
| """Start cleaning.""" | ||
| await self.send( | ||
| command=B01_Q10_DP.START_CLEAN, | ||
| # TODO: figure out other commands | ||
| # 1 = start cleaning | ||
| # 2 = "electoral" clean, also has "clean_parameters" | ||
| # 4 = fast create map | ||
| params={"cmd": 1}, | ||
| ) | ||
|
|
||
| async def pause_clean(self) -> None: | ||
| """Pause cleaning.""" | ||
| await self.send( | ||
| command=B01_Q10_DP.PAUSE, | ||
| params={}, | ||
| ) | ||
|
|
||
| async def resume_clean(self) -> None: | ||
| """Resume cleaning.""" | ||
| await self.send( | ||
| command=B01_Q10_DP.RESUME, | ||
| params={}, | ||
| ) | ||
|
|
||
| async def stop_clean(self) -> None: | ||
| """Stop cleaning.""" | ||
| await self.send( | ||
| command=B01_Q10_DP.STOP, | ||
| params={}, | ||
| ) | ||
|
|
||
| async def return_to_dock(self) -> None: | ||
| """Return to dock.""" | ||
| await self.send( | ||
| command=B01_Q10_DP.START_DOCK_TASK, | ||
| params={}, | ||
| ) | ||
|
|
||
| async def send(self, command: B01_Q10_DP, params: ParamsType) -> None: | ||
| """Send a command to the device.""" | ||
| await send_command( | ||
| self._channel, | ||
| command=command, | ||
| params=params, | ||
| ) | ||
|
|
||
| async def _run_loop(self) -> None: | ||
| """Run the main loop for processing incoming messages.""" | ||
| async for decoded_dps in stream_decoded_responses(self._channel): | ||
| _LOGGER.debug("Received B01 Q10 decoded DPS: %s", decoded_dps) | ||
|
|
||
| # Temporary debugging: Log all common values | ||
| if B01_Q10_DP.COMMON not in decoded_dps: | ||
allenporter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue | ||
| common_values = decoded_dps[B01_Q10_DP.COMMON] | ||
| for key, value in common_values.items(): | ||
| _LOGGER.debug("%s: %s", key, value) | ||
|
|
||
|
|
||
| def create(channel: MqttChannel) -> Q10PropertiesApi: | ||
| """Create traits for B01 devices.""" | ||
| return Q10PropertiesApi(channel) | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function tries to parse cmd as an integer code using B01_Q10_DP.from_code, but cmd is a string. This will fail with a ValueError when from_code tries to compare string cmd with integer member.code values. The code should convert cmd to int before calling from_code, like:
B01_Q10_DP.from_code(int(cmd)).