Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device support of the Xiaomi Air Humidifier #66

Merged
merged 2 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mirobo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mirobo.plug import Plug
from mirobo.plug_v1 import PlugV1
from mirobo.airpurifier import AirPurifier
from mirobo.airhumidifier import AirHumidifier
from mirobo.strip import Strip
from mirobo.ceil import Ceil
from mirobo.philips_eyecare import PhilipsEyecare
Expand Down
125 changes: 125 additions & 0 deletions mirobo/airhumidifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import logging
import enum
from typing import Any, Dict, Optional
from collections import defaultdict
from .device import Device

_LOGGER = logging.getLogger(__name__)


class OperationMode(enum.Enum):
Silent = 'silent'
Medium = 'medium'
High = 'high'


class LedBrightness(enum.Enum):
Bright = 0
Dim = 1
Off = 2


class AirHumidifier(Device):
"""Main class representing the air humidifier."""

def status(self):
"""Retrieve properties."""

properties = ['power', 'mode', 'temp_dec', 'humidity', 'buzzer',
'led_b', ]

values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return AirHumidifierStatus(
defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])

def set_mode(self, mode: OperationMode):
"""Set mode."""
return self.send("set_mode", [mode.value])

def set_led_brightness(self, brightness: LedBrightness):
"""Set led brightness."""
return self.send("set_led_b", [brightness.value])

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'])

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"])


class AirHumidifierStatus:
"""Container for status reports from the air humidifier."""

def __init__(self, data: Dict[str, Any]) -> None:
self.data = data

@property
def power(self) -> str:
return self.data["power"]

@property
def is_on(self) -> bool:
return self.power == "on"

@property
def mode(self) -> OperationMode:
return OperationMode(self.data["mode"])

@property
def temperature(self) -> Optional[float]:
if self.data["temp_dec"] is not None:
return self.data["temp_dec"] / 10.0
return None

@property
def humidity(self) -> int:
return self.data["humidity"]

@property
def buzzer(self) -> bool:
return self.data["buzzer"] == "on"

@property
def led(self) -> bool:
return self.data["led"] == "on"

@property
def led_brightness(self) -> Optional[LedBrightness]:
if self.data["led_b"] is not None:
return LedBrightness(self.data["led_b"])
return None

def __str__(self) -> str:
s = "<AirHumidiferStatus power=%s, mode=%s, temperature=%s, " \
"humidity=%s%%, led=%s, led_brightness=%s, buzzer=%s>" % \
(self.power, self.mode, self.temperature,
self.humidity, self.led, self.led_brightness, self.buzzer)
return s
4 changes: 2 additions & 2 deletions mirobo/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import inspect
import codecs
from . import (Device, Vacuum, Plug, PlugV1, Strip, AirPurifier, Ceil,
PhilipsEyecare, ChuangmiIr)
PhilipsEyecare, ChuangmiIr, AirHumidifier)
from typing import Union, Callable, Dict, Optional # noqa: F401


Expand All @@ -24,7 +24,7 @@
"zhimi-airpurifier-v3": AirPurifier,
"zhimi-airpurifier-v6": AirPurifier,
"chuangmi-ir-v2": ChuangmiIr,
# "zhimi-humidifier-v1": Humidifier,
"zhimi-humidifier-v1": AirHumidifier,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'AirHumidifier'

# "yunmi-waterpuri-v2": WaterPurifier,
# It looks like philips devices cannot be discovered via mdns
"philips-light-bulb": Ceil,
Expand Down