-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add new command: "AutoEmpty" #349
Closed
MVladislav
wants to merge
8
commits into
DeebotUniverse:dev
from
MVladislav:feature/CommandAutoEmpty
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
53e0894
feat: add command AutoEmpty
MVladislav e68d406
refactor: change/update for renamed commands
MVladislav 9470e0a
Combine SetAutoEmpty and SetAutoEmptyMode
edenhaus 918d538
feat: add test
MVladislav 9fe99e2
Merge branch 'DeebotUniverse:dev' into feature/CommandAutoEmpty
MVladislav d72023a
fix(#0): command auto empty fix lint
MVladislav ca0121b
refactor: command auto empty improve test case
MVladislav ee267ba
refactor: extend bot (p95mgv) with auto empty command
MVladislav 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 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 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 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,55 @@ | ||
"""Auto empty command module.""" | ||
from types import MappingProxyType | ||
from typing import Any | ||
|
||
from deebot_client.command import InitParam | ||
from deebot_client.event_bus import EventBus | ||
from deebot_client.events import AutoEmptyMode, AutoEmptyModeEvent | ||
from deebot_client.message import HandlingResult, MessageBodyDataDict | ||
|
||
from .common import JsonCommandWithMessageHandling, JsonSetCommand | ||
|
||
|
||
class GetAutoEmpty(JsonCommandWithMessageHandling, MessageBodyDataDict): | ||
"""Get auto empty command.""" | ||
|
||
name = "getAutoEmpty" | ||
|
||
@classmethod | ||
def _handle_body_data_dict( | ||
cls, event_bus: EventBus, data: dict[str, Any] | ||
) -> HandlingResult: | ||
"""Handle message->body->data and notify the correct event subscribers. | ||
|
||
:return: A message response | ||
""" | ||
event_bus.notify( | ||
AutoEmptyModeEvent( | ||
enable=bool(data["enable"]), | ||
mode=AutoEmptyMode(str(data["frequency"])), | ||
) | ||
) | ||
return HandlingResult.success() | ||
|
||
|
||
class SetAutoEmpty(JsonSetCommand): | ||
"""Set auto empty command.""" | ||
|
||
name = "setAutoEmpty" | ||
get_command = GetAutoEmpty | ||
_mqtt_params = MappingProxyType( | ||
{"enable": InitParam(bool), "frequency": InitParam(AutoEmptyMode)} | ||
) | ||
|
||
def __init__( | ||
self, | ||
enable: bool = True, # noqa: FBT001, FBT002 | ||
frequency: AutoEmptyMode | str | None = None, | ||
) -> None: | ||
args: dict[str, int | str] = {"enable": int(enable)} | ||
if frequency is not None: | ||
if isinstance(frequency, str): | ||
frequency = AutoEmptyMode.get(frequency) | ||
args["frequency"] = frequency.value | ||
|
||
super().__init__(args) |
This file contains 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 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,24 @@ | ||
"""Auto empty event module.""" | ||
from dataclasses import dataclass | ||
|
||
from deebot_client.util import DisplayNameStrEnum | ||
|
||
from .base import Event | ||
|
||
|
||
class AutoEmptyMode(DisplayNameStrEnum): | ||
"""Enum class for all possible auto emptys.""" | ||
|
||
MODE_10 = "10" | ||
MODE_15 = "15" | ||
MODE_25 = "25" | ||
MODE_AUTO = "auto" | ||
MODE_SMART = "smart" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class AutoEmptyModeEvent(Event): | ||
"""Auto empty event representation.""" | ||
|
||
enable: bool | ||
mode: AutoEmptyMode |
This file contains 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 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 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,135 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from deebot_client.commands.json import GetAutoEmpty, SetAutoEmpty | ||
from deebot_client.events import AutoEmptyMode, AutoEmptyModeEvent | ||
from tests.helpers import ( | ||
get_request_json, | ||
get_success_body, | ||
verify_DisplayNameStrEnum_unique, | ||
) | ||
|
||
from . import assert_command, assert_set_command | ||
|
||
|
||
def test_WorkMode_unique() -> None: | ||
verify_DisplayNameStrEnum_unique(AutoEmptyMode) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("json", "expected"), | ||
[ | ||
( | ||
{"enable": 1, "frequency": "10"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_10), | ||
), | ||
( | ||
{"enable": 1, "frequency": "15"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_15), | ||
), | ||
( | ||
{"enable": 1, "frequency": "25"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_25), | ||
), | ||
( | ||
{"enable": 0, "frequency": "25"}, | ||
AutoEmptyModeEvent(enable=False, mode=AutoEmptyMode.MODE_25), | ||
), | ||
( | ||
{"enable": 1, "frequency": "auto"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_AUTO), | ||
), | ||
( | ||
{"enable": 1, "frequency": "smart"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_SMART), | ||
), | ||
], | ||
) | ||
async def test_GetAutoEmpty(json: dict[str, Any], expected: AutoEmptyModeEvent) -> None: | ||
json = get_request_json(get_success_body(json)) | ||
await assert_command(GetAutoEmpty(), json, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("value", "args", "expected"), | ||
[ | ||
( | ||
(True, AutoEmptyMode.MODE_10), | ||
{"enable": 1, "frequency": "10"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_10), | ||
), | ||
( | ||
(True, "mode_smart"), | ||
{"enable": 1, "frequency": "smart"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_SMART), | ||
), | ||
# NOTE: this test is also working, as 'enable' will set auto to 'true' if not provided | ||
# as 'enable' is required when set a 'frequency' | ||
( | ||
(None, AutoEmptyMode.MODE_25), | ||
{"enable": 1, "frequency": "25"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_25), | ||
), | ||
# NOTE: it should be possible to only send 'True' for turn on without 'frequency', | ||
# but not sure how to implement the test correct | ||
( | ||
(True, AutoEmptyMode.MODE_AUTO), | ||
{"enable": 1, "frequency": "auto"}, | ||
AutoEmptyModeEvent(enable=True, mode=AutoEmptyMode.MODE_AUTO), | ||
), | ||
# NOTE: it should be possible to only send 'False' for turn off without 'frequency', | ||
# but not sure how to implement the test correct | ||
( | ||
(False, AutoEmptyMode.MODE_AUTO), | ||
{"enable": 0, "frequency": "auto"}, | ||
AutoEmptyModeEvent(enable=False, mode=AutoEmptyMode.MODE_AUTO), | ||
), | ||
], | ||
) | ||
async def test_SetAutoEmpty( | ||
value: tuple[bool | None, AutoEmptyMode | str | None], | ||
args: dict[str, Any], | ||
expected: AutoEmptyModeEvent, | ||
) -> None: | ||
command = SetAutoEmpty() | ||
if value[0] is None and value[1] is not None: | ||
command = SetAutoEmpty(frequency=value[1]) | ||
elif value[1] is None and value[0] is not None: | ||
command = SetAutoEmpty(enable=value[0]) | ||
elif value[0] is not None and value[1] is not None: | ||
command = SetAutoEmpty(value[0], value[1]) | ||
|
||
await assert_set_command(command, args, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("value", "args", "expected"), | ||
[ | ||
( | ||
(None, AutoEmptyMode.MODE_AUTO), | ||
{"enable": 0, "frequency": "auto"}, | ||
AutoEmptyModeEvent(enable=False, mode=AutoEmptyMode.MODE_AUTO), | ||
), | ||
( | ||
(None, None), | ||
{"enable": 0, "frequency": "auto"}, | ||
AutoEmptyModeEvent(enable=False, mode=AutoEmptyMode.MODE_AUTO), | ||
), | ||
], | ||
) | ||
async def test_SetAutoEmptyFail( | ||
value: tuple[bool | None, AutoEmptyMode | str | None], | ||
args: dict[str, Any], | ||
expected: AutoEmptyModeEvent, | ||
) -> None: | ||
command = SetAutoEmpty() | ||
if value[0] is None and value[1] is not None: | ||
command = SetAutoEmpty(frequency=value[1]) | ||
elif value[1] is None and value[0] is not None: | ||
command = SetAutoEmpty(enable=value[0]) | ||
elif value[0] is not None and value[1] is not None: | ||
command = SetAutoEmpty(value[0], value[1]) | ||
|
||
with pytest.raises(AssertionError): | ||
await assert_set_command(command, args, expected) |
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.
Why are you adding this enum back?
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.
Because inside the event auto_empty.py in class AutoEmptyMod i used instead of integer enums - string enums. For that I copy the existing int enum definition to have it with same functions for string enum.