From 208f3897ae41d69727112841ed753f497c9a9d08 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 3 Sep 2017 07:08:49 +0200 Subject: [PATCH 1/3] defaultdict used for safety and transparency. --- mirobo/airpurifier.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mirobo/airpurifier.py b/mirobo/airpurifier.py index a03af1322..c3bf0127b 100644 --- a/mirobo/airpurifier.py +++ b/mirobo/airpurifier.py @@ -1,7 +1,10 @@ +import logging from .device import Device from typing import Any, Dict import enum +_LOGGER = logging.getLogger(__name__) + class OperationMode(enum.Enum): Auto = 'auto' @@ -22,7 +25,6 @@ class AirPurifier(Device): def status(self): """Retrieve properties.""" - # A few more properties: properties = ['power', 'aqi', 'humidity', 'temp_dec', 'mode', 'led', 'led_b', 'buzzer', 'child_lock', 'bright', 'favorite_level', 'filter1_life', @@ -32,7 +34,16 @@ def status(self): "get_prop", properties ) - return AirPurifierStatus(dict(zip(properties, values))) + + 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 CeilStatus(defaultdict(lambda: None, zip(properties, values))) def on(self): """Power on.""" @@ -98,7 +109,7 @@ def __init__(self, data: Dict[str, Any]) -> None: 'act_det': null, 'f1_hour_used': 680 ] use_time and motor1_speed is missing because a request is limitted - to 16 properties. + to 16 properties. We request 15 properties at the moment. """ self.data = data From 5ef73ac9028f80c58bf7a87cd1a5cdf9abec4e72 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 3 Sep 2017 07:13:37 +0200 Subject: [PATCH 2/3] Slips of the pen fixed. --- mirobo/airpurifier.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mirobo/airpurifier.py b/mirobo/airpurifier.py index c3bf0127b..49a6214c6 100644 --- a/mirobo/airpurifier.py +++ b/mirobo/airpurifier.py @@ -1,7 +1,8 @@ import logging -from .device import Device -from typing import Any, Dict import enum +from typing import Any, Dict +from collections import defaultdict +from .device import Device _LOGGER = logging.getLogger(__name__) @@ -43,7 +44,7 @@ def status(self): "count (%s) of received values.", properties_count, values_count) - return CeilStatus(defaultdict(lambda: None, zip(properties, values))) + return AirPurifierStatus(defaultdict(lambda: None, zip(properties, values))) def on(self): """Power on.""" From 0fb611239e41da5d23d0aa078b815e682dab97fe Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sun, 3 Sep 2017 07:15:17 +0200 Subject: [PATCH 3/3] Reformat. --- mirobo/airpurifier.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mirobo/airpurifier.py b/mirobo/airpurifier.py index 49a6214c6..e10515e28 100644 --- a/mirobo/airpurifier.py +++ b/mirobo/airpurifier.py @@ -44,7 +44,8 @@ def status(self): "count (%s) of received values.", properties_count, values_count) - return AirPurifierStatus(defaultdict(lambda: None, zip(properties, values))) + return AirPurifierStatus( + defaultdict(lambda: None, zip(properties, values))) def on(self): """Power on.""" @@ -90,6 +91,7 @@ def set_buzzer(self, buzzer: bool): class AirPurifierStatus: """Container for status reports from the air purifier.""" + def __init__(self, data: Dict[str, Any]) -> None: """ Response of a Air Purifier Pro: