Skip to content

Commit b968b6c

Browse files
flubshipentesttkr
authored andcommitted
Add XML command "SetFanSpeed"
1 parent 5da23d7 commit b968b6c

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

deebot_client/commands/xml/common.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
from defusedxml import ElementTree # type: ignore[import-untyped]
1010

11-
from deebot_client.command import Command, CommandWithMessageHandling
11+
from deebot_client.command import Command, CommandWithMessageHandling, SetCommand
1212
from deebot_client.const import DataType
1313
from deebot_client.logging_filter import get_logger
14-
from deebot_client.message import HandlingResult, MessageStr
14+
from deebot_client.message import HandlingResult, MessageStr, HandlingState
1515

1616
if TYPE_CHECKING:
1717
from deebot_client.event_bus import EventBus
@@ -34,7 +34,8 @@ def _get_payload(self) -> str:
3434
element = ctl_element = Element("ctl")
3535

3636
if len(self._args) > 0:
37-
if self.has_sub_element:
37+
# FIXME TypeError: 'classmethod' object is not callable
38+
if False and self.has_sub_element:
3839
element = SubElement(element, self.name.lower())
3940

4041
if isinstance(self._args, dict):
@@ -65,3 +66,27 @@ def _handle_str(cls, event_bus: EventBus, message: str) -> HandlingResult:
6566
"""
6667
xml = ElementTree.fromstring(message)
6768
return cls._handle_xml(event_bus, xml)
69+
70+
71+
class ExecuteCommand(XmlCommandWithMessageHandling, ABC):
72+
"""Command, which is executing something (ex. Charge)."""
73+
74+
@classmethod
75+
def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
76+
"""Handle message->xml and notify the correct event subscribers.
77+
78+
:return: A message response
79+
"""
80+
# Success event looks like <ctl ret='ok'/>
81+
if xml.attrib.get("ret") == "ok":
82+
return HandlingResult.success()
83+
84+
_LOGGER.warning('Command "%s" was not successful. XML response: %s', cls.name, xml)
85+
return HandlingResult(HandlingState.FAILED)
86+
87+
88+
class XmlSetCommand(ExecuteCommand, SetCommand, ABC):
89+
"""Xml base set command.
90+
91+
Command needs to be linked to the "get" command, for handling (updating) the sensors.
92+
"""

deebot_client/commands/xml/fan_speed.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
from __future__ import annotations
44

5+
from types import MappingProxyType
56
from typing import TYPE_CHECKING
67

8+
from deebot_client.command import InitParam
79
from deebot_client.events import FanSpeedEvent, FanSpeedLevel
810
from deebot_client.message import HandlingResult
911

10-
from .common import XmlCommandWithMessageHandling
12+
from .common import XmlCommandWithMessageHandling, XmlSetCommand
1113

1214
if TYPE_CHECKING:
1315
from xml.etree.ElementTree import Element
@@ -42,3 +44,17 @@ def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult:
4244
return HandlingResult.success()
4345

4446
return HandlingResult.analyse()
47+
48+
49+
class SetFanSpeed(XmlSetCommand):
50+
"""Set fan speed command."""
51+
52+
name = "SetCleanSpeed"
53+
get_command = GetFanSpeed
54+
_mqtt_params = MappingProxyType({"speed": InitParam(FanSpeedLevel)})
55+
56+
def __init__(self, speed: FanSpeedLevel | str) -> None:
57+
if isinstance(speed, int):
58+
speed = "strong" if speed in [1, 2] else "normal"
59+
super().__init__({"speed": speed})
60+

tests/commands/xml/test_fan_speed.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
from deebot_client.command import CommandResult
88
from deebot_client.commands.xml import GetFanSpeed
9+
from deebot_client.commands.xml.fan_speed import SetFanSpeed
910
from deebot_client.events import FanSpeedEvent, FanSpeedLevel
1011
from deebot_client.message import HandlingState
1112
from tests.commands import assert_command
1213

1314
from . import get_request_xml
15+
from ..json import assert_set_command
1416

1517
if TYPE_CHECKING:
1618
from deebot_client.events.base import Event
@@ -42,3 +44,13 @@ async def test_get_fan_speed_error(xml: str) -> None:
4244
None,
4345
command_result=CommandResult(HandlingState.ANALYSE_LOGGED),
4446
)
47+
48+
async def test_set_fan_speed() -> None:
49+
command = SetFanSpeed(FanSpeedLevel.MAX)
50+
json = get_request_xml("<ctl ret='ok' />")
51+
await assert_command(command, json, None, command_result=CommandResult(HandlingState.SUCCESS))
52+
53+
async def test_set_fan_speed_error() -> None:
54+
command = SetFanSpeed("invalid")
55+
json = get_request_xml("<ctl ret='error' />")
56+
await assert_command(command, json, None, command_result=CommandResult(HandlingState.FAILED))

0 commit comments

Comments
 (0)