From a8d241d91a185a0dd75b7f89893f487dadacc927 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 21 Oct 2017 16:16:56 +0200 Subject: [PATCH] Philips Eyecare: More safety property mapping of the device status (#95) * More safety property mapping of the device status. * Some clean-up. * Type hint for status method added. * Order of classes interchanged. * Status method moved. --- miio/philips_eyecare.py | 157 ++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 72 deletions(-) diff --git a/miio/philips_eyecare.py b/miio/philips_eyecare.py index b8d19cc0e..594ed436e 100644 --- a/miio/philips_eyecare.py +++ b/miio/philips_eyecare.py @@ -1,10 +1,95 @@ +import logging from .device import Device from typing import Any, Dict +from collections import defaultdict + +_LOGGER = logging.getLogger(__name__) + + +class PhilipsEyecareStatus: + """Container for status reports from Xiaomi Philips Eyecare Smart Lamp 2""" + + def __init__(self, data: Dict[str, Any]) -> None: + # ['power': 'off', 'bright': 5, 'notifystatus': 'off', + # 'ambstatus': 'off': 'ambvalue': 41, 'eyecare': 'on', + # 'scene_num': 3, 'bls': 'on', 'dvalue': 0] + self.data = data + + @property + def power(self) -> str: + return self.data["power"] + + @property + def is_on(self) -> bool: + return self.power == "on" + + @property + def brightness(self) -> int: + return self.data["bright"] + + @property + def reminder(self) -> bool: + return self.data["notifystatus"] == "on" + + @property + def ambient(self) -> bool: + return self.data["ambstatus"] == "on" + + @property + def ambient_brightness(self) -> int: + return self.data["ambvalue"] + + @property + def eyecare(self) -> bool: + return self.data["eyecare"] == "on" + + @property + def scene(self) -> int: + return self.data["scene_num"] + + @property + def smart_night_light(self) -> bool: + return self.data["bls"] == "on" + + @property + def delay_off_countdown(self) -> int: + return self.data["dvalue"] + + def __str__(self) -> str: + s = "" % \ + (self.power, self.brightness, + self.reminder, self.ambient, self.ambient_brightness, + self.eyecare, self.scene, self.smart_night_light, + self.delay_off_countdown) + return s class PhilipsEyecare(Device): """Main class representing Xiaomi Philips Eyecare Smart Lamp 2.""" + def status(self) -> PhilipsEyecareStatus: + """Retrieve properties.""" + properties = ['power', 'bright', 'notifystatus', 'ambstatus', + 'ambvalue', 'eyecare', 'scene_num', 'bls', + 'dvalue', ] + values = self.send( + "get_prop", + properties + ) + 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 PhilipsEyecareStatus( + defaultdict(lambda: None, zip(properties, values))) + def on(self): """Power on.""" return self.send("set_power", ["on"]) @@ -60,75 +145,3 @@ def ambient_off(self): def set_ambient_brightness(self, level: int): """Set Ambient Light brightness level.""" return self.send("set_amb_bright", [level]) - - def status(self): - """Retrieve properties.""" - properties = ['power', 'bright', 'notifystatus', 'ambstatus', - 'ambvalue', 'eyecare', 'scene_num', 'bls', - 'dvalue', ] - values = self.send( - "get_prop", - properties - ) - return PhilipsEyecareStatus(dict(zip(properties, values))) - - -class PhilipsEyecareStatus: - """Container for status reports from Xiaomi Philips Eyecare Smart Lamp 2""" - - def __init__(self, data: Dict[str, Any]) -> None: - # ["power","bright","notifystatus","ambstatus","ambvalue","eyecare", - # "scene_num","bls","dvalue"]} - # ["off",5,"off","off",41,"on",3,"on",0] - self.data = data - - @property - def power(self) -> str: - return self.data["power"] - - @property - def is_on(self) -> bool: - return self.power == "on" - - @property - def brightness(self) -> int: - return self.data["bright"] - - @property - def reminder(self) -> bool: - return self.data["notifystatus"] == "on" - - @property - def ambient(self) -> bool: - return self.data["ambstatus"] == "on" - - @property - def ambient_brightness(self) -> int: - return self.data["ambvalue"] - - @property - def eyecare(self) -> bool: - return self.data["eyecare"] == "on" - - @property - def scene(self) -> str: - return self.data["scene_num"] - - @property - def smart_night_light(self) -> bool: - return self.data["bls"] == "on" - - @property - def delay_off_countdown(self) -> int: - return self.data["dvalue"] - - def __str__(self) -> str: - s = "" % \ - (self.power, self.brightness, - self.reminder, self.ambient, self.ambient_brightness, - self.eyecare, self.scene, self.smart_night_light, - self.delay_off_countdown) - return s