-
-
Notifications
You must be signed in to change notification settings - Fork 568
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
Changes from 3 commits
6e083cc
204d0d3
552327a
0d2b4e8
aaa7c4e
15586f7
51d381b
cdfe82c
3eb5c88
000d06e
02e1e53
55b2f0a
29562b5
ce2c387
ec258ba
b3b7e8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,10 @@ | |
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class AirHumidifierException(Exception): | ||
pass | ||
|
||
|
||
class OperationMode(enum.Enum): | ||
Silent = 'silent' | ||
Medium = 'medium' | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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", | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. It's more like a "fan speed". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm waiting for some details currently: https://community.home-assistant.io/t/xiaomi-humidifier-support/35672/26 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (83 > 79 characters) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(): | ||
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line at end of file |
There was a problem hiding this comment.
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.