Skip to content

Commit

Permalink
Add feedback for Get/SetCleanSpeed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
flubshi authored and pentesttkr committed Sep 11, 2024
1 parent 366e96a commit 315307a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
8 changes: 5 additions & 3 deletions deebot_client/commands/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@

from .charge_state import GetChargeState
from .error import GetError
from .fan_speed import GetFanSpeed, SetFanSpeed
from .fan_speed import GetCleanSpeed, SetCleanSpeed
from .pos import GetPos
from .stats import GetCleanSum

if TYPE_CHECKING:
from .common import XmlCommand

__all__ = [
"GetCleanSpeed",
"SetCleanSpeed",
"GetChargeState",
"GetCleanSum",
"GetError",
"GetFanSpeed",
"SetFanSpeed",
"GetPos",
]

# fmt: off
# ordered by file asc
_COMMANDS: list[type[XmlCommand]] = [
GetCleanSpeed,
SetCleanSpeed,
GetError,
]
# fmt: on
Expand Down
18 changes: 9 additions & 9 deletions deebot_client/commands/xml/fan_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from deebot_client.event_bus import EventBus


class GetFanSpeed(XmlCommandWithMessageHandling):
"""GetFanSpeed command."""
class GetCleanSpeed(XmlCommandWithMessageHandling):
"""GetCleanSpeed command."""

name = "GetCleanSpeed"

Expand All @@ -35,9 +35,9 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:

match speed.lower():
case "standard":
event = FanSpeedEvent(FanSpeedLevel.NORMAL)
event = FanSpeedEvent(FanSpeedLevel.STANDARD)
case "strong":
event = FanSpeedEvent(FanSpeedLevel.MAX)
event = FanSpeedEvent(FanSpeedLevel.STRONG)

if event:
event_bus.notify(event)
Expand All @@ -46,15 +46,15 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
return HandlingResult.analyse()


class SetFanSpeed(XmlSetCommand):
"""Set fan speed command."""
class SetCleanSpeed(XmlSetCommand):
"""Set clean speed command."""

name = "SetCleanSpeed"
get_command = GetFanSpeed
get_command = GetCleanSpeed
_mqtt_params = MappingProxyType({"speed": InitParam(FanSpeedLevel)})

def __init__(self, speed: FanSpeedLevel | str) -> None:
if isinstance(speed, int):
speed = "strong" if speed in [1, 2] else "normal"
if isinstance(speed, FanSpeedLevel):
speed = speed.name.lower()
super().__init__({"speed": speed})

2 changes: 2 additions & 0 deletions deebot_client/events/fan_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class FanSpeedLevel(IntEnum):

# Values should be sort from low to high on their meanings
QUIET = 1000
STANDARD = -1
NORMAL = 0
MAX = 1
STRONG = -2
MAX_PLUS = 2


Expand Down
18 changes: 9 additions & 9 deletions tests/commands/xml/test_fan_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import pytest

from deebot_client.command import CommandResult
from deebot_client.commands.xml import GetFanSpeed
from deebot_client.commands.xml.fan_speed import SetFanSpeed
from deebot_client.commands.xml import GetCleanSpeed, SetCleanSpeed
from deebot_client.events import FanSpeedEvent, FanSpeedLevel
from deebot_client.message import HandlingState
from tests.commands import assert_command

from . import get_request_xml
from ..json import assert_set_command

if TYPE_CHECKING:
from deebot_client.events.base import Event
Expand All @@ -21,14 +19,14 @@
@pytest.mark.parametrize(
("speed", "expected_event"),
[
("standard", FanSpeedEvent(FanSpeedLevel.NORMAL)),
("strong", FanSpeedEvent(FanSpeedLevel.MAX)),
("standard", FanSpeedEvent(FanSpeedLevel.STANDARD)),
("strong", FanSpeedEvent(FanSpeedLevel.STRONG)),
],
ids=["standard", "strong"],
)
async def test_get_fan_speed(speed: str, expected_event: Event) -> None:
json = get_request_xml(f"<ctl ret='ok' speed='{speed}'/>")
await assert_command(GetFanSpeed(), json, expected_event)
await assert_command(GetCleanSpeed(), json, expected_event)


@pytest.mark.parametrize(
Expand All @@ -39,18 +37,20 @@ async def test_get_fan_speed(speed: str, expected_event: Event) -> None:
async def test_get_fan_speed_error(xml: str) -> None:
json = get_request_xml(xml)
await assert_command(
GetFanSpeed(),
GetCleanSpeed(),
json,
None,
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
)


async def test_set_fan_speed() -> None:
command = SetFanSpeed(FanSpeedLevel.MAX)
command = SetCleanSpeed(FanSpeedLevel.STRONG)
json = get_request_xml("<ctl ret='ok' />")
await assert_command(command, json, None, command_result=CommandResult(HandlingState.SUCCESS))


async def test_set_fan_speed_error() -> None:
command = SetFanSpeed("invalid")
command = SetCleanSpeed("invalid")
json = get_request_xml("<ctl ret='error' />")
await assert_command(command, json, None, command_result=CommandResult(HandlingState.FAILED))

0 comments on commit 315307a

Please sign in to comment.