|
| 1 | +"""Implementation of vacuum (experimental).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from enum import IntEnum |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +from ...feature import Feature |
| 10 | +from ..smartmodule import SmartModule |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from ..smartdevice import SmartDevice |
| 14 | + |
| 15 | + |
| 16 | +_LOGGER = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class Status(IntEnum): |
| 20 | + """Status of vacuum.""" |
| 21 | + |
| 22 | + Idle = 0 |
| 23 | + Cleaning = 1 |
| 24 | + GoingHome = 4 |
| 25 | + Charging = 5 |
| 26 | + Charged = 6 |
| 27 | + Paused = 7 |
| 28 | + Error = 100 |
| 29 | + Unknown = 101 |
| 30 | + |
| 31 | + |
| 32 | +class Vacuum(SmartModule): |
| 33 | + """Implementation of experimental vacuum support.""" |
| 34 | + |
| 35 | + REQUIRED_COMPONENT = "clean" |
| 36 | + |
| 37 | + def __init__(self, device: SmartDevice, module: str): |
| 38 | + super().__init__(device, module) |
| 39 | + self._add_feature( |
| 40 | + Feature( |
| 41 | + device, |
| 42 | + "return_home", |
| 43 | + "Return home", |
| 44 | + container=self, |
| 45 | + attribute_setter="return_home", |
| 46 | + category=Feature.Category.Primary, |
| 47 | + type=Feature.Action, |
| 48 | + ) |
| 49 | + ) |
| 50 | + self._add_feature( |
| 51 | + Feature( |
| 52 | + device, |
| 53 | + "start_cleaning", |
| 54 | + "Start cleaning", |
| 55 | + container=self, |
| 56 | + attribute_setter="start", |
| 57 | + category=Feature.Category.Primary, |
| 58 | + type=Feature.Action, |
| 59 | + ) |
| 60 | + ) |
| 61 | + self._add_feature( |
| 62 | + Feature( |
| 63 | + device, |
| 64 | + "pause", |
| 65 | + "Pause", |
| 66 | + container=self, |
| 67 | + attribute_setter="pause", |
| 68 | + category=Feature.Category.Primary, |
| 69 | + type=Feature.Action, |
| 70 | + ) |
| 71 | + ) |
| 72 | + self._add_feature( |
| 73 | + Feature( |
| 74 | + device, |
| 75 | + "status", |
| 76 | + "Vacuum state", |
| 77 | + container=self, |
| 78 | + attribute_getter="status", |
| 79 | + category=Feature.Category.Primary, |
| 80 | + type=Feature.Type.Sensor, |
| 81 | + ) |
| 82 | + ) |
| 83 | + |
| 84 | + def query(self) -> dict: |
| 85 | + """Query to execute during the update cycle.""" |
| 86 | + return {"getVacStatus": None} |
| 87 | + |
| 88 | + async def start(self) -> None: |
| 89 | + """Start cleaning.""" |
| 90 | + # If we are paused, do not restart cleaning |
| 91 | + |
| 92 | + if self.status == Status.Paused: |
| 93 | + return await self.resume() |
| 94 | + |
| 95 | + # TODO: we need to create settings for clean_modes |
| 96 | + return self.call( |
| 97 | + "setSwitchClean", |
| 98 | + { |
| 99 | + "clean_mode": 0, |
| 100 | + "clean_on": True, |
| 101 | + "clean_order": True, |
| 102 | + "force_clean": False, |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + async def pause(self): |
| 107 | + """Pause cleaning.""" |
| 108 | + return await self.set_pause(True) |
| 109 | + |
| 110 | + async def resume(self): |
| 111 | + """Resume cleaning.""" |
| 112 | + return await self.set_pause(False) |
| 113 | + |
| 114 | + async def set_pause(self, enabled: bool) -> None: |
| 115 | + """Pause or resume cleaning.""" |
| 116 | + return self.call("setRobotPause", {"pause": enabled}) |
| 117 | + |
| 118 | + async def return_home(self): |
| 119 | + """Return home.""" |
| 120 | + return await self.set_return_home(True) |
| 121 | + |
| 122 | + async def set_return_home(self, enabled: bool) -> None: |
| 123 | + """Return home / pause returning.""" |
| 124 | + return self.call("setSwitchCharge", {"switch_charge": enabled}) |
| 125 | + |
| 126 | + @property |
| 127 | + def status(self) -> Status: |
| 128 | + """Return current status.""" |
| 129 | + if self.data.get("err_status"): |
| 130 | + return Status.Error |
| 131 | + status_code = self.data["status"] |
| 132 | + try: |
| 133 | + return Status(status_code) |
| 134 | + except Exception: |
| 135 | + _LOGGER.warning("Got unknown status code: %s (%s)", status_code, self.data) |
| 136 | + return Status.Unknown |
0 commit comments