|
| 1 | +"""Clean commands.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from deebot_client.events import FanSpeedEvent, FanSpeedLevel, StateEvent |
| 8 | +from deebot_client.logging_filter import get_logger |
| 9 | +from deebot_client.message import HandlingResult |
| 10 | +from deebot_client.models import CleanAction, CleanMode, State |
| 11 | + |
| 12 | +from .common import ExecuteCommand, XmlCommandWithMessageHandling |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from xml.etree.ElementTree import Element |
| 16 | + |
| 17 | + from deebot_client.event_bus import EventBus |
| 18 | + |
| 19 | +_LOGGER = get_logger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class Clean(ExecuteCommand): |
| 23 | + """Generic start/pause/stop cleaning command.""" |
| 24 | + |
| 25 | + NAME = "Clean" |
| 26 | + HAS_SUB_ELEMENT = True |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, action: CleanAction, speed: FanSpeedLevel = FanSpeedLevel.NORMAL |
| 30 | + ) -> None: |
| 31 | + # <ctl><clean type='SpotArea' act='s' speed='standard' deep='1' mid='4,5'/></ctl> |
| 32 | + |
| 33 | + super().__init__( |
| 34 | + { |
| 35 | + "type": CleanMode.AUTO.xml_value, |
| 36 | + "act": action.xml_value, |
| 37 | + "speed": speed.xml_value, |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +class CleanArea(ExecuteCommand): |
| 43 | + """Clean area command.""" |
| 44 | + |
| 45 | + NAME = "Clean" |
| 46 | + HAS_SUB_ELEMENT = True |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + mode: CleanMode, |
| 51 | + area: str, |
| 52 | + cleanings: int = 1, |
| 53 | + speed: FanSpeedLevel = FanSpeedLevel.NORMAL, |
| 54 | + ) -> None: |
| 55 | + # <ctl><clean type='SpotArea' act='s' speed='standard' deep='1' mid='4,5'/></ctl> |
| 56 | + |
| 57 | + super().__init__( |
| 58 | + { |
| 59 | + "type": mode.xml_value, |
| 60 | + "act": CleanAction.START.xml_value, |
| 61 | + "speed": speed.xml_value, |
| 62 | + "deep": str(cleanings), |
| 63 | + "mid": area, |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class GetCleanState(XmlCommandWithMessageHandling): |
| 69 | + """GetCleanState command.""" |
| 70 | + |
| 71 | + NAME = "GetCleanState" |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def _handle_xml(cls, event_bus: EventBus, xml: Element) -> HandlingResult: |
| 75 | + """Handle xml message and notify the correct event subscribers. |
| 76 | +
|
| 77 | + :return: A message response |
| 78 | + """ |
| 79 | + if xml.attrib.get("ret") != "ok" or (clean := xml.find("clean")) is None: |
| 80 | + return HandlingResult.analyse() |
| 81 | + |
| 82 | + speed_attrib = clean.attrib.get("speed") |
| 83 | + if speed_attrib is not None: |
| 84 | + fan_speed_level = FanSpeedLevel.from_xml(speed_attrib) |
| 85 | + event_bus.notify(FanSpeedEvent(fan_speed_level)) |
| 86 | + |
| 87 | + clean_attrib = clean.attrib.get("st") |
| 88 | + if clean_attrib is not None: |
| 89 | + clean_action = CleanAction.from_xml(clean_attrib) |
| 90 | + if clean_action == CleanAction.START: |
| 91 | + event_bus.notify(StateEvent(State.CLEANING)) |
| 92 | + elif clean_action == CleanAction.PAUSE: |
| 93 | + event_bus.notify(StateEvent(State.PAUSED)) |
| 94 | + else: |
| 95 | + _LOGGER.debug("Ignored CleanState %s", clean_action) |
| 96 | + |
| 97 | + return HandlingResult.success() |
0 commit comments