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

Philips Eyecare: More safety property mapping of the device status #95

Merged
merged 5 commits into from
Oct 21, 2017
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
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