Skip to content

Commit

Permalink
Add cut direction for lawn mower goat g1 (DeebotUniverse#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
Augar authored May 29, 2024
1 parent 09b6e11 commit d12ef19
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deebot_client/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ContinuousCleaningEvent,
CrossMapBorderWarningEvent,
CustomCommandEvent,
CutDirectionEvent,
ErrorEvent,
Event,
FanSpeedEvent,
Expand Down Expand Up @@ -189,6 +190,7 @@ class CapabilitySettings:
)
border_switch: CapabilitySetEnable[BorderSwitchEvent] | None = None
child_lock: CapabilitySetEnable[ChildLockEvent] | None = None
cut_direction: CapabilitySet[CutDirectionEvent, int] | None = None
moveup_warning: CapabilitySetEnable[MoveUpWarningEvent] | None = None
cross_map_border_warning: CapabilitySetEnable[CrossMapBorderWarningEvent] | None = (
None
Expand Down
3 changes: 3 additions & 0 deletions deebot_client/commands/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .clear_map import ClearMap
from .continuous_cleaning import GetContinuousCleaning, SetContinuousCleaning
from .cross_map_border_warning import GetCrossMapBorderWarning, SetCrossMapBorderWarning
from .cut_direction import GetCutDirection, SetCutDirection
from .efficiency import GetEfficiencyMode, SetEfficiencyMode
from .error import GetError
from .fan_speed import GetFanSpeed, SetFanSpeed
Expand Down Expand Up @@ -77,6 +78,8 @@
"GetCleanLogs",
"GetContinuousCleaning",
"SetContinuousCleaning",
"SetCutDirection",
"GetCutDirection",
"GetCrossMapBorderWarning",
"SetCrossMapBorderWarning",
"GetEfficiencyMode",
Expand Down
43 changes: 43 additions & 0 deletions deebot_client/commands/json/cut_direction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Cut direction command module."""

from __future__ import annotations

from types import MappingProxyType
from typing import TYPE_CHECKING, Any

from deebot_client.command import InitParam
from deebot_client.events import CutDirectionEvent
from deebot_client.message import HandlingResult

from .common import JsonGetCommand, JsonSetCommand

if TYPE_CHECKING:
from deebot_client.event_bus import EventBus


class GetCutDirection(JsonGetCommand):
"""Get cut direction command."""

name = "getCutDirection"

@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(CutDirectionEvent(angle=data["angle"]))
return HandlingResult.success()


class SetCutDirection(JsonSetCommand):
"""Set cut direction command."""

name = "setCutDirection"
get_command = GetCutDirection
_mqtt_params = MappingProxyType({"angle": InitParam(int)})

def __init__(self, angle: int) -> None:
super().__init__({"angle": angle})
7 changes: 7 additions & 0 deletions deebot_client/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,10 @@ class MoveUpWarningEvent(EnableEvent):
@dataclass(frozen=True)
class SafeProtectEvent(EnableEvent):
"""Safe protect event."""


@dataclass(frozen=True)
class CutDirectionEvent(Event):
"""Cut direction event representation."""

angle: int
6 changes: 6 additions & 0 deletions deebot_client/hardware/deebot/5xu9h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
GetBorderSwitch,
GetChildLock,
GetCrossMapBorderWarning,
GetCutDirection,
GetMoveUpWarning,
GetSafeProtect,
SetBorderSwitch,
SetChildLock,
SetCrossMapBorderWarning,
SetCutDirection,
SetMoveUpWarning,
SetSafeProtect,
)
Expand All @@ -49,6 +51,7 @@
ChildLockEvent,
CrossMapBorderWarningEvent,
CustomCommandEvent,
CutDirectionEvent,
ErrorEvent,
LifeSpan,
LifeSpanEvent,
Expand Down Expand Up @@ -104,6 +107,9 @@
border_switch=CapabilitySetEnable(
BorderSwitchEvent, [GetBorderSwitch()], SetBorderSwitch
),
cut_direction=CapabilitySet(
CutDirectionEvent, [GetCutDirection()], SetCutDirection
),
child_lock=CapabilitySetEnable(
ChildLockEvent, [GetChildLock()], SetChildLock
),
Expand Down
20 changes: 20 additions & 0 deletions tests/commands/json/test_cut_direction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from __future__ import annotations

import pytest

from deebot_client.commands.json import GetCutDirection, SetCutDirection
from deebot_client.events import CutDirectionEvent
from tests.helpers import get_request_json, get_success_body

from . import assert_command, assert_set_command


async def test_GetCutDirection() -> None:
json = get_request_json(get_success_body({"angle": 90}))
await assert_command(GetCutDirection(), json, CutDirectionEvent(90))


@pytest.mark.parametrize("angle", [1, 45, 90])
async def test_SetCutDirection(angle: int) -> None:
args = {"angle": angle}
await assert_set_command(SetCutDirection(angle), args, CutDirectionEvent(angle))
4 changes: 4 additions & 0 deletions tests/hardware/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest

from deebot_client.commands.json import GetCutDirection
from deebot_client.commands.json.advanced_mode import GetAdvancedMode
from deebot_client.commands.json.battery import GetBattery
from deebot_client.commands.json.border_switch import GetBorderSwitch
Expand Down Expand Up @@ -49,6 +50,7 @@
ContinuousCleaningEvent,
CrossMapBorderWarningEvent,
CustomCommandEvent,
CutDirectionEvent,
ErrorEvent,
LifeSpan,
LifeSpanEvent,
Expand Down Expand Up @@ -147,6 +149,7 @@ async def test_get_static_device_info(
AvailabilityEvent: [GetBattery(is_available_check=True)],
BatteryEvent: [GetBattery()],
BorderSwitchEvent: [GetBorderSwitch()],
CutDirectionEvent: [GetCutDirection()],
ChildLockEvent: [GetChildLock()],
CrossMapBorderWarningEvent: [GetCrossMapBorderWarning()],
CustomCommandEvent: [],
Expand All @@ -170,6 +173,7 @@ async def test_get_static_device_info(
AvailabilityEvent: [GetBattery(is_available_check=True)],
BatteryEvent: [GetBattery()],
BorderSwitchEvent: [GetBorderSwitch()],
CutDirectionEvent: [GetCutDirection()],
ChildLockEvent: [GetChildLock()],
CrossMapBorderWarningEvent: [GetCrossMapBorderWarning()],
CustomCommandEvent: [],
Expand Down

0 comments on commit d12ef19

Please sign in to comment.