From dfa0f703cab12faa536e787d6356451cb18ab99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=B5=B7=E6=B6=9B?= Date: Fri, 9 Aug 2019 23:16:13 +0800 Subject: [PATCH 1/2] add support for AirQualityMonitor:cgllc.airmonitor.s1 --- miio/airqualitymonitor.py | 60 +++++++++++++++++++++++++++++++++++---- miio/discovery.py | 4 ++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/miio/airqualitymonitor.py b/miio/airqualitymonitor.py index 9eec1287c..8cab7f409 100644 --- a/miio/airqualitymonitor.py +++ b/miio/airqualitymonitor.py @@ -10,6 +10,7 @@ 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', @@ -17,9 +18,14 @@ '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, } @@ -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 @@ -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 = "" % \ (self.power, self.aqi, self.battery, self.usb_power, + self.temperature, + self.humidity, + self.co2, + self.pm25, + self.tvoc, self.display_clock) return s @@ -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( "", @@ -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"), diff --git a/miio/discovery.py b/miio/discovery.py index 9514f30e6..9f9e97e2d 100644 --- a/miio/discovery.py +++ b/miio/discovery.py @@ -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, @@ -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]] From 75a5006632f5eed5c4926a860ff17ebaf13ea0dc Mon Sep 17 00:00:00 2001 From: "zhumu.zht" Date: Mon, 12 Aug 2019 00:42:38 +0800 Subject: [PATCH 2/2] update docstrings to the new properties for cgllc.airmonitor.s1 --- miio/airqualitymonitor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/miio/airqualitymonitor.py b/miio/airqualitymonitor.py index 8cab7f409..85439f521 100644 --- a/miio/airqualitymonitor.py +++ b/miio/airqualitymonitor.py @@ -105,22 +105,22 @@ def sensor_state(self) -> str: @property def co2(self) -> int: - """Return co2 value for MODEL_AIRQUALITYMONITOR_S1.""" + """Return co2 value (400...9999)ppm for MODEL_AIRQUALITYMONITOR_S1.""" return self.data["co2"] @property def humidity(self) -> float: - """Return humidity value for MODEL_AIRQUALITYMONITOR_S1.""" + """Return humidity value (0...100)% for MODEL_AIRQUALITYMONITOR_S1.""" return self.data["humidity"] @property def pm25(self) -> float: - """Return pm2.5 value for MODEL_AIRQUALITYMONITOR_S1.""" + """Return pm2.5 value (0...999)μg/m³ for MODEL_AIRQUALITYMONITOR_S1.""" return self.data["pm25"] @property def temperature(self) -> float: - """Return temperature value for MODEL_AIRQUALITYMONITOR_S1.""" + """Return temperature value (-10...50)°C for MODEL_AIRQUALITYMONITOR_S1.""" return self.data["temperature"] @property