-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Update device manager and device to establish an MQTT subscription #409
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 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e640e47
feat: Update device manager and device to establish an MQTT subscription
allenporter 8d16c95
feat: Add test coverage to device modules
allenporter e49fd95
feat: Add test coverage for device manager close
allenporter 5c33055
feat: Update roborock/devices/mqtt_channel.py
allenporter 341d96d
feat: Apply suggestions from code review
allenporter 06b9178
feat: Add support for sending/recieving messages
allenporter 364e88e
feat: Simplify rpc handling and tests
allenporter a558e12
feat: Gather tasks
allenporter 63d7bba
feat: Add debug lines
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import logging | ||
| from collections.abc import Callable | ||
|
|
||
| from roborock.containers import RRiot | ||
| from roborock.mqtt.session import MqttParams, MqttSession | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class MqttChannel: | ||
| """RPC-style channel for communicating with a specific device over MQTT. | ||
|
|
||
| This currently only supports listening to messages and does not yet | ||
| support RPC functionality. | ||
| """ | ||
|
|
||
| def __init__(self, mqtt_session: MqttSession, duid: str, rriot: RRiot, mqtt_params: MqttParams): | ||
| self._mqtt_session = mqtt_session | ||
| self._duid = duid | ||
| self._rriot = rriot | ||
| self._mqtt_params = mqtt_params | ||
| self._unsub: Callable[[], None] | None = None | ||
|
allenporter marked this conversation as resolved.
Outdated
|
||
|
|
||
| @property | ||
| def _publish_topic(self) -> str: | ||
| """Topic to send commands to the device.""" | ||
| return f"rr/m/i/{self._rriot.u}/{self._mqtt_params.username}/{self._duid}" | ||
|
|
||
| @property | ||
| def _subscribe_topic(self) -> str: | ||
| """Topic to receive responses from the device.""" | ||
| return f"rr/m/o/{self._rriot.u}/{self._mqtt_params.username}/{self._duid}" | ||
|
|
||
| async def subscribe(self, callback: Callable[[bytes], None]) -> Callable[[], None]: | ||
| """Subscribe to the device's response topic. | ||
|
|
||
| The callback will be called with the message payload when a message is received. | ||
| If already subscribed, raises ValueError. | ||
|
allenporter marked this conversation as resolved.
Outdated
|
||
|
|
||
| Returns a callable that can be used to unsubscribe from the topic. | ||
| """ | ||
| if self._unsub: | ||
| raise ValueError("Already subscribed to the response topic") | ||
|
allenporter marked this conversation as resolved.
Outdated
|
||
| return await self._mqtt_session.subscribe(self._subscribe_topic, callback) | ||
|
allenporter marked this conversation as resolved.
Outdated
|
||
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,40 @@ | ||
| """Tests for the Device class.""" | ||
|
|
||
| from unittest.mock import AsyncMock, Mock | ||
|
|
||
| from roborock.containers import HomeData, UserData | ||
| from roborock.devices.device import DeviceVersion, RoborockDevice | ||
|
|
||
| from .. import mock_data | ||
|
|
||
| USER_DATA = UserData.from_dict(mock_data.USER_DATA) | ||
| HOME_DATA = HomeData.from_dict(mock_data.HOME_DATA_RAW) | ||
|
|
||
|
|
||
| async def test_device_connection() -> None: | ||
| """Test the Device connection setup.""" | ||
|
|
||
| unsub = Mock() | ||
| subscribe = AsyncMock() | ||
| subscribe.return_value = unsub | ||
| mqtt_channel = AsyncMock() | ||
| mqtt_channel.subscribe = subscribe | ||
|
|
||
| device = RoborockDevice( | ||
| USER_DATA, | ||
| device_info=HOME_DATA.devices[0], | ||
| product_info=HOME_DATA.products[0], | ||
| mqtt_channel=mqtt_channel, | ||
| ) | ||
| assert device.duid == "abc123" | ||
| assert device.name == "Roborock S7 MaxV" | ||
| assert device.device_version == DeviceVersion.V1 | ||
|
|
||
| assert not subscribe.called | ||
|
|
||
| await device.connect() | ||
| assert subscribe.called | ||
| assert not unsub.called | ||
|
|
||
| await device.close() | ||
| assert unsub.called |
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.
Uh oh!
There was an error while loading. Please reload this page.