Skip to content

Commit 78d692e

Browse files
committed
Implement XML fan speed commands
1 parent d69ccac commit 78d692e

3 files changed

Lines changed: 42 additions & 21 deletions

File tree

deebot_client/commands/xml/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .charge_state import GetChargeState
1212
from .clean_logs import GetCleanLogs
1313
from .error import GetError
14-
from .fan_speed import GetFanSpeed
14+
from .fan_speed import GetCleanSpeed, SetCleanSpeed
1515
from .life_span import GetLifeSpan
1616
from .play_sound import PlaySound
1717
from .pos import GetPos
@@ -25,22 +25,25 @@
2525
"GetBatteryInfo",
2626
"GetChargeState",
2727
"GetCleanLogs",
28+
"GetCleanSpeed",
2829
"GetCleanSum",
2930
"GetError",
30-
"GetFanSpeed",
3131
"GetLifeSpan",
3232
"GetPos",
3333
"PlaySound",
34+
"SetCleanSpeed",
3435
]
3536

3637
# fmt: off
3738
# ordered by file asc
3839
_COMMANDS: list[type[XmlCommand]] = [
3940
GetBatteryInfo,
4041
GetCleanLogs,
42+
GetCleanSpeed,
4143
GetError,
4244
GetLifeSpan,
4345
PlaySound,
46+
SetCleanSpeed
4447
]
4548
# fmt: on
4649

deebot_client/commands/xml/fan_speed.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
from deebot_client.events import FanSpeedEvent, FanSpeedLevel
88
from deebot_client.message import HandlingResult
9+
from deebot_client.util import get_enum
910

10-
from .common import XmlCommandWithMessageHandling
11+
from .common import ExecuteCommand, XmlCommandWithMessageHandling
1112

1213
if TYPE_CHECKING:
1314
from xml.etree.ElementTree import Element
1415

1516
from deebot_client.event_bus import EventBus
1617

1718

18-
class GetFanSpeed(XmlCommandWithMessageHandling):
19-
"""GetFanSpeed command."""
19+
class GetCleanSpeed(XmlCommandWithMessageHandling):
20+
"""GetCleanSpeed command."""
2021

2122
NAME = "GetCleanSpeed"
2223

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

32-
event: FanSpeedEvent | None = None
33+
event_bus.notify(FanSpeedEvent(FanSpeedLevel.from_xml(speed)))
34+
return HandlingResult.success()
3335

34-
match speed.lower():
35-
case "standard":
36-
event = FanSpeedEvent(FanSpeedLevel.NORMAL)
37-
case "strong":
38-
event = FanSpeedEvent(FanSpeedLevel.MAX)
3936

40-
if event:
41-
event_bus.notify(event)
42-
return HandlingResult.success()
37+
class SetCleanSpeed(ExecuteCommand):
38+
"""SetCleanSpeed command."""
4339

44-
return HandlingResult.analyse()
40+
NAME = "SetCleanSpeed"
41+
42+
def __init__(self, speed: FanSpeedLevel | str) -> None:
43+
if isinstance(speed, str):
44+
speed = get_enum(FanSpeedLevel, speed)
45+
super().__init__({"speed": speed.xml_value})

tests/commands/xml/test_fan_speed.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import pytest
66

7-
from deebot_client.command import CommandResult
8-
from deebot_client.commands.xml import GetFanSpeed
7+
from deebot_client.command import CommandResult, CommandWithMessageHandling
8+
from deebot_client.commands.xml import GetCleanSpeed, SetCleanSpeed
99
from deebot_client.events import FanSpeedEvent, FanSpeedLevel
1010
from deebot_client.message import HandlingState
1111
from tests.commands import assert_command
@@ -26,19 +26,36 @@
2626
)
2727
async def test_get_fan_speed(speed: str, expected_event: Event) -> None:
2828
json = get_request_xml(f"<ctl ret='ok' speed='{speed}'/>")
29-
await assert_command(GetFanSpeed(), json, expected_event)
29+
await assert_command(GetCleanSpeed(), json, expected_event)
3030

3131

3232
@pytest.mark.parametrize(
3333
"xml",
34-
["<ctl ret='error'/>", "<ctl ret='ok' speed='invalid'/>"],
35-
ids=["error", "no_state"],
34+
["<ctl ret='error'/>"],
35+
ids=["error"],
3636
)
3737
async def test_get_fan_speed_error(xml: str) -> None:
3838
json = get_request_xml(xml)
3939
await assert_command(
40-
GetFanSpeed(),
40+
GetCleanSpeed(),
4141
json,
4242
None,
4343
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
4444
)
45+
46+
47+
@pytest.mark.parametrize(
48+
("command", "xml", "result"),
49+
[
50+
(
51+
SetCleanSpeed(FanSpeedLevel.MAX),
52+
"<ctl ret='ok' />",
53+
HandlingState.SUCCESS,
54+
),
55+
],
56+
)
57+
async def test_set_fan_speed(
58+
command: CommandWithMessageHandling, xml: str, result: HandlingState
59+
) -> None:
60+
json = get_request_xml(xml)
61+
await assert_command(command, json, None, command_result=CommandResult(result))

0 commit comments

Comments
 (0)