Skip to content
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
23 changes: 19 additions & 4 deletions deebot_client/commands/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from .charge import Charge
from .charge_state import GetChargeState
from .clean_logs import GetCleanLogs
from .clean_speed import GetCleanSpeed, SetCleanSpeed
from .error import GetError
from .fan_speed import GetFanSpeed
from .life_span import GetLifeSpan
from .play_sound import PlaySound
from .pos import GetChargerPos, GetPos
Expand All @@ -26,24 +26,39 @@
"GetChargeState",
"GetChargerPos",
"GetCleanLogs",
"GetCleanSpeed",
"GetCleanSum",
"GetError",
"GetFanSpeed",
"GetLifeSpan",
"GetPos",
"PlaySound",
"SetCleanSpeed",
]

# fmt: off
# ordered by file asc
_COMMANDS: list[type[XmlCommand]] = [
GetBatteryInfo,
GetChargerPos,

GetChargeState,

Charge,

GetCleanLogs,

GetCleanSpeed,
SetCleanSpeed,

GetError,

GetLifeSpan,
GetPos,

PlaySound,

GetChargerPos,
GetPos,

GetCleanSum
]
# fmt: on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

from deebot_client.events import FanSpeedEvent, FanSpeedLevel
from deebot_client.message import HandlingResult
from deebot_client.util import get_enum

from .common import XmlCommandWithMessageHandling
from .common import ExecuteCommand, XmlCommandWithMessageHandling

if TYPE_CHECKING:
from xml.etree.ElementTree import Element

from deebot_client.event_bus import EventBus


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

NAME = "GetCleanSpeed"

Expand All @@ -29,16 +30,16 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
if xml.attrib.get("ret") != "ok" or not (speed := xml.attrib.get("speed")):
return HandlingResult.analyse()

event: FanSpeedEvent | None = None
event_bus.notify(FanSpeedEvent(FanSpeedLevel.from_xml(speed)))
return HandlingResult.success()

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

if event:
event_bus.notify(event)
return HandlingResult.success()
class SetCleanSpeed(ExecuteCommand):
"""SetCleanSpeed command."""

return HandlingResult.analyse()
NAME = "SetCleanSpeed"

def __init__(self, speed: FanSpeedLevel | str) -> None:
if isinstance(speed, str):
speed = get_enum(FanSpeedLevel, speed)
super().__init__({"speed": speed.xml_value})
75 changes: 75 additions & 0 deletions tests/commands/xml/test_clean_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from deebot_client.command import CommandResult, CommandWithMessageHandling
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

if TYPE_CHECKING:
from deebot_client.events.base import Event


@pytest.mark.parametrize(
"speed", list(FanSpeedLevel) + [level.name for level in FanSpeedLevel]
)
def test_GetCleanSpeed_should_build_with_string_and_enums(
speed: str | FanSpeedLevel,
) -> None:
"""Test case for the way HA invokes the constructor."""
assert SetCleanSpeed(speed) is not None


@pytest.mark.parametrize(
("speed", "expected_event"),
[
("standard", FanSpeedEvent(FanSpeedLevel.NORMAL)),
("strong", FanSpeedEvent(FanSpeedLevel.MAX)),
],
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(GetCleanSpeed(), json, expected_event)


@pytest.mark.parametrize(
("xml", "expected_state"),
[
("<ctl ret='error'/>", HandlingState.ANALYSE_LOGGED),
("<ctl ret='ok' />", HandlingState.ANALYSE_LOGGED),
("<ctl ret='ok' speed='invalid'/>", HandlingState.ERROR),
],
ids=["error", "no_state", "invalid_speed"],
)
async def test_get_fan_speed_error(xml: str, expected_state: HandlingState) -> None:
json = get_request_xml(xml)
await assert_command(
GetCleanSpeed(),
json,
None,
command_result=CommandResult(expected_state),
)


@pytest.mark.parametrize(
("command", "xml", "result"),
[
(
SetCleanSpeed(FanSpeedLevel.MAX),
"<ctl ret='ok' />",
HandlingState.SUCCESS,
),
],
)
async def test_set_fan_speed(
command: CommandWithMessageHandling, xml: str, result: HandlingState
) -> None:
json = get_request_xml(xml)
await assert_command(command, json, None, command_result=CommandResult(result))
44 changes: 0 additions & 44 deletions tests/commands/xml/test_fan_speed.py

This file was deleted.

Loading