|
| 1 | +"""WaterBox command module.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from deebot_client.commands.xml.common import ( |
| 8 | + ExecuteCommand, |
| 9 | + XmlCommandWithMessageHandling, |
| 10 | +) |
| 11 | +from deebot_client.events.water_info import ( |
| 12 | + WaterAmount, |
| 13 | + WaterAmountEvent, |
| 14 | +) |
| 15 | +from deebot_client.message import HandlingResult |
| 16 | +from deebot_client.messages.xml import WaterBoxInfo |
| 17 | +from deebot_client.util import get_enum |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from xml.etree.ElementTree import Element |
| 21 | + |
| 22 | + from deebot_client.event_bus import EventBus |
| 23 | + |
| 24 | + |
| 25 | +class GetWaterPermeability(XmlCommandWithMessageHandling): |
| 26 | + """GetWaterPermeability command.""" |
| 27 | + |
| 28 | + NAME = "GetWaterPermeability" |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult: |
| 32 | + """Handle xml message and notify the correct event subscribers. |
| 33 | +
|
| 34 | + :return: A message response. |
| 35 | + """ |
| 36 | + if xml.attrib.get("ret") != "ok" or not (value := xml.attrib.get("v")): |
| 37 | + return HandlingResult.analyse() |
| 38 | + |
| 39 | + if value.isdecimal() and (value_int := int(value)) >= 0: |
| 40 | + event_bus.notify(WaterAmountEvent(WaterAmount(value_int))) |
| 41 | + return HandlingResult.success() |
| 42 | + |
| 43 | + return HandlingResult.analyse() |
| 44 | + |
| 45 | + |
| 46 | +class SetWaterPermeability(ExecuteCommand): |
| 47 | + """SetWaterPermeability command.""" |
| 48 | + |
| 49 | + NAME = "SetWaterPermeability" |
| 50 | + |
| 51 | + def __init__(self, amount: WaterAmount | str) -> None: |
| 52 | + if isinstance(amount, str): |
| 53 | + amount = get_enum(WaterAmount, amount) |
| 54 | + super().__init__({"v": str(amount.value)}) |
| 55 | + |
| 56 | + |
| 57 | +class GetWaterBoxInfo(XmlCommandWithMessageHandling, WaterBoxInfo): |
| 58 | + """GetWaterBoxInfo command.""" |
| 59 | + |
| 60 | + NAME = "GetWaterBoxInfo" |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult: |
| 64 | + """Handle xml message and notify the correct event subscribers. |
| 65 | +
|
| 66 | + :return: A message response. |
| 67 | + """ |
| 68 | + if xml.attrib.get("ret") != "ok": |
| 69 | + return HandlingResult.analyse() |
| 70 | + |
| 71 | + return cls._parse_xml(event_bus, xml) |
0 commit comments