Skip to content
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: "Efficiency" #346

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions deebot_client/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
WorkMode,
WorkModeEvent,
)
from deebot_client.events.efficiency_mode import EfficiencyMode, EfficiencyModeEvent
from deebot_client.models import CleanAction, CleanMode

if TYPE_CHECKING:
Expand Down Expand Up @@ -168,6 +169,9 @@ class CapabilitySettings:

advanced_mode: CapabilitySetEnable[AdvancedModeEvent]
carpet_auto_fan_boost: CapabilitySetEnable[CarpetAutoFanBoostEvent]
efficiency_mode: (
CapabilitySetTypes[EfficiencyModeEvent, EfficiencyMode] | None
) = None
true_detect: CapabilitySetEnable[TrueDetectEvent] | 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 @@ -12,6 +12,7 @@
from .clean_preference import GetCleanPreference, SetCleanPreference
from .common import JsonCommand
from .continuous_cleaning import GetContinuousCleaning, SetContinuousCleaning
from .efficiency import GetEfficiencyMode, SetEfficiencyMode
from .error import GetError
from .fan_speed import GetFanSpeed, SetFanSpeed
from .life_span import GetLifeSpan, ResetLifeSpan
Expand Down Expand Up @@ -52,6 +53,8 @@
"GetCleanLogs",
"GetContinuousCleaning",
"SetContinuousCleaning",
"GetEfficiencyMode",
"SetEfficiencyMode",
"GetError",
"GetFanSpeed",
"SetFanSpeed",
Expand Down Expand Up @@ -111,6 +114,9 @@
GetContinuousCleaning,
SetContinuousCleaning,

GetEfficiencyMode,
SetEfficiencyMode,

GetError,

GetFanSpeed,
Expand Down
39 changes: 39 additions & 0 deletions deebot_client/commands/json/efficiency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Efficiency mode command module."""
from typing import Any

from deebot_client.command import InitParam
from deebot_client.event_bus import EventBus
from deebot_client.events import EfficiencyMode, EfficiencyModeEvent
from deebot_client.message import HandlingResult, MessageBodyDataDict

from .common import JsonCommandWithMessageHandling, JsonSetCommand


class GetEfficiencyMode(JsonCommandWithMessageHandling, MessageBodyDataDict):
"""Get efficiency mode command."""

name = "getEfficiency"

@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(EfficiencyModeEvent(EfficiencyMode(int(data["efficiency"]))))
return HandlingResult.success()


class SetEfficiencyMode(JsonSetCommand):
"""Set efficiency mode command."""

name = "setEfficiency"
get_command = GetEfficiencyMode
_mqtt_params = {"efficiency": InitParam(EfficiencyMode)}

def __init__(self, efficiency: EfficiencyMode | str) -> None:
if isinstance(efficiency, str):
efficiency = EfficiencyMode.get(efficiency)
super().__init__({"efficiency": efficiency.value})
3 changes: 3 additions & 0 deletions deebot_client/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from deebot_client.models import Room, State
from deebot_client.util import DisplayNameIntEnum

from .efficiency_mode import EfficiencyMode, EfficiencyModeEvent
from .fan_speed import FanSpeedEvent, FanSpeedLevel
from .map import (
CachedMapInfoEvent,
Expand All @@ -31,6 +32,8 @@
"CachedMapInfoEvent",
"CleanJobStatus",
"CleanLogEntry",
"EfficiencyMode",
"EfficiencyModeEvent",
"Event",
"FanSpeedEvent",
"FanSpeedLevel",
Expand Down
20 changes: 20 additions & 0 deletions deebot_client/events/efficiency_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Efficiency mode event module."""
from dataclasses import dataclass

from deebot_client.util import DisplayNameIntEnum

from .base import Event


class EfficiencyMode(DisplayNameIntEnum):
"""Enum class for all possible efficiency modes."""

STANDART_MODE = 0
ENERGY_EFFICIENT_MODE = 1


@dataclass(frozen=True)
class EfficiencyModeEvent(Event):
"""Efficiency mode event representation."""

efficiency: EfficiencyMode
40 changes: 40 additions & 0 deletions tests/commands/json/test_efficiency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Any

import pytest

from deebot_client.commands.json import GetEfficiencyMode, SetEfficiencyMode
from deebot_client.events import EfficiencyMode, EfficiencyModeEvent
from tests.helpers import (
get_request_json,
get_success_body,
verify_DisplayNameEnum_unique,
)

from . import assert_command, assert_set_command


def test_WorkMode_unique() -> None:
verify_DisplayNameEnum_unique(EfficiencyMode)


@pytest.mark.parametrize(
("json", "expected"),
[
({"efficiency": 0}, EfficiencyModeEvent(EfficiencyMode.STANDART_MODE)),
({"efficiency": 1}, EfficiencyModeEvent(EfficiencyMode.ENERGY_EFFICIENT_MODE)),
],
)
async def test_GetEfficiencyMode(
json: dict[str, Any], expected: EfficiencyModeEvent
) -> None:
json = get_request_json(get_success_body(json))
await assert_command(GetEfficiencyMode(), json, expected)


@pytest.mark.parametrize(("value"), [EfficiencyMode.STANDART_MODE, "standart_mode"])
async def test_SetEfficiencyMode(value: EfficiencyMode | str) -> None:
command = SetEfficiencyMode(value)
args = {"efficiency": 0}
await assert_set_command(
command, args, EfficiencyModeEvent(EfficiencyMode.STANDART_MODE)
)