Skip to content

Commit fe35037

Browse files
flubshiedenhauspentesttkrpre-commit-ci[bot]
authored
Add XML command "PlaySound" (#574)
Co-authored-by: flubshi <[email protected]> Co-authored-by: Robert Resch <[email protected]> Co-authored-by: Tim Kranz <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 765319a commit fe35037

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

deebot_client/commands/xml/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .error import GetError
1111
from .fan_speed import GetFanSpeed
1212
from .life_span import GetLifeSpan
13+
from .play_sound import PlaySound
1314
from .pos import GetPos
1415
from .stats import GetCleanSum
1516

@@ -23,13 +24,15 @@
2324
"GetFanSpeed",
2425
"GetLifeSpan",
2526
"GetPos",
27+
"PlaySound",
2628
]
2729

2830
# fmt: off
2931
# ordered by file asc
3032
_COMMANDS: list[type[XmlCommand]] = [
3133
GetError,
3234
GetLifeSpan,
35+
PlaySound,
3336
]
3437
# fmt: on
3538

deebot_client/commands/xml/common.py

+28-2
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, HandlingState, MessageStr
1515

1616
if TYPE_CHECKING:
1717
from deebot_client.event_bus import EventBus
@@ -60,3 +60,29 @@ def _handle_str(cls, event_bus: EventBus, message: str) -> HandlingResult:
6060
"""
6161
xml = ElementTree.fromstring(message)
6262
return cls._handle_xml(event_bus, xml)
63+
64+
65+
class ExecuteCommand(XmlCommandWithMessageHandling, ABC):
66+
"""Command, which is executing something (ex. Charge)."""
67+
68+
@classmethod
69+
def _handle_xml(cls, _: EventBus, xml: Element) -> HandlingResult:
70+
"""Handle message->xml and notify the correct event subscribers.
71+
72+
:return: A message response
73+
"""
74+
# Success event looks like <ctl ret='ok'/>
75+
if xml.attrib.get("ret") == "ok":
76+
return HandlingResult.success()
77+
78+
_LOGGER.warning(
79+
'Command "%s" was not successful. XML response: %s', cls.NAME, xml
80+
)
81+
return HandlingResult(HandlingState.FAILED)
82+
83+
84+
class XmlSetCommand(ExecuteCommand, SetCommand, ABC):
85+
"""Xml base set command.
86+
87+
Command needs to be linked to the "get" command, for handling (updating) the sensors.
88+
"""
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Play sound commands."""
2+
3+
from __future__ import annotations
4+
5+
from .common import ExecuteCommand
6+
7+
8+
class PlaySound(ExecuteCommand):
9+
"""Play sound command."""
10+
11+
NAME = "PlaySound"
12+
13+
def __init__(self) -> None:
14+
super().__init__({"sid": "30"})

tests/commands/xml/test_play_sound.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from deebot_client.command import CommandResult
6+
from deebot_client.commands.xml import PlaySound
7+
from deebot_client.message import HandlingState
8+
from tests.commands import assert_command
9+
10+
from . import get_request_xml
11+
12+
13+
@pytest.mark.parametrize(
14+
("xml_response", "command_result"),
15+
[
16+
("<ctl ret='ok'/>", HandlingState.SUCCESS),
17+
("<ctl ret='fail'/>", HandlingState.FAILED),
18+
],
19+
)
20+
async def test_play_sound(xml_response: str, command_result: HandlingState) -> None:
21+
json = get_request_xml(xml_response)
22+
await assert_command(
23+
PlaySound(), json, None, command_result=CommandResult(command_result)
24+
)

0 commit comments

Comments
 (0)