-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Add a trait for sending commands #539
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
8 commits
Select commit
Hold shift + click to select a range
a88e961
feat: Add a trait for sending commands
allenporter cd38486
feat: Update cli to use new command interface
allenporter c32af0b
chore: fix tests
allenporter fbe7989
Update tests/devices/traits/v1/test_command.py
allenporter df23c0c
Update tests/devices/traits/v1/test_command.py
allenporter 38b6872
chore: fix syntax and lint errors
allenporter 737d14a
chore: fix lint errors
allenporter 0229700
Merge branch 'main' into commands
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from typing import Any | ||
|
|
||
| from roborock import RoborockCommand | ||
|
|
||
|
|
||
| class CommandTrait: | ||
| """Trait for sending commands to Roborock devices.""" | ||
|
|
||
| def __post_init__(self) -> None: | ||
| """Post-initialization to set up the RPC channel. | ||
|
|
||
| This is called automatically after the dataclass is initialized by the | ||
| device setup code. | ||
| """ | ||
| self._rpc_channel = None | ||
|
|
||
| async def send(self, command: RoborockCommand, params: dict[str, Any] | None = None) -> Any: | ||
| """Send a command to the device.""" | ||
| if not self._rpc_channel: | ||
| raise ValueError("Device trait in invalid state") | ||
| return await self._rpc_channel.send_command(command, params=params) |
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,61 @@ | ||
| """Tests for the CommandTrait class.""" | ||
|
|
||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from roborock.devices.traits.v1.command import CommandTrait | ||
| from roborock.exceptions import RoborockException | ||
| from roborock.roborock_typing import RoborockCommand | ||
|
|
||
|
|
||
| @pytest.fixture(name="command_trait") | ||
| def command_trait_fixture() -> CommandTrait: | ||
| """Create a CommandTrait instance with a mocked RPC channel.""" | ||
| trait = CommandTrait() | ||
| trait._rpc_channel = AsyncMock() # type: ignore[assignment] | ||
| return trait | ||
|
|
||
|
|
||
| async def test_send_command_success(command_trait: CommandTrait) -> None: | ||
| """Test successfully sending a command.""" | ||
| mock_rpc_channel = command_trait._rpc_channel | ||
| assert mock_rpc_channel is not None | ||
| mock_rpc_channel.send_command.return_value = {"result": "ok"} | ||
|
|
||
| # Call the method | ||
| result = await command_trait.send(RoborockCommand.APP_START) | ||
|
|
||
| # Verify the result | ||
| assert result == {'result': 'ok'} | ||
|
|
||
| # Verify the RPC call was made correctly | ||
| mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.APP_START, params=None) | ||
|
|
||
|
|
||
| async def test_send_command_with_params(command_trait: CommandTrait) -> None: | ||
| """Test successfully sending a command with parameters.""" | ||
| mock_rpc_channel = command_trait._rpc_channel | ||
| assert mock_rpc_channel is not None | ||
| mock_rpc_channel.send_command.return_value = {"result": "ok"} | ||
| params = {"segments": [1, 2, 3]} | ||
|
|
||
| # Call the method | ||
| result = await command_trait.send(RoborockCommand.APP_SEGMENT_CLEAN, params) | ||
|
|
||
| # Verify the result | ||
| assert result == {'result': 'ok'} | ||
|
allenporter marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Verify the RPC call was made correctly | ||
| mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.APP_SEGMENT_CLEAN, params=params) | ||
|
|
||
|
|
||
| async def test_send_command_propagates_exception(command_trait: CommandTrait) -> None: | ||
| """Test that exceptions from RPC channel are propagated.""" | ||
| mock_rpc_channel = command_trait._rpc_channel | ||
| assert mock_rpc_channel is not None | ||
| mock_rpc_channel.send_command.side_effect = RoborockException("Communication error") | ||
|
|
||
| # Verify the exception is propagated | ||
| with pytest.raises(RoborockException, match="Communication error"): | ||
| await command_trait.send(RoborockCommand.APP_START) | ||
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.