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

Add property LED to the Xiaomi Air Fresh #390

Merged
merged 1 commit into from
Oct 8, 2018
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
22 changes: 22 additions & 0 deletions miio/airfresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def mode(self) -> OperationMode:
"""Current operation mode."""
return OperationMode(self.data["mode"])

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

@property
def led_brightness(self) -> Optional[LedBrightness]:
"""Brightness of the LED."""
Expand Down Expand Up @@ -137,6 +142,7 @@ def __repr__(self) -> str:
"humidity=%s%%, " \
"co2=%s, " \
"mode=%s, " \
"led=%s, " \
"led_brightness=%s, " \
"buzzer=%s, " \
"child_lock=%s, " \
Expand All @@ -152,6 +158,7 @@ def __repr__(self) -> str:
self.humidity,
self.co2,
self.mode,
self.led,
self.led_brightness,
self.buzzer,
self.child_lock,
Expand Down Expand Up @@ -179,6 +186,7 @@ class AirFresh(Device):
"Humidity: {result.humidity} %\n"
"CO2: {result.co2} %\n"
"Mode: {result.mode.value}\n"
"LED: {result.led}\n"
"LED brightness: {result.led_brightness}\n"
"Buzzer: {result.buzzer}\n"
"Child lock: {result.child_lock}\n"
Expand Down Expand Up @@ -237,6 +245,20 @@ def set_mode(self, mode: OperationMode):
"""Set mode."""
return self.send("set_mode", [mode.value])

@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("brightness", type=EnumType(LedBrightness, False)),
default_output=format_output(
Expand Down
14 changes: 13 additions & 1 deletion miio/tests/test_airfresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ def __init__(self, *args, **kwargs):
'filter_life': 80,
'f_hour': 3500,
'favorite_level': None,
'led': None,
'led': 'on',
}
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_buzzer': lambda x: self._set_state("buzzer", x),
'set_child_lock': lambda x: self._set_state("child_lock", x),
'set_led': lambda x: self._set_state("led", x),
'set_led_level': lambda x: self._set_state("led_level", x),
'reset_filter1': lambda x: (
self._set_state('f1_hour_used', [0]),
Expand Down Expand Up @@ -90,6 +91,7 @@ def test_status(self):
assert self.state().filter_hours_used == self.device.start_state["f1_hour_used"]
assert self.state().use_time == self.device.start_state["use_time"]
assert self.state().motor_speed == self.device.start_state["motor1_speed"]
assert self.state().led == (self.device.start_state["led"] == 'on')
assert self.state().led_brightness == LedBrightness(self.device.start_state["led_level"])
assert self.state().buzzer == (self.device.start_state["buzzer"] == 'on')
assert self.state().child_lock == (self.device.start_state["child_lock"] == 'on')
Expand Down Expand Up @@ -117,6 +119,16 @@ def mode():
self.device.set_mode(OperationMode.Strong)
assert mode() == OperationMode.Strong

def test_set_led(self):
def led():
return self.device.status().led

self.device.set_led(True)
assert led() is True

self.device.set_led(False)
assert led() is False

def test_set_led_brightness(self):
def led_brightness():
return self.device.status().led_brightness
Expand Down