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

Air purifier: defaultdict used for safety and transparency #49

Merged
Merged
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
24 changes: 19 additions & 5 deletions mirobo/airpurifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from .device import Device
from typing import Any, Dict
import logging
import enum
from typing import Any, Dict
from collections import defaultdict
from .device import Device

_LOGGER = logging.getLogger(__name__)


class OperationMode(enum.Enum):
Expand All @@ -22,7 +26,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',
Expand All @@ -32,7 +35,17 @@ 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 AirPurifierStatus(
defaultdict(lambda: None, zip(properties, values)))

def on(self):
"""Power on."""
Expand Down Expand Up @@ -78,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:
Expand All @@ -98,7 +112,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
Expand Down