Skip to content

Add XML command "PlaySound" #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 20, 2025
3 changes: 3 additions & 0 deletions deebot_client/commands/xml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .error import GetError
from .fan_speed import GetFanSpeed
from .life_span import GetLifeSpan
from .play_sound import PlaySound
from .pos import GetPos
from .stats import GetCleanSum

Expand All @@ -23,13 +24,15 @@
"GetFanSpeed",
"GetLifeSpan",
"GetPos",
"PlaySound",
]

# fmt: off
# ordered by file asc
_COMMANDS: list[type[XmlCommand]] = [
GetError,
GetLifeSpan,
PlaySound,
]
# fmt: on

Expand Down
30 changes: 28 additions & 2 deletions deebot_client/commands/xml/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

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

from deebot_client.command import Command, CommandWithMessageHandling
from deebot_client.command import Command, CommandWithMessageHandling, SetCommand
from deebot_client.const import DataType
from deebot_client.logging_filter import get_logger
from deebot_client.message import HandlingResult, MessageStr
from deebot_client.message import HandlingResult, HandlingState, MessageStr

if TYPE_CHECKING:
from deebot_client.event_bus import EventBus
Expand Down Expand Up @@ -60,3 +60,29 @@ def _handle_str(cls, event_bus: EventBus, message: str) -> HandlingResult:
"""
xml = ElementTree.fromstring(message)
return cls._handle_xml(event_bus, xml)


class ExecuteCommand(XmlCommandWithMessageHandling, ABC):
"""Command, which is executing something (ex. Charge)."""

@classmethod
def _handle_xml(cls, _: EventBus, xml: Element) -> HandlingResult:
"""Handle message->xml and notify the correct event subscribers.

:return: A message response
"""
# Success event looks like <ctl ret='ok'/>
if xml.attrib.get("ret") == "ok":
return HandlingResult.success()

_LOGGER.warning(
'Command "%s" was not successful. XML response: %s', cls.NAME, xml
)
return HandlingResult(HandlingState.FAILED)


class XmlSetCommand(ExecuteCommand, SetCommand, ABC):
"""Xml base set command.

Command needs to be linked to the "get" command, for handling (updating) the sensors.
"""
14 changes: 14 additions & 0 deletions deebot_client/commands/xml/play_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Play sound commands."""

from __future__ import annotations

from .common import ExecuteCommand


class PlaySound(ExecuteCommand):
"""Play sound command."""

NAME = "PlaySound"

def __init__(self) -> None:
super().__init__({"sid": "30"})
24 changes: 24 additions & 0 deletions tests/commands/xml/test_play_sound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

import pytest

from deebot_client.command import CommandResult
from deebot_client.commands.xml import PlaySound
from deebot_client.message import HandlingState
from tests.commands import assert_command

from . import get_request_xml


@pytest.mark.parametrize(
("xml_response", "command_result"),
[
("<ctl ret='ok'/>", HandlingState.SUCCESS),
("<ctl ret='fail'/>", HandlingState.FAILED),
],
)
async def test_play_sound(xml_response: str, command_result: HandlingState) -> None:
json = get_request_xml(xml_response)
await assert_command(
PlaySound(), json, None, command_result=CommandResult(command_result)
)
Loading