-
Notifications
You must be signed in to change notification settings - Fork 65
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
base: main
Are you sure you want to change the base?
Changes from all commits
55e75d6
8e974ba
e0305cf
7a73268
7b5688c
8fe70c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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", | ||
| ] |
| 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={}, | ||
| ) | ||
|
Comment on lines
53
to
79
|
||
|
|
||
| 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) | ||
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)).