Skip to content

Commit

Permalink
Air Quality Monitor: Decrease the brightness at night (Closes: rytila…
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi committed Apr 1, 2018
1 parent 65d68f5 commit b3e124b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
73 changes: 67 additions & 6 deletions miio/airqualitymonitor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = "<AirQualityMonitorStatus power=%s, " \
"aqi=%s, " \
"battery=%s, " \
"usb_power=%s, " \
"time_state=%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",
Expand All @@ -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])
11 changes: 9 additions & 2 deletions miio/tests/test_airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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')

0 comments on commit b3e124b

Please sign in to comment.