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

New properties of the Xiaomi Air Humidifier added #173

Merged
merged 16 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
71 changes: 65 additions & 6 deletions miio/airhumidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
_LOGGER = logging.getLogger(__name__)


class AirHumidifierException(Exception):
Copy link
Owner

Choose a reason for hiding this comment

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

This should derive from DeviceException (looks like we have some other exceptions too, which do not do this currently) to allow easy catching of all exception from this library no matter which device is being used.

pass


class OperationMode(enum.Enum):
Silent = 'silent'
Medium = 'medium'
Expand All @@ -27,7 +31,8 @@ def __init__(self, data: Dict[str, Any]) -> None:
Response of a Air Humidifier (zhimi.humidifier.v1):

{'power': 'off', 'mode': 'high', 'temp_dec': 294,
'humidity': 33, 'buzzer': 'on', 'led_b': 0}
'humidity': 33, 'buzzer': 'on', 'led_b': 0,
'child_lock': 'on', 'limit_hum': 40, 'trans_level': 85}
"""

self.data = data
Expand Down Expand Up @@ -71,11 +76,41 @@ def led_brightness(self) -> Optional[LedBrightness]:
return LedBrightness(self.data["led_b"])
return None

@property
def child_lock(self) -> bool:
"""Return True if child lock is on."""
return self.data["child_lock"] == "on"

@property
def target_humidity(self) -> int:
"""Target humiditiy. Can be either 40, 50, 60, 70, 80 percent."""
return self.data["limit_hum"]

@property
def favorite_level(self) -> int:
"""Return favorite level, which is used if the mode is ``favorite``."""
# Favorite level used when the mode is `favorite`.
return self.data["trans_level"]

def __str__(self) -> str:
s = "<AirHumidiferStatus power=%s, mode=%s, temperature=%s, " \
"humidity=%s%%, led_brightness=%s, buzzer=%s>" % \
(self.power, self.mode, self.temperature,
self.humidity, self.led_brightness, self.buzzer)
s = "<AirHumidiferStatus power=%s, " \
"mode=%s, " \
"temperature=%s, " \
"humidity=%s%%, " \
"led_brightness=%s, " \
"buzzer=%s, " \
"child_lock=%s, " \
"target_humidity=%s%%, " \
"trans_level=%s>" % \
(self.power,
self.mode,
self.temperature,
self.humidity,
self.led_brightness,
self.buzzer,
self.child_lock,
self.target_humidity,
self.trans_level)
return s


Expand All @@ -86,7 +121,7 @@ def status(self) -> AirHumidifierStatus:
"""Retrieve properties."""

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

values = self.send(
"get_prop",
Expand Down Expand Up @@ -126,3 +161,27 @@ def set_buzzer(self, buzzer: bool):
return self.send("set_buzzer", ["on"])
else:
return self.send("set_buzzer", ["off"])

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

def set_target_humidity(self, humidity: int):
"""Set the target humidity."""
if humidity not in [40, 50, 60, 70, 80]:
raise AirHumidifierException(
"Invalid target humidity: %s" % humidity)

return self.send("set_limit_hum", [humidity])

def set_favorite_level(self, level: int):
"""Set favorite level."""
if level < 30 or level > 85:
raise AirHumidifierException("Invalid favorite level: %s" % level)
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a link between this level and target humidity (which allows only specific values)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. It's more like a "fan speed".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, feel free to merge when you are ready 👍


# Set the favorite level used when the mode is `favorite`,
# should be between 30 and 85.
return self.send("set_trans_level", [level]) # 30 ... 85
68 changes: 65 additions & 3 deletions miio/tests/test_airhumidifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase
from miio import AirHumidifier
from miio.airhumidifier import OperationMode, LedBrightness
from miio.airhumidifier import OperationMode, LedBrightness, AirHumidifierException

Choose a reason for hiding this comment

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

line too long (83 > 79 characters)

Copy link
Owner

Choose a reason for hiding this comment

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

I think houndci should be configured to allow lines up to 100 lines, even when <80 is mostly preferred. This is not a biggie (esp. as it is in the tests), but can be fixed at some point.

from .dummies import DummyDevice
import pytest

Expand All @@ -10,17 +10,22 @@ def __init__(self, *args, **kwargs):
self.state = {
'power': 'on',
'mode': 'medium',
'temp_dec': 186,
'humidity': 62,
'temp_dec': 294,
'humidity': 33,
'buzzer': 'off',
'led_b': 2,
'child_lock': 'on',
'limit_hum': 40,
'trans_level': 85,
}
self.return_values = {
'get_prop': self._get_state,
'set_power': lambda x: self._set_state("power", x),
'set_mode': lambda x: self._set_state("mode", x),
'set_led_b': lambda x: self._set_state("led_b", x),
'set_buzzer': lambda x: self._set_state("buzzer", x),
'set_child_lock': lambda x: self._set_state("child_lock", x),
'set_trans_level': lambda x: self._set_state("trans_level", x),
}
super().__init__(args, kwargs)

Expand Down Expand Up @@ -64,6 +69,9 @@ def test_status(self):
assert self.state().mode == OperationMode(self.device.start_state["mode"])
assert self.state().led_brightness == LedBrightness(self.device.start_state["led_b"])
assert self.state().buzzer == (self.device.start_state["buzzer"] == 'on')
assert self.state().child_lock == (self.device.start_state["child_lock"] == 'on')

Choose a reason for hiding this comment

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

line too long (89 > 79 characters)

assert self.state().target_humidity == self.device.start_state["limit_hum"]

Choose a reason for hiding this comment

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

line too long (83 > 79 characters)

assert self.state().favorite_level == self.device.start_state["trans_level"]

Choose a reason for hiding this comment

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

line too long (84 > 79 characters)


def test_set_mode(self):
def mode():
Expand Down Expand Up @@ -112,3 +120,57 @@ def test_status_without_led_brightness(self):
self.device.state["led_b"] = None

assert self.state().led_brightness is None

def test_set_target_humidity(self):
def target_humidity():
return self.device.status().target_humidity

self.device.set_target_humidity(40)
assert target_humidity() == 40
self.device.set_target_humidity(60)
assert target_humidity() == 60
self.device.set_target_humidity(80)
assert target_humidity() == 80

with pytest.raises(AirHumidifierException):
self.device.set_target_humidity(-1)

with pytest.raises(AirHumidifierException):
self.device.set_target_humidity(30)

with pytest.raises(AirHumidifierException):
self.device.set_target_humidity(90)

with pytest.raises(AirHumidifierException):
self.device.set_target_humidity(110)

def test_set_favorite_level(self):
def favorite_level():
return self.device.status().favorite_level

self.device.set_favorite_level(30)
assert favorite_level() == 30
self.device.set_favorite_level(55)
assert favorite_level() == 55
self.device.set_favorite_level(85)
assert favorite_level() == 85

with pytest.raises(AirHumidifierException):
self.device.set_favorite_level(-1)

with pytest.raises(AirHumidifierException):
self.device.set_favorite_level(29)

with pytest.raises(AirHumidifierException):
self.device.set_favorite_level(86)

def test_set_child_lock(self):
def child_lock():
return self.device.status().child_lock

self.device.set_child_lock(True)
assert child_lock() is True

self.device.set_child_lock(False)
assert child_lock() is False

Choose a reason for hiding this comment

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

blank line at end of file

15 changes: 3 additions & 12 deletions miio/tests/test_airpurifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def favorite_level():
self.device.set_favorite_level(6)
assert favorite_level() == 6
self.device.set_favorite_level(10)
assert favorite_level() == 10

with pytest.raises(AirPurifierException):
self.device.set_favorite_level(-1)
Expand All @@ -141,7 +142,6 @@ def test_set_led(self):
def led():
return self.device.status().led

# The LED brightness of a Air Purifier Pro cannot be set so far.
self.device.set_led(True)
assert led() is True

Expand All @@ -168,20 +168,11 @@ def child_lock():
self.device.set_child_lock(False)
assert child_lock() is False

def test_status_without_led_b_and_with_bright(self):
self.device._reset_state()

self.device.state["bright"] = self.device.state["led_b"]
del self.device.state["led_b"]

assert self.state().led_brightness == LedBrightness(
self.device.start_state["led_b"])

def test_status_without_led_brightness_at_all(self):
def test_status_without_led_brightness(self):
self.device._reset_state()

# The Air Purifier Pro doesn't support LED brightness
self.device.state["led_b"] = None
self.device.state["bright"] = None
assert self.state().led_brightness is None

def test_status_without_temperature(self):
Expand Down