Skip to content

Commit

Permalink
Add Xiaomi Air Quality Monitor 2gen (cgllc.airmonitor.b1) support (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jan 22, 2019
1 parent 26c2155 commit f586c80
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
43 changes: 39 additions & 4 deletions miio/airqualitymonitor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import logging
from collections import defaultdict

import click

from .click_common import command, format_output
from .device import Device, DeviceException

_LOGGER = logging.getLogger(__name__)

MODEL_AIRQUALITYMONITOR_V1 = 'zhimi.airmonitor.v1'
MODEL_AIRQUALITYMONITOR_B1 = 'cgllc.airmonitor.b1'

AVAILABLE_PROPERTIES_COMMON = [
'power', 'aqi', 'battery', 'usb_state', 'time_state',
'night_state', 'night_beg_time', 'night_end_time',
'sensor_state'
]

AVAILABLE_PROPERTIES = {
MODEL_AIRQUALITYMONITOR_V1: AVAILABLE_PROPERTIES_COMMON,
MODEL_AIRQUALITYMONITOR_B1: AVAILABLE_PROPERTIES_COMMON,
}


class AirQualityMonitorException(DeviceException):
pass
Expand All @@ -14,7 +31,15 @@ class AirQualityMonitorStatus:
"""Container of air quality monitor status."""

def __init__(self, data):
# {'power': 'on', 'aqi': 34, 'battery': 100, 'usb_state': 'off', 'time_state': 'on'}
"""
Response of a Xiaomi Air Quality Monitor (zhimi.airmonitor.v1):
{'power': 'on', 'aqi': 34, 'battery': 100, 'usb_state': 'off', 'time_state': 'on'}
Response of a Xiaomi Air Quality Monitor (cgllc.airmonitor.b1):
unknown.
"""
self.data = data

@property
Expand Down Expand Up @@ -86,6 +111,18 @@ def __json__(self):

class AirQualityMonitor(Device):
"""Xiaomi PM2.5 Air Quality Monitor."""
def __init__(self, ip: str = None, token: str = None, start_id: int = 0,
debug: int = 0, lazy_discover: bool = True,
model: str = MODEL_AIRQUALITYMONITOR_V1) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover)

if model in AVAILABLE_PROPERTIES:
self.model = model
else:
self.model = MODEL_AIRQUALITYMONITOR_V1
_LOGGER.error("Device model %s unsupported. Falling back to %s.", model, self.model)

self.device_info = None

@command(
default_output=format_output(
Expand All @@ -100,9 +137,7 @@ class AirQualityMonitor(Device):
def status(self) -> AirQualityMonitorStatus:
"""Return device status."""

properties = ['power', 'aqi', 'battery', 'usb_state', 'time_state',
'night_state', 'night_beg_time', 'night_end_time',
'sensor_state']
properties = AVAILABLE_PROPERTIES[self.model]

values = self.send(
"get_prop",
Expand Down
5 changes: 4 additions & 1 deletion miio/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
Yeelight, Fan, Cooker, AirConditioningCompanion, AirQualityMonitor, AqaraCamera)

from .airconditioningcompanion import (MODEL_ACPARTNER_V1, MODEL_ACPARTNER_V2, MODEL_ACPARTNER_V3, )
from .airqualitymonitor import (MODEL_AIRQUALITYMONITOR_V1, MODEL_AIRQUALITYMONITOR_B1, )
from .airhumidifier import (MODEL_HUMIDIFIER_CA1, MODEL_HUMIDIFIER_V1, )
from .chuangmi_plug import (MODEL_CHUANGMI_PLUG_V1, MODEL_CHUANGMI_PLUG_V2, MODEL_CHUANGMI_PLUG_V3,
MODEL_CHUANGMI_PLUG_M1, MODEL_CHUANGMI_PLUG_M3,
MODEL_CHUANGMI_PLUG_HMI205, )

from .fan import (MODEL_FAN_V2, MODEL_FAN_V3, MODEL_FAN_SA1, MODEL_FAN_ZA1, )
from .powerstrip import (MODEL_POWER_STRIP_V1, MODEL_POWER_STRIP_V2, )

Expand Down Expand Up @@ -79,7 +81,8 @@
"zhimi-fan-sa1": partial(Fan, model=MODEL_FAN_SA1),
"zhimi-fan-za1": partial(Fan, model=MODEL_FAN_ZA1),
"zhimi-airfresh-va2": AirFresh,
"zhimi-airmonitor-v1": AirQualityMonitor,
"zhimi-airmonitor-v1": partial(AirQualityMonitor, model=MODEL_AIRQUALITYMONITOR_V1),
"cgllc-airmonitor-b1": partial(AirQualityMonitor, model=MODEL_AIRQUALITYMONITOR_B1),
"lumi-gateway-": lambda x: other_package_info(
x, "https://github.com/Danielhiversen/PyXiaomiGateway")
} # type: Dict[str, Union[Callable, Device]]
Expand Down
3 changes: 2 additions & 1 deletion miio/tests/test_airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import pytest

from miio import AirQualityMonitor
from miio.airqualitymonitor import AirQualityMonitorStatus
from miio.airqualitymonitor import AirQualityMonitorStatus, MODEL_AIRQUALITYMONITOR_V1
from .dummies import DummyDevice


class DummyAirQualityMonitor(DummyDevice, AirQualityMonitor):
def __init__(self, *args, **kwargs):
self.model = MODEL_AIRQUALITYMONITOR_V1
self.state = {
'power': 'on',
'aqi': 34,
Expand Down

0 comments on commit f586c80

Please sign in to comment.