From ebe2d13f09a552778abb4bfadecdb2feda7ea831 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Wed, 17 Nov 2021 20:42:06 +0100 Subject: [PATCH] add advanced mode, break point (continuous cleaning), carpet pressure commands (#24) * add advanced mode commands * add advanced mode to EVENT_DTO_REFRESH_COMMANDS * create generic enabled commands * add break point (continuous cleaning) command * add carpet pressure commands * use also break point as event too * rename enable event/commands * rename break point * rename carpet pressure * rename carpet pressure --- deebot_client/commands/__init__.py | 15 ++++++- deebot_client/commands/advanced_mode.py | 18 +++++++++ deebot_client/commands/carpet.py | 18 +++++++++ deebot_client/commands/common.py | 39 +++++++++++++++++++ deebot_client/commands/continuous_cleaning.py | 18 +++++++++ deebot_client/events/__init__.py | 22 +++++++++++ deebot_client/events/const.py | 11 +++++- tests/events/test_events.py | 4 +- 8 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 deebot_client/commands/advanced_mode.py create mode 100644 deebot_client/commands/carpet.py create mode 100644 deebot_client/commands/continuous_cleaning.py diff --git a/deebot_client/commands/__init__.py b/deebot_client/commands/__init__.py index 53ac14e8..8b7dd48b 100644 --- a/deebot_client/commands/__init__.py +++ b/deebot_client/commands/__init__.py @@ -1,12 +1,15 @@ """Commands module.""" from typing import Dict, List, Type +from .advanced_mode import GetAdvancedMode, SetAdvancedMode from .battery import GetBattery +from .carpet import GetCarpetAutoFanBoost, SetCarpetAutoFanBoost from .charge import Charge from .charge_state import GetChargeState from .clean import Clean, CleanArea, GetCleanInfo from .clean_logs import GetCleanLogs from .common import CommandWithHandling, SetCommand +from .continuous_cleaning import GetContinuousCleaning, SetContinuousCleaning from .error import GetError from .fan_speed import FanSpeedLevel, GetFanSpeed, SetFanSpeed from .life_span import GetLifeSpan @@ -21,15 +24,21 @@ from .play_sound import PlaySound from .pos import GetPos from .relocation import SetRelocationState -from .stats import GetStats +from .stats import GetStats, GetTotalStats from .volume import GetVolume, SetVolume from .water_info import GetWaterInfo, SetWaterInfo # fmt: off # ordered by file asc _COMMANDS: List[Type[CommandWithHandling]] = [ + GetAdvancedMode, + SetAdvancedMode, + GetBattery, + GetCarpetAutoFanBoost, + SetCarpetAutoFanBoost, + Charge, GetChargeState, @@ -40,6 +49,9 @@ GetCleanLogs, + GetContinuousCleaning, + SetContinuousCleaning, + GetError, GetFanSpeed, @@ -61,6 +73,7 @@ SetRelocationState, GetStats, + GetTotalStats, GetVolume, SetVolume, diff --git a/deebot_client/commands/advanced_mode.py b/deebot_client/commands/advanced_mode.py new file mode 100644 index 00000000..4b20d77f --- /dev/null +++ b/deebot_client/commands/advanced_mode.py @@ -0,0 +1,18 @@ +"""Advanced mode command module.""" + +from ..events import AdvancedModeEvent +from .common import SetEnableCommand, _GetEnableCommand + + +class GetAdvancedMode(_GetEnableCommand): + """Get advanced mode command.""" + + name = "getAdvancedMode" + event_type = AdvancedModeEvent + + +class SetAdvancedMode(SetEnableCommand): + """Set advanced mode command.""" + + name = "setAdvancedMode" + get_command = GetAdvancedMode diff --git a/deebot_client/commands/carpet.py b/deebot_client/commands/carpet.py new file mode 100644 index 00000000..7c6ad4cd --- /dev/null +++ b/deebot_client/commands/carpet.py @@ -0,0 +1,18 @@ +"""Carpet pressure command module.""" + +from ..events import CarpetAutoFanBoostEvent +from .common import SetEnableCommand, _GetEnableCommand + + +class GetCarpetAutoFanBoost(_GetEnableCommand): + """Get carpet auto fan boost command.""" + + name = "getCarpertPressure" + event_type = CarpetAutoFanBoostEvent + + +class SetCarpetAutoFanBoost(SetEnableCommand): + """Set carpet auto fan boost command.""" + + name = "setCarpertPressure" + get_command = GetCarpetAutoFanBoost diff --git a/deebot_client/commands/common.py b/deebot_client/commands/common.py index 168e87d0..305c04cf 100644 --- a/deebot_client/commands/common.py +++ b/deebot_client/commands/common.py @@ -4,6 +4,7 @@ from typing import Any, Dict, List, Mapping, Optional, Type, Union from ..command import Command +from ..events import EnableEvent from ..events.event_bus import EventBus from ..logging_filter import get_logger from ..message import HandlingResult, HandlingState, Message @@ -106,3 +107,41 @@ def __init__( def get_command(self) -> Type[CommandWithHandling]: """Return the corresponding "get" command.""" raise NotImplementedError + + +class _GetEnableCommand(_NoArgsCommand): + """Abstract get enable command.""" + + # required as name is class variable, will be overwritten in subclasses + name = "__invalid__" + + @classmethod + @property + @abstractmethod + def event_type(cls) -> Type[EnableEvent]: + """Event type.""" + raise NotImplementedError + + @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: EnableEvent = cls.event_type(bool(data["enable"])) # type: ignore + event_bus.notify(event) + return HandlingResult.success() + + +class SetEnableCommand(SetCommand): + """Abstract set enable command.""" + + # required as name is class variable, will be overwritten in subclasses + name = "__invalid__" + + def __init__(self, enable: Union[int, bool], **kwargs: Mapping[str, Any]) -> None: + if isinstance(enable, bool): + enable = 1 if enable else 0 + super().__init__({"enable": enable}, **kwargs) diff --git a/deebot_client/commands/continuous_cleaning.py b/deebot_client/commands/continuous_cleaning.py new file mode 100644 index 00000000..5bd7b5fd --- /dev/null +++ b/deebot_client/commands/continuous_cleaning.py @@ -0,0 +1,18 @@ +"""Continuous cleaning (break point) command module.""" + +from ..events import ContinuousCleaningEvent +from .common import SetEnableCommand, _GetEnableCommand + + +class GetContinuousCleaning(_GetEnableCommand): + """Get continuous cleaning command.""" + + name = "getBreakPoint" + event_type = ContinuousCleaningEvent + + +class SetContinuousCleaning(SetEnableCommand): + """Set continuous cleaning command.""" + + name = "setBreakPoint" + get_command = GetContinuousCleaning diff --git a/deebot_client/events/__init__.py b/deebot_client/events/__init__.py index 934acd26..586df5e7 100644 --- a/deebot_client/events/__init__.py +++ b/deebot_client/events/__init__.py @@ -148,3 +148,25 @@ class VolumeEvent(Event): volume: int maximum: Optional[int] + + +@dataclass(frozen=True) +class EnableEvent(Event): + """Enabled event.""" + + enable: bool + + +@dataclass(frozen=True) +class AdvancedModeEvent(EnableEvent): + """Advanced mode event.""" + + +@dataclass(frozen=True) +class ContinuousCleaningEvent(EnableEvent): + """Continuous cleaning event.""" + + +@dataclass(frozen=True) +class CarpetAutoFanBoostEvent(EnableEvent): + """Carpet pressure event.""" diff --git a/deebot_client/events/const.py b/deebot_client/events/const.py index 5a7e7942..21ef6dcf 100644 --- a/deebot_client/events/const.py +++ b/deebot_client/events/const.py @@ -3,11 +3,14 @@ from ..command import Command from ..commands import ( + GetAdvancedMode, GetBattery, GetCachedMapInfo, + GetCarpetAutoFanBoost, GetChargeState, GetCleanInfo, GetCleanLogs, + GetContinuousCleaning, GetError, GetFanSpeed, GetLifeSpan, @@ -15,13 +18,16 @@ GetMapTrace, GetPos, GetStats, + GetTotalStats, GetVolume, GetWaterInfo, ) -from ..commands.stats import GetTotalStats from . import ( + AdvancedModeEvent, BatteryEvent, + CarpetAutoFanBoostEvent, CleanLogEvent, + ContinuousCleaningEvent, CustomCommandEvent, ErrorEvent, Event, @@ -40,8 +46,11 @@ from .map import MajorMapEvent, MapSetEvent, MapTraceEvent, MinorMapEvent EVENT_DTO_REFRESH_COMMANDS: Mapping[Type[Event], List[Command]] = { + AdvancedModeEvent: [GetAdvancedMode()], BatteryEvent: [GetBattery()], + CarpetAutoFanBoostEvent: [GetCarpetAutoFanBoost()], CleanLogEvent: [GetCleanLogs()], + ContinuousCleaningEvent: [GetContinuousCleaning()], CustomCommandEvent: [], ErrorEvent: [GetError()], FanSpeedEvent: [GetFanSpeed()], diff --git a/tests/events/test_events.py b/tests/events/test_events.py index 627a37d8..142cf7ff 100644 --- a/tests/events/test_events.py +++ b/tests/events/test_events.py @@ -1,11 +1,11 @@ import inspect import deebot_client.events -from deebot_client.events import Event +from deebot_client.events import EnableEvent, Event from deebot_client.events.const import EVENT_DTO_REFRESH_COMMANDS def test_events_has_refresh_function(): for name, obj in inspect.getmembers(deebot_client.events, inspect.isclass): - if issubclass(obj, Event) and obj != Event: + if issubclass(obj, Event) and obj not in [Event, EnableEvent]: assert obj in EVENT_DTO_REFRESH_COMMANDS