1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
import enum
import logging
from collections import defaultdict
from typing import Any, Dict, Optional
import click
from .click_common import EnumType, command, format_output
from .device import Device, DeviceInfo, DeviceStatus
from .exceptions import DeviceError, DeviceException
_LOGGER = logging.getLogger(__name__)
MODEL_DEHUMIDIFIER_V1 = "nwt.derh.wdh318efw1"
AVAILABLE_PROPERTIES = {
MODEL_DEHUMIDIFIER_V1: [
"on_off",
"mode",
"fan_st",
"buzzer",
"led",
"child_lock",
"humidity",
"temp",
"compressor_status",
"fan_speed",
"tank_full",
"defrost_status",
"alarm",
"auto",
]
}
class AirDehumidifierException(DeviceException):
pass
class OperationMode(enum.Enum):
On = "on"
Auto = "auto"
DryCloth = "dry_cloth"
class FanSpeed(enum.Enum):
Sleep = 0
Low = 1
Medium = 2
High = 3
Strong = 4
class AirDehumidifierStatus(DeviceStatus):
"""Container for status reports from the air dehumidifier."""
def __init__(self, data: Dict[str, Any], device_info: DeviceInfo) -> None:
"""Response of a Air Dehumidifier (nwt.derh.wdh318efw1):
{'on_off': 'on', 'mode': 'auto', 'fan_st': 2,
'buzzer': 'off', 'led': 'on', 'child_lock': 'off',
'humidity': 47, 'temp': 34, 'compressor_status': 'off',
'fan_speed': 0, 'tank_full': 'off', 'defrost_status': 'off,
'alarm': 'ok','auto': 50}
"""
self.data = data
self.device_info = device_info
@property
def power(self) -> str:
"""Power state."""
return self.data["on_off"]
@property
def is_on(self) -> bool:
"""True if device is turned on."""
return self.power == "on"
@property
def mode(self) -> OperationMode:
"""Operation mode.
Can be either on, auth or dry_cloth.
"""
return OperationMode(self.data["mode"])
@property
def temperature(self) -> Optional[float]:
"""Current temperature, if available."""
if "temp" in self.data and self.data["temp"] is not None:
return self.data["temp"]
return None
@property
def humidity(self) -> int:
"""Current humidity."""
return self.data["humidity"]
@property
def buzzer(self) -> bool:
"""True if buzzer is turned on."""
return self.data["buzzer"] == "on"
@property
def led(self) -> bool:
"""LED brightness if available."""
return self.data["led"] == "on"
@property
def child_lock(self) -> bool:
"""Return True if child lock is on."""
return self.data["child_lock"] == "on"
@property
def target_humidity(self) -> Optional[int]:
"""Target humiditiy.
Can be either 40, 50, 60 percent.
"""
if "auto" in self.data and self.data["auto"] is not None:
return self.data["auto"]
return None
@property
def fan_speed(self) -> Optional[FanSpeed]:
"""Current fan speed."""
if "fan_speed" in self.data and self.data["fan_speed"] is not None:
return FanSpeed(self.data["fan_speed"])
return None
@property
def tank_full(self) -> bool:
"""The remaining amount of water in percent."""
return self.data["tank_full"] == "on"
@property
def compressor_status(self) -> bool:
"""Compressor status."""
return self.data["compressor_status"] == "on"
@property
def defrost_status(self) -> bool:
"""Defrost status."""
return self.data["defrost_status"] == "on"
@property
def fan_st(self) -> int:
"""Fan st."""
return self.data["fan_st"]
@property
def alarm(self) -> str:
"""Alarm."""
return self.data["alarm"]
class AirDehumidifier(Device):
"""Implementation of Xiaomi Mi Air Dehumidifier."""
_supported_models = list(AVAILABLE_PROPERTIES.keys())
@command(
default_output=format_output(
"",
"Power: {result.power}\n"
"Mode: {result.mode}\n"
"Temperature: {result.temperature} °C\n"
"Humidity: {result.humidity} %\n"
"Buzzer: {result.buzzer}\n"
"LED : {result.led}\n"
"Child lock: {result.child_lock}\n"
"Target humidity: {result.target_humidity} %\n"
"Fan speed: {result.fan_speed}\n"
"Tank Full: {result.tank_full}\n"
"Compressor Status: {result.compressor_status}\n"
"Defrost Status: {result.defrost_status}\n"
"Fan st: {result.fan_st}\n"
"Alarm: {result.alarm}\n",
)
)
def status(self) -> AirDehumidifierStatus:
"""Retrieve properties."""
properties = AVAILABLE_PROPERTIES.get(
self.model, AVAILABLE_PROPERTIES[MODEL_DEHUMIDIFIER_V1]
)
values = self.get_properties(properties, max_properties=1)
return AirDehumidifierStatus(
defaultdict(lambda: None, zip(properties, values)), self.info()
)
@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.send("set_power", ["on"])
@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.send("set_power", ["off"])
@command(
click.argument("mode", type=EnumType(OperationMode)),
default_output=format_output("Setting mode to '{mode.value}'"),
)
def set_mode(self, mode: OperationMode):
"""Set mode."""
try:
return self.send("set_mode", [mode.value])
except DeviceError as error:
# {'code': -6011, 'message': 'device_poweroff'}
if error.code == -6011:
self.on()
return self.send("set_mode", [mode.value])
raise
@command(
click.argument("fan_speed", type=EnumType(FanSpeed)),
default_output=format_output("Setting fan level to {fan_speed}"),
)
def set_fan_speed(self, fan_speed: FanSpeed):
"""Set the fan speed."""
try:
return self.send("set_fan_level", [fan_speed.value])
except DeviceError as ex:
if ex.code == -10000:
raise AirDehumidifierException(
"Unable to set fan speed, this can happen if device is turned off."
) from ex
raise
@command(
click.argument("led", type=bool),
default_output=format_output(
lambda led: "Turning on LED" if led else "Turning off LED"
),
)
def set_led(self, led: bool):
"""Turn led on/off."""
if led:
return self.send("set_led", ["on"])
else:
return self.send("set_led", ["off"])
@command(
click.argument("buzzer", type=bool),
default_output=format_output(
lambda buzzer: "Turning on buzzer" if buzzer else "Turning off buzzer"
),
)
def set_buzzer(self, buzzer: bool):
"""Set buzzer on/off."""
if buzzer:
return self.send("set_buzzer", ["on"])
else:
return self.send("set_buzzer", ["off"])
@command(
click.argument("lock", type=bool),
default_output=format_output(
lambda lock: "Turning on child lock" if lock else "Turning off child lock"
),
)
def set_child_lock(self, lock: bool):
"""Set child lock on/off."""
if lock:
return self.send("set_child_lock", ["on"])
else:
return self.send("set_child_lock", ["off"])
@command(
click.argument("humidity", type=int),
default_output=format_output("Setting target humidity to {humidity}"),
)
def set_target_humidity(self, humidity: int):
"""Set the auto target humidity."""
if humidity not in [40, 50, 60]:
raise AirDehumidifierException(
"Invalid auto target humidity: %s" % humidity
)
return self.send("set_auto", [humidity])
|