Skip to content

Commit

Permalink
Philips Eyecare: More safety property mapping of the device status (#95)
Browse files Browse the repository at this point in the history
* More safety property mapping of the device status.

* Some clean-up.

* Type hint for status method added.

* Order of classes interchanged.

* Status method moved.
  • Loading branch information
syssi authored and rytilahti committed Oct 21, 2017
1 parent 453cc70 commit a8d241d
Showing 1 changed file with 85 additions and 72 deletions.
157 changes: 85 additions & 72 deletions miio/philips_eyecare.py
Original file line number Diff line number Diff line change
@@ -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 = "<PhilipsEyecareStatus power=%s, brightness=%s, " \
"notify=%s, ambient=%s, ambient_brightness=%s, " \
"eyecare=%s, scene=%s, smart_night_light=%s, " \
"delay_off_countdown=%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"])
Expand Down Expand Up @@ -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 = "<PhilipsEyecareStatus power=%s, brightness=%s, " \
"notify=%s, ambient=%s, ambient_brightness=%s, " \
"eyecare=%s, scene=%s, smart_night_light=%s, " \
"delay_off_countdown=%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

0 comments on commit a8d241d

Please sign in to comment.