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

Device support of the Xiaomi PM2.5 Air Quality Monitor introduced #89

Merged
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
1 change: 1 addition & 0 deletions miio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from miio.chuangmi_ir import ChuangmiIr
from miio.fan import Fan
from miio.wifispeaker import WifiSpeaker
from miio.airqualitymonitor import AirQualityMonitor
from miio.yeelight import Yeelight
from miio.device import Device, DeviceException
from miio.discovery import Discovery
65 changes: 65 additions & 0 deletions miio/airqualitymonitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging
from collections import defaultdict
from .device import Device

_LOGGER = logging.getLogger(__name__)


class AirQualityMonitorStatus:
def __init__(self, data):
# ['power': 'on', 'aqi': 34, 'battery': 0, 'usb_state': 'on']
self.data = data

@property
def power(self) -> str:
return self.data["power"]

@property
def is_on(self) -> bool:
return self.power == "on"

@property
def usb_power(self) -> bool:
return self.data["usb_state"] == "on"

@property
def aqi(self) -> int:
return self.data["aqi"]

@property
def battery(self) -> int:
return self.data["battery"]


class AirQualityMonitor(Device):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def status(self):
"""Return device status."""

properties = ['power', 'aqi', 'battery', 'usb_state']

values = self.send(
"get_prop",
properties
)

properties_count = len(properties)
values_count = len(values)
if properties_count != values_count:
_LOGGER.debug(
"Count (%s) of requested properties does not match the "
"count (%s) of received values.",
properties_count, values_count)

return AirQualityMonitorStatus(
defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
return self.send("set_power", ["on"])

def off(self):
"""Power off."""
return self.send("set_power", ["off"])