|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | from abc import ABC, abstractmethod |
6 | | -from typing import TYPE_CHECKING, cast |
| 6 | +from typing import TYPE_CHECKING, cast, override |
7 | 7 | from xml.etree.ElementTree import Element, SubElement |
8 | 8 |
|
9 | 9 | from defusedxml import ElementTree # type: ignore[import-untyped] |
10 | 10 |
|
11 | | -from deebot_client.command import Command, CommandWithMessageHandling, SetCommand |
| 11 | +from deebot_client.command import ( |
| 12 | + Command, |
| 13 | + CommandMqttP2P, |
| 14 | + CommandWithMessageHandling, |
| 15 | + SetCommand, |
| 16 | +) |
12 | 17 | from deebot_client.const import DataType |
13 | 18 | from deebot_client.logging_filter import get_logger |
14 | 19 | from deebot_client.message import HandlingResult, HandlingState, MessageStr |
15 | 20 |
|
16 | 21 | if TYPE_CHECKING: |
| 22 | + from typing import Any |
| 23 | + |
17 | 24 | from deebot_client.event_bus import EventBus |
18 | 25 |
|
19 | 26 | _LOGGER = get_logger(__name__) |
@@ -76,11 +83,46 @@ def _handle_xml(cls, _: EventBus, xml: Element) -> HandlingResult: |
76 | 83 | return HandlingResult.success() |
77 | 84 |
|
78 | 85 | _LOGGER.warning( |
79 | | - 'Command "%s" was not successful. XML response: %s', cls.NAME, xml |
| 86 | + 'Command "%s" was not successful. XML response: %s', |
| 87 | + cls.NAME, |
| 88 | + ElementTree.tostring(xml, "unicode"), |
80 | 89 | ) |
81 | 90 | return HandlingResult(HandlingState.FAILED) |
82 | 91 |
|
83 | 92 |
|
| 93 | +class XmlCommandMqttP2P(XmlCommand, CommandMqttP2P, ABC): |
| 94 | + """Json base command for mqtt p2p channel.""" |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def create_from_mqtt(cls, payload: str | bytes | bytearray) -> CommandMqttP2P: |
| 98 | + """Create a command from the mqtt data.""" |
| 99 | + xml = ElementTree.fromstring(payload) |
| 100 | + return cls._create_from_mqtt(xml.attrib) |
| 101 | + |
| 102 | + @override |
| 103 | + def handle_mqtt_p2p( |
| 104 | + self, event_bus: EventBus, response_payload: str | bytes | bytearray |
| 105 | + ) -> None: |
| 106 | + """Handle response received over the mqtt channel "p2p".""" |
| 107 | + if isinstance(response_payload, bytearray): |
| 108 | + data = bytes(response_payload).decode() |
| 109 | + elif isinstance(response_payload, bytes): |
| 110 | + data = response_payload.decode() |
| 111 | + elif isinstance(response_payload, str): |
| 112 | + data = response_payload |
| 113 | + else: |
| 114 | + msg = "Unsupported message data type {message_type}" # type: ignore[unreachable] |
| 115 | + raise TypeError(msg.format(essage_type=type(response_payload))) |
| 116 | + |
| 117 | + self._handle_mqtt_p2p(event_bus, data) |
| 118 | + |
| 119 | + @abstractmethod |
| 120 | + def _handle_mqtt_p2p( |
| 121 | + self, event_bus: EventBus, response: dict[str, Any] | str |
| 122 | + ) -> None: |
| 123 | + """Handle response received over the mqtt channel "p2p".""" |
| 124 | + |
| 125 | + |
84 | 126 | class XmlSetCommand(ExecuteCommand, SetCommand, ABC): |
85 | 127 | """Xml base set command. |
86 | 128 |
|
|
0 commit comments