Skip to content

Commit

Permalink
add support for AirQualityMonitor:cgllc.airmonitor.s1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumuht committed Aug 10, 2019
1 parent 3a9c16e commit dfa0f70
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
60 changes: 55 additions & 5 deletions miio/airqualitymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@

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

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

AVAILABLE_PROPERTIES_S1 = [
'battery', 'co2', 'humidity', 'pm25', 'temperature', 'tvoc'
]

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


Expand All @@ -39,6 +45,11 @@ def __init__(self, data):
Response of a Xiaomi Air Quality Monitor (cgllc.airmonitor.b1):
unknown.
Response of a Xiaomi Air Quality Monitor (cgllc.airmonitor.s1):
{'battery': 100, 'co2': 695, 'humidity': 62.1, 'pm25': 19.4, 'temperature': 27.4,
'tvoc': 254}
"""
self.data = data

Expand Down Expand Up @@ -92,16 +103,51 @@ def sensor_state(self) -> str:
"""Sensor state."""
return self.data["sensor_state"]

@property
def co2(self) -> int:
"""Return co2 value for MODEL_AIRQUALITYMONITOR_S1."""
return self.data["co2"]

@property
def humidity(self) -> float:
"""Return humidity value for MODEL_AIRQUALITYMONITOR_S1."""
return self.data["humidity"]

@property
def pm25(self) -> float:
"""Return pm2.5 value for MODEL_AIRQUALITYMONITOR_S1."""
return self.data["pm25"]

@property
def temperature(self) -> float:
"""Return temperature value for MODEL_AIRQUALITYMONITOR_S1."""
return self.data["temperature"]

@property
def tvoc(self) -> int:
"""Return tvoc value for MODEL_AIRQUALITYMONITOR_S1."""
return self.data["tvoc"]

def __repr__(self) -> str:
s = "<AirQualityMonitorStatus power=%s, " \
"aqi=%s, " \
"battery=%s, " \
"usb_power=%s, " \
"temperature=%s, " \
"humidity=%s, " \
"co2=%s, " \
"pm2.5=%s, " \
"tvoc=%s, " \
"display_clock=%s>" % \
(self.power,
self.aqi,
self.battery,
self.usb_power,
self.temperature,
self.humidity,
self.co2,
self.pm25,
self.tvoc,
self.display_clock)
return s

Expand All @@ -113,17 +159,19 @@ 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:
model: str = None) -> None:
super().__init__(ip, token, start_id, debug, lazy_discover)

self.device_info = self.info()
if self.device_info and model is None:
model = self.device_info.model

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 Down Expand Up @@ -152,8 +200,10 @@ def status(self) -> AirQualityMonitorStatus:
"count (%s) of received values.",
properties_count, values_count)

return AirQualityMonitorStatus(
defaultdict(lambda: None, zip(properties, values)))
if self.model == MODEL_AIRQUALITYMONITOR_S1:
return AirQualityMonitorStatus(defaultdict(lambda: None, values))
else:
return AirQualityMonitorStatus(defaultdict(lambda: None, zip(properties, values)))

@command(
default_output=format_output("Powering on"),
Expand Down
4 changes: 3 additions & 1 deletion miio/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
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 .airqualitymonitor import (MODEL_AIRQUALITYMONITOR_V1, MODEL_AIRQUALITYMONITOR_B1,
MODEL_AIRQUALITYMONITOR_S1, )
from .airhumidifier import (MODEL_HUMIDIFIER_CB1, 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,
Expand Down Expand Up @@ -91,6 +92,7 @@
"zhimi-airfresh-va2": AirFresh,
"zhimi-airmonitor-v1": partial(AirQualityMonitor, model=MODEL_AIRQUALITYMONITOR_V1),
"cgllc-airmonitor-b1": partial(AirQualityMonitor, model=MODEL_AIRQUALITYMONITOR_B1),
"cgllc-airmonitor-s1": partial(AirQualityMonitor, model=MODEL_AIRQUALITYMONITOR_S1),
"lumi-gateway-": lambda x: other_package_info(
x, "https://github.com/Danielhiversen/PyXiaomiGateway")
} # type: Dict[str, Union[Callable, Device]]
Expand Down

0 comments on commit dfa0f70

Please sign in to comment.