From b3e124bac8e90cbb9f7d28933fcf92e4ed474f8f Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 1 Apr 2018 19:31:22 +0200 Subject: [PATCH] Air Quality Monitor: Decrease the brightness at night (Closes: #283) --- miio/airqualitymonitor.py | 73 +++++++++++++++++++++++++--- miio/tests/test_airqualitymonitor.py | 11 ++++- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/miio/airqualitymonitor.py b/miio/airqualitymonitor.py index ac3c20555..fa0800671 100644 --- a/miio/airqualitymonitor.py +++ b/miio/airqualitymonitor.py @@ -1,13 +1,19 @@ +from datetime import timedelta import logging from collections import defaultdict -from .device import Device +from .device import Device, DeviceException _LOGGER = logging.getLogger(__name__) +class AirQualityMonitorException(DeviceException): + pass + + class AirQualityMonitorStatus: """Container of air quality monitor status.""" + def __init__(self, data): # {'power': 'on', 'aqi': 34, 'battery': 100, 'usb_state': 'off', 'time_state': 'on'} self.data = data @@ -38,30 +44,53 @@ def battery(self) -> int: return self.data["battery"] @property - def time_state(self) -> bool: - """Current time state.""" + def display_clock(self) -> bool: + """Display a clock instead the AQI.""" return self.data["time_state"] == "on" + @property + def night_mode(self) -> bool: + """Return True if the night mode is on.""" + return self.data["night_state"] == "on" + + @property + def night_time_begin(self) -> str: + """Return the begin of the night time.""" + return self.data["night_beg_time"] + + @property + def night_time_end(self) -> str: + """Return the end of the night time.""" + return self.data["night_end_time"] + + @property + def sensor_state(self) -> str: + """Sensor state.""" + return self.data["sensor_state"] + def __repr__(self) -> str: s = "" % \ + "display_clock=%s>" % \ (self.power, self.aqi, self.battery, self.usb_power, - self.time_state) + self.display_clock) return s class AirQualityMonitor(Device): """Xiaomi PM2.5 Air Quality Monitor.""" + def status(self) -> AirQualityMonitorStatus: """Return device status.""" - properties = ['power', 'aqi', 'battery', 'usb_state', 'time_state'] + properties = ['power', 'aqi', 'battery', 'usb_state', 'time_state', + 'night_state', 'night_beg_time', 'night_end_time', + 'sensor_state'] values = self.send( "get_prop", @@ -86,3 +115,35 @@ def on(self): def off(self): """Power off.""" return self.send("set_power", ["off"]) + + def set_display_clock(self, display_clock: bool): + """Enable/disable displaying a clock instead the AQI.""" + if display_clock: + self.send("set_time_state", ["on"]) + else: + self.send("set_time_state", ["off"]) + + def set_auto_close(self, auto_close: bool): + """Purpose unknown.""" + if auto_close: + self.send("set_auto_close", ["on"]) + else: + self.send("set_auto_close", ["off"]) + + def set_night_mode(self, night_mode: bool): + """Decrease the brightness of the display.""" + if night_mode: + self.send("set_night_state", ["on"]) + else: + self.send("set_night_state", ["off"]) + + def set_night_time(self, begin_hour: int, begin_minute: int, + end_hour: int, end_minute: int): + """Enable night mode daily at bedtime.""" + begin = begin_hour * 3600 + begin_minute * 60 + end = end_hour * 3600 + end_minute * 60 + + if begin < 0 or begin > 86399 or end < 0 or end > 86399: + AirQualityMonitorException("Begin or/and end time invalid.") + + self.send("set_night_time", [begin, end]) diff --git a/miio/tests/test_airqualitymonitor.py b/miio/tests/test_airqualitymonitor.py index f89f7ad92..11fe312e2 100644 --- a/miio/tests/test_airqualitymonitor.py +++ b/miio/tests/test_airqualitymonitor.py @@ -14,11 +14,17 @@ def __init__(self, *args, **kwargs): 'aqi': 34, 'battery': 100, 'usb_state': 'off', - 'time_state': 'on' + 'time_state': 'on', + 'night_state': 'on', + 'night_beg_time': 'format unknown', + 'night_end_time': 'format unknown', + 'sensor_state': 'format unknown', } self.return_values = { 'get_prop': self._get_state, 'set_power': lambda x: self._set_state("power", x), + 'set_time_state': lambda x: self._set_state("time_state", x), + 'set_night_state': lambda x: self._set_state("night_state", x), } super().__init__(args, kwargs) @@ -60,4 +66,5 @@ def test_status(self): assert self.state().aqi == self.device.start_state["aqi"] assert self.state().battery == self.device.start_state["battery"] assert self.state().usb_power is (self.device.start_state["usb_state"] == 'on') - assert self.state().time_state is (self.device.start_state["time_state"] == 'on') + assert self.state().display_clock is (self.device.start_state["time_state"] == 'on') + assert self.state().night_mode is (self.device.start_state["night_state"] == 'on')