From ecc890c806f3fd5823986e221555f5c07113e7c0 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 8 Oct 2018 21:34:16 +0200 Subject: [PATCH] Add property LED to the Xiaomi Air Fresh --- miio/airfresh.py | 22 ++++++++++++++++++++++ miio/tests/test_airfresh.py | 14 +++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/miio/airfresh.py b/miio/airfresh.py index ce7adf22b..8007cd3fe 100644 --- a/miio/airfresh.py +++ b/miio/airfresh.py @@ -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.""" @@ -137,6 +142,7 @@ def __repr__(self) -> str: "humidity=%s%%, " \ "co2=%s, " \ "mode=%s, " \ + "led=%s, " \ "led_brightness=%s, " \ "buzzer=%s, " \ "child_lock=%s, " \ @@ -152,6 +158,7 @@ def __repr__(self) -> str: self.humidity, self.co2, self.mode, + self.led, self.led_brightness, self.buzzer, self.child_lock, @@ -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" @@ -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( diff --git a/miio/tests/test_airfresh.py b/miio/tests/test_airfresh.py index aa200f445..5259a2129 100644 --- a/miio/tests/test_airfresh.py +++ b/miio/tests/test_airfresh.py @@ -28,7 +28,7 @@ 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, @@ -36,6 +36,7 @@ def __init__(self, *args, **kwargs): '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]), @@ -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') @@ -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