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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
import enum
import logging
from datetime import timedelta
from typing import Any, Dict
import click
from .click_common import EnumType, command, format_output
from .exceptions import DeviceException
from .miot_device import DeviceStatus, MiotDevice
_LOGGER = logging.getLogger(__name__)
SUPPORTED_MODELS = [
"xiaomi.aircondition.mc1",
"xiaomi.aircondition.mc2",
"xiaomi.aircondition.mc4",
"xiaomi.aircondition.mc5",
]
_MAPPING = {
# Source http://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-conditioner:0000A004:xiaomi-mc4:1
# Air Conditioner (siid=2)
"power": {"siid": 2, "piid": 1},
"mode": {"siid": 2, "piid": 2},
"target_temperature": {"siid": 2, "piid": 4},
"eco": {"siid": 2, "piid": 7},
"heater": {"siid": 2, "piid": 9},
"dryer": {"siid": 2, "piid": 10},
"sleep_mode": {"siid": 2, "piid": 11},
# Fan Control (siid=3)
"fan_speed": {"siid": 3, "piid": 2},
"vertical_swing": {"siid": 3, "piid": 4},
# Environment (siid=4)
"temperature": {"siid": 4, "piid": 7},
# Alarm (siid=5)
"buzzer": {"siid": 5, "piid": 1},
# Indicator Light (siid=6)
"led": {"siid": 6, "piid": 1},
# Electricity (siid=8)
"electricity": {"siid": 8, "piid": 1},
# Maintenance (siid=9)
"clean": {"siid": 9, "piid": 1},
"running_duration": {"siid": 9, "piid": 5},
# Enhance (siid=10)
"fan_speed_percent": {"siid": 10, "piid": 1},
"timer": {"siid": 10, "piid": 3},
}
_MAPPINGS = {model: _MAPPING for model in SUPPORTED_MODELS}
CLEANING_STAGES = [
"Stopped",
"Condensing water",
"Frosting the surface",
"Defrosting the surface",
"Drying",
]
class AirConditionerMiotException(DeviceException):
pass
class CleaningStatus(DeviceStatus):
def __init__(self, status: str):
"""Auto clean mode indicator.
Value format: <int>,<int>,<int>,<int>
Integer 1: whether auto cleaning mode started.
Integer 2: current progress in percent.
Integer 3: which stage it is currently under (see CLEANING_STAGE list).
Integer 4: if current operation could be cancelled.
Example auto clean indicator 1: 0,100,0,1
indicates the auto clean mode has finished or not started yet.
Example auto clean indicator 2: 1,22,1,1
indicates auto clean mode finished 22%, it is condensing water and can be cancelled.
Example auto clean indicator 3: 1,72,4,0
indicates auto clean mode finished 72%, it is drying and cannot be cancelled.
Only write 1 or 0 to it would start or abort the auto clean mode.
"""
self.status = [int(value) for value in status.split(",")]
@property
def cleaning(self) -> bool:
return bool(self.status[0])
@property
def progress(self) -> int:
return int(self.status[1])
@property
def stage(self) -> str:
try:
return CLEANING_STAGES[self.status[2]]
except KeyError:
return "Unknown stage"
@property
def cancellable(self) -> bool:
return bool(self.status[3])
class OperationMode(enum.Enum):
Cool = 2
Dry = 3
Fan = 4
Heat = 5
class FanSpeed(enum.Enum):
Auto = 0
Level1 = 1
Level2 = 2
Level3 = 3
Level4 = 4
Level5 = 5
Level6 = 6
Level7 = 7
class TimerStatus(DeviceStatus):
def __init__(self, status):
"""Countdown timer indicator.
Value format: <int>,<int>,<int>,<int>
Integer 1: whether the timer is enabled.
Integer 2: countdown timer setting value in minutes.
Integer 3: the device would be powered on (1) or powered off (0) after timeout.
Integer 4: the remaining countdown time in minutes.
Example timer value 1: 1,120,0,103
indicates the device would be turned off after 120 minutes, remaining 103 minutes.
Example timer value 2: 1,60,1,60
indicates the device would be turned on after 60 minutes, remaining 60 minutes.
Example timer value 3: 0,0,0,0
indicates countdown timer not set.
Write the first three integers would set the correct countdown timer.
Also, if the countdown minutes set to 0, the timer would be disabled.
"""
self.status = [int(value) for value in status.split(",")]
@property
def enabled(self) -> bool:
return bool(self.status[0])
@property
def countdown(self) -> timedelta:
return timedelta(minutes=self.status[1])
@property
def power_on(self) -> bool:
return bool(self.status[2])
@property
def time_left(self) -> timedelta:
return timedelta(minutes=self.status[3])
class AirConditionerMiotStatus(DeviceStatus):
"""Container for status reports from the air conditioner (MIoT)."""
def __init__(self, data: Dict[str, Any]) -> None:
"""
Response (MIoT format) of a Mi Smart Air Conditioner A (xiaomi.aircondition.mc4)
[
{'did': 'power', 'siid': 2, 'piid': 1, 'code': 0, 'value': False},
{'did': 'mode', 'siid': 2, 'piid': 2, 'code': 0, 'value': 2},
{'did': 'target_temperature', 'siid': 2, 'piid': 4, 'code': 0, 'value': 26.5},
{'did': 'eco', 'siid': 2, 'piid': 7, 'code': 0, 'value': False},
{'did': 'heater', 'siid': 2, 'piid': 9, 'code': 0, 'value': True},
{'did': 'dryer', 'siid': 2, 'piid': 10, 'code': 0, 'value': True},
{'did': 'sleep_mode', 'siid': 2, 'piid': 11, 'code': 0, 'value': False},
{'did': 'fan_speed', 'siid': 3, 'piid': 2, 'code': 0, 'value': 0},
{'did': 'vertical_swing', 'siid': 3, 'piid': 4, 'code': 0, 'value': True},
{'did': 'temperature', 'siid': 4, 'piid': 7, 'code': 0, 'value': 28.4},
{'did': 'buzzer', 'siid': 5, 'piid': 1, 'code': 0, 'value': False},
{'did': 'led', 'siid': 6, 'piid': 1, 'code': 0, 'value': False},
{'did': 'electricity', 'siid': 8, 'piid': 1, 'code': 0, 'value': 0.0},
{'did': 'clean', 'siid': 9, 'piid': 1, 'code': 0, 'value': '0,100,1,1'},
{'did': 'running_duration', 'siid': 9, 'piid': 5, 'code': 0, 'value': 151.0},
{'did': 'fan_speed_percent', 'siid': 10, 'piid': 1, 'code': 0, 'value': 101},
{'did': 'timer', 'siid': 10, 'piid': 3, 'code': 0, 'value': '0,0,0,0'}
]
"""
self.data = data
@property
def is_on(self) -> bool:
"""True if the device is turned on."""
return self.data["power"]
@property
def power(self) -> str:
"""Current power state."""
return "on" if self.is_on else "off"
@property
def mode(self) -> OperationMode:
"""Current operation mode."""
return OperationMode(self.data["mode"])
@property
def target_temperature(self) -> float:
"""Target temperature in Celsius."""
return self.data["target_temperature"]
@property
def eco(self) -> bool:
"""True if ECO mode is on."""
return self.data["eco"]
@property
def heater(self) -> bool:
"""True if aux heat mode is on."""
return self.data["heater"]
@property
def dryer(self) -> bool:
"""True if aux dryer mode is on."""
return self.data["dryer"]
@property
def sleep_mode(self) -> bool:
"""True if sleep mode is on."""
return self.data["sleep_mode"]
@property
def fan_speed(self) -> FanSpeed:
"""Current Fan speed."""
return FanSpeed(self.data["fan_speed"])
@property
def vertical_swing(self) -> bool:
"""True if vertical swing is on."""
return self.data["vertical_swing"]
@property
def temperature(self) -> float:
"""Current ambient temperature in Celsius."""
return self.data["temperature"]
@property
def buzzer(self) -> bool:
"""True if buzzer is on."""
return self.data["buzzer"]
@property
def led(self) -> bool:
"""True if LED is on."""
return self.data["led"]
@property
def electricity(self) -> float:
"""Power consumption accumulation in kWh."""
return self.data["electricity"]
@property
def clean(self) -> CleaningStatus:
"""Auto clean mode indicator."""
return CleaningStatus(self.data["clean"])
@property
def total_running_duration(self) -> timedelta:
"""Total running duration in hours."""
return timedelta(hours=self.data["running_duration"])
@property
def fan_speed_percent(self) -> int:
"""Current fan speed in percent."""
return self.data["fan_speed_percent"]
@property
def timer(self) -> TimerStatus:
"""Countdown timer indicator."""
return TimerStatus(self.data["timer"])
class AirConditionerMiot(MiotDevice):
"""Main class representing the air conditioner which uses MIoT protocol."""
_mappings = _MAPPINGS
@command(
default_output=format_output(
"",
"Power: {result.power}\n"
"Mode: {result.mode}\n"
"Target Temperature: {result.target_temperature} ℃\n"
"ECO Mode: {result.eco}\n"
"Heater: {result.heater}\n"
"Dryer: {result.dryer}\n"
"Sleep Mode: {result.sleep_mode}\n"
"Fan Speed: {result.fan_speed}\n"
"Vertical Swing: {result.vertical_swing}\n"
"Room Temperature: {result.temperature} ℃\n"
"Buzzer: {result.buzzer}\n"
"LED: {result.led}\n"
"Electricity: {result.electricity}kWh\n"
"Clean: {result.clean}\n"
"Running Duration: {result.total_running_duration}\n"
"Fan percent: {result.fan_speed_percent}\n"
"Timer: {result.timer}\n",
)
)
def status(self) -> AirConditionerMiotStatus:
"""Retrieve properties."""
return AirConditionerMiotStatus(
{
prop["did"]: prop["value"] if prop["code"] == 0 else None
for prop in self.get_properties_for_mapping()
}
)
@command(default_output=format_output("Powering on"))
def on(self):
"""Power on."""
return self.set_property("power", True)
@command(default_output=format_output("Powering off"))
def off(self):
"""Power off."""
return self.set_property("power", False)
@command(
click.argument("mode", type=EnumType(OperationMode)),
default_output=format_output("Setting operation mode to '{mode.value}'"),
)
def set_mode(self, mode: OperationMode):
"""Set operation mode."""
return self.set_property("mode", mode.value)
@command(
click.argument("target_temperature", type=float),
default_output=format_output(
"Setting target temperature to {target_temperature}"
),
)
def set_target_temperature(self, target_temperature: float):
"""Set target temperature in Celsius."""
if (
target_temperature < 16.0
or target_temperature > 31.0
or target_temperature % 0.5 != 0
):
raise AirConditionerMiotException(
"Invalid target temperature: %s" % target_temperature
)
return self.set_property("target_temperature", target_temperature)
@command(
click.argument("eco", type=bool),
default_output=format_output(
lambda eco: "Turning on ECO mode" if eco else "Turning off ECO mode"
),
)
def set_eco(self, eco: bool):
"""Turn ECO mode on/off."""
return self.set_property("eco", eco)
@command(
click.argument("heater", type=bool),
default_output=format_output(
lambda heater: "Turning on heater" if heater else "Turning off heater"
),
)
def set_heater(self, heater: bool):
"""Turn aux heater mode on/off."""
return self.set_property("heater", heater)
@command(
click.argument("dryer", type=bool),
default_output=format_output(
lambda dryer: "Turning on dryer" if dryer else "Turning off dryer"
),
)
def set_dryer(self, dryer: bool):
"""Turn aux dryer mode on/off."""
return self.set_property("dryer", dryer)
@command(
click.argument("sleep_mode", type=bool),
default_output=format_output(
lambda sleep_mode: "Turning on sleep mode"
if sleep_mode
else "Turning off sleep mode"
),
)
def set_sleep_mode(self, sleep_mode: bool):
"""Turn sleep mode on/off."""
return self.set_property("sleep_mode", sleep_mode)
@command(
click.argument("fan_speed", type=EnumType(FanSpeed)),
default_output=format_output("Setting fan speed to {fan_speed}"),
)
def set_fan_speed(self, fan_speed: FanSpeed):
"""Set fan speed."""
return self.set_property("fan_speed", fan_speed.value)
@command(
click.argument("vertical_swing", type=bool),
default_output=format_output(
lambda vertical_swing: "Turning on vertical swing"
if vertical_swing
else "Turning off vertical swing"
),
)
def set_vertical_swing(self, vertical_swing: bool):
"""Turn vertical swing on/off."""
return self.set_property("vertical_swing", vertical_swing)
@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."""
return self.set_property("led", led)
@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."""
return self.set_property("buzzer", buzzer)
@command(
click.argument("percent", type=int),
default_output=format_output("Setting fan percent to {percent}%"),
)
def set_fan_speed_percent(self, fan_speed_percent):
"""Set fan speed in percent, should be between 1 to 100 or 101(auto)."""
if fan_speed_percent < 1 or fan_speed_percent > 101:
raise AirConditionerMiotException(
"Invalid fan percent: %s" % fan_speed_percent
)
return self.set_property("fan_speed_percent", fan_speed_percent)
@command(
click.argument("minutes", type=int),
click.argument("delay_on", type=bool),
default_output=format_output(
lambda minutes, delay_on: "Setting timer to delay on after "
+ str(minutes)
+ " minutes"
if delay_on
else "Setting timer to delay off after " + str(minutes) + " minutes"
),
)
def set_timer(self, minutes, delay_on):
"""Set countdown timer minutes and if it would be turned on after timeout.
Set minutes to 0 would disable the timer.
"""
return self.set_property(
"timer", ",".join(["1", str(minutes), str(int(delay_on))])
)
@command(
click.argument("clean", type=bool),
default_output=format_output(
lambda clean: "Begin auto cleanning" if clean else "Abort auto cleaning"
),
)
def set_clean(self, clean):
"""Start or abort clean mode."""
return self.set_property("clean", str(int(clean)))
|