Skip to content

Commit

Permalink
add advanced mode, break point (continuous cleaning), carpet pressure…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
edenhaus authored Nov 17, 2021
1 parent d78bb34 commit ebe2d13
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 4 deletions.
15 changes: 14 additions & 1 deletion deebot_client/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -40,6 +49,9 @@

GetCleanLogs,

GetContinuousCleaning,
SetContinuousCleaning,

GetError,

GetFanSpeed,
Expand All @@ -61,6 +73,7 @@
SetRelocationState,

GetStats,
GetTotalStats,

GetVolume,
SetVolume,
Expand Down
18 changes: 18 additions & 0 deletions deebot_client/commands/advanced_mode.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions deebot_client/commands/carpet.py
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions deebot_client/commands/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
18 changes: 18 additions & 0 deletions deebot_client/commands/continuous_cleaning.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions deebot_client/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
11 changes: 10 additions & 1 deletion deebot_client/events/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@

from ..command import Command
from ..commands import (
GetAdvancedMode,
GetBattery,
GetCachedMapInfo,
GetCarpetAutoFanBoost,
GetChargeState,
GetCleanInfo,
GetCleanLogs,
GetContinuousCleaning,
GetError,
GetFanSpeed,
GetLifeSpan,
GetMajorMap,
GetMapTrace,
GetPos,
GetStats,
GetTotalStats,
GetVolume,
GetWaterInfo,
)
from ..commands.stats import GetTotalStats
from . import (
AdvancedModeEvent,
BatteryEvent,
CarpetAutoFanBoostEvent,
CleanLogEvent,
ContinuousCleaningEvent,
CustomCommandEvent,
ErrorEvent,
Event,
Expand All @@ -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()],
Expand Down
4 changes: 2 additions & 2 deletions tests/events/test_events.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ebe2d13

Please sign in to comment.