From 5baf7077d40c7d2e23245982aa7b4f8af30161c2 Mon Sep 17 00:00:00 2001 From: flubshi <4031504+flubshi@users.noreply.github.com> Date: Sun, 5 Jan 2025 11:56:48 +0100 Subject: [PATCH] XML add ExecuteCommand --- deebot_client/commands/xml/common.py | 35 +++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/deebot_client/commands/xml/common.py b/deebot_client/commands/xml/common.py index bc04d3d9..d220ad0b 100644 --- a/deebot_client/commands/xml/common.py +++ b/deebot_client/commands/xml/common.py @@ -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, MessageStr, HandlingState if TYPE_CHECKING: from deebot_client.event_bus import EventBus @@ -46,11 +46,16 @@ class XmlCommandWithMessageHandling( @classmethod @abstractmethod - def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult: + def _handle_xml(cls, _: EventBus, xml: Element) -> HandlingResult: """Handle xml message and notify the correct event subscribers. :return: A message response """ + if xml.attrib.get("ret") == "ok": + return HandlingResult.success() + + _LOGGER.warning('Command "%s" was not successfully. xml response: %s', cls.NAME, xml) + return HandlingResult(HandlingState.FAILED) @classmethod def _handle_str(cls, event_bus: EventBus, message: str) -> HandlingResult: @@ -60,3 +65,27 @@ 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, event_bus: EventBus, xml: Element) -> HandlingResult: + """Handle message->xml and notify the correct event subscribers. + + :return: A message response + """ + # Success event looks like + 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. + """ \ No newline at end of file