Skip to content

Commit

Permalink
Add 'Mop-Only' commands and event (#388)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Resch <[email protected]>
  • Loading branch information
mrbungle64 and edenhaus authored Feb 1, 2024
1 parent ad9bd6f commit ed59c71
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 4 deletions.
2 changes: 2 additions & 0 deletions deebot_client/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
RoomsEvent,
StateEvent,
StatsEvent,
SweepModeEvent,
TotalStatsEvent,
TrueDetectEvent,
VoiceAssistantStateEvent,
Expand Down Expand Up @@ -177,6 +178,7 @@ class CapabilitySettings:
efficiency_mode: (
CapabilitySetTypes[EfficiencyModeEvent, EfficiencyMode] | None
) = None
sweep_mode: CapabilitySetEnable[SweepModeEvent] | None = None
true_detect: CapabilitySetEnable[TrueDetectEvent] | None = None
voice_assistant: CapabilitySetEnable[VoiceAssistantStateEvent] | None = None
volume: CapabilitySet[VolumeEvent, int]
Expand Down
6 changes: 6 additions & 0 deletions deebot_client/commands/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .pos import GetPos
from .relocation import SetRelocationState
from .stats import GetStats, GetTotalStats
from .sweep_mode import GetSweepMode, SetSweepMode
from .true_detect import GetTrueDetect, SetTrueDetect
from .voice_assistant_state import GetVoiceAssistantState, SetVoiceAssistantState
from .volume import GetVolume, SetVolume
Expand Down Expand Up @@ -80,6 +81,8 @@
"GetPos",
"SetRelocationState",
"GetStats",
"GetSweepMode",
"SetSweepMode",
"GetTotalStats",
"GetTrueDetect",
"SetTrueDetect",
Expand Down Expand Up @@ -152,6 +155,9 @@

SetRelocationState,

GetSweepMode,
SetSweepMode,

GetStats,
GetTotalStats,

Expand Down
15 changes: 12 additions & 3 deletions deebot_client/commands/json/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def handle_set_args(
class GetEnableCommand(JsonGetCommand, ABC):
"""Abstract get enable command."""

_field_name: str = "enable"

@property # type: ignore[misc]
@classmethod
@abstractmethod
Expand All @@ -112,15 +114,22 @@ def _handle_body_data_dict(
:return: A message response
"""
event: EnableEvent = cls.event_type(bool(data["enable"])) # type: ignore[call-arg, assignment]
event: EnableEvent = cls.event_type(bool(data[cls._field_name])) # type: ignore[call-arg, assignment]
event_bus.notify(event)
return HandlingResult.success()


_ENABLE = "enable"


class SetEnableCommand(JsonSetCommand, ABC):
"""Abstract set enable command."""

_mqtt_params = MappingProxyType({"enable": InitParam(bool)})
_field_name = _ENABLE

def __init_subclass__(cls, **kwargs: Any) -> None:
cls._mqtt_params = MappingProxyType({cls._field_name: InitParam(bool, _ENABLE)})
super().__init_subclass__(**kwargs)

def __init__(self, enable: bool) -> None: # noqa: FBT001
super().__init__({"enable": 1 if enable else 0})
super().__init__({self._field_name: 1 if enable else 0})
22 changes: 22 additions & 0 deletions deebot_client/commands/json/sweep_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""SweepMode command module for "Mop-Only" option."""
from __future__ import annotations

from deebot_client.events import SweepModeEvent

from .common import GetEnableCommand, SetEnableCommand


class GetSweepMode(GetEnableCommand):
"""GetSweepMode command."""

name = "getSweepMode"
event_type = SweepModeEvent
_field_name = "type"


class SetSweepMode(SetEnableCommand):
"""SetSweepMode command."""

name = "setSweepMode"
get_command = GetSweepMode
_field_name = "type"
6 changes: 6 additions & 0 deletions deebot_client/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Position",
"PositionType",
"PositionsEvent",
"SweepModeEvent",
"WaterAmount",
"WaterInfoEvent",
"WorkMode",
Expand Down Expand Up @@ -238,3 +239,8 @@ class TrueDetectEvent(EnableEvent):
@dataclass(frozen=True)
class VoiceAssistantStateEvent(EnableEvent):
"""VoiceAssistantState event."""


@dataclass(frozen=True)
class SweepModeEvent(EnableEvent):
"""SweepMode event ("Mop-Only" option)."""
5 changes: 5 additions & 0 deletions deebot_client/hardware/deebot/2o4lnm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from deebot_client.commands.json.pos import GetPos
from deebot_client.commands.json.relocation import SetRelocationState
from deebot_client.commands.json.stats import GetStats, GetTotalStats
from deebot_client.commands.json.sweep_mode import GetSweepMode, SetSweepMode
from deebot_client.commands.json.true_detect import GetTrueDetect, SetTrueDetect
from deebot_client.commands.json.voice_assistant_state import (
GetVoiceAssistantState,
Expand Down Expand Up @@ -83,6 +84,7 @@
RoomsEvent,
StateEvent,
StatsEvent,
SweepModeEvent,
TotalStatsEvent,
TrueDetectEvent,
VoiceAssistantStateEvent,
Expand Down Expand Up @@ -176,6 +178,9 @@
[GetCarpetAutoFanBoost()],
SetCarpetAutoFanBoost,
),
sweep_mode=CapabilitySetEnable(
SweepModeEvent, [GetSweepMode()], SetSweepMode
),
true_detect=CapabilitySetEnable(
TrueDetectEvent, [GetTrueDetect()], SetTrueDetect
),
Expand Down
3 changes: 2 additions & 1 deletion tests/commands/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async def assert_set_enable_command(
expected_get_command_event: type[EnableEvent],
*,
enabled: bool,
field_name: str = "enable",
) -> None:
args = {"enable": 1 if enabled else 0}
args = {field_name: 1 if enabled else 0}
await assert_set_command(command, args, expected_get_command_event(enabled))
22 changes: 22 additions & 0 deletions tests/commands/json/test_sweep_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import pytest

from deebot_client.commands.json import GetSweepMode, SetSweepMode
from deebot_client.events import SweepModeEvent
from tests.helpers import get_request_json, get_success_body

from . import assert_command, assert_set_enable_command


@pytest.mark.parametrize("value", [False, True])
async def test_GetSweepMode(*, value: bool) -> None:
json = get_request_json(get_success_body({"type": 1 if value else 0}))
await assert_command(GetSweepMode(), json, SweepModeEvent(value))


@pytest.mark.parametrize("value", [False, True])
async def test_SetSweepMode(*, value: bool) -> None:
await assert_set_enable_command(
SetSweepMode(value), SweepModeEvent, enabled=value, field_name="type"
)

0 comments on commit ed59c71

Please sign in to comment.