Add available property and typing hints (#5593)

* light.demo: add available property, add typing hints

* light.demo: keep all lights available, fix init ordering

* Fix issues raised during review

* Update demo.py
This commit is contained in:
Teemu R 2017-02-08 21:13:07 +01:00 committed by Paulus Schoutsen
parent 4b15946a9b
commit 061985bc65

View file

@ -28,19 +28,21 @@ SUPPORT_DEMO = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_EFFECT |
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup the demo light platform.""" """Setup the demo light platform."""
add_devices_callback([ add_devices_callback([
DemoLight("Bed Light", False, effect_list=LIGHT_EFFECT_LIST, DemoLight("Bed Light", False, True, effect_list=LIGHT_EFFECT_LIST,
effect=LIGHT_EFFECT_LIST[0]), effect=LIGHT_EFFECT_LIST[0]),
DemoLight("Ceiling Lights", True, LIGHT_COLORS[0], LIGHT_TEMPS[1]), DemoLight("Ceiling Lights", True, True,
DemoLight("Kitchen Lights", True, LIGHT_COLORS[1], LIGHT_TEMPS[0]) LIGHT_COLORS[0], LIGHT_TEMPS[1]),
DemoLight("Kitchen Lights", True, True,
LIGHT_COLORS[1], LIGHT_TEMPS[0])
]) ])
class DemoLight(Light): class DemoLight(Light):
"""Represenation of a demo light.""" """Representation of a demo light."""
def __init__( def __init__(self, name, state, available=False, rgb=None, ct=None,
self, name, state, rgb=None, ct=None, brightness=180, brightness=180, xy_color=(.5, .5), white=200,
xy_color=(.5, .5), white=200, effect_list=None, effect=None): effect_list=None, effect=None):
"""Initialize the light.""" """Initialize the light."""
self._name = name self._name = name
self._state = state self._state = state
@ -53,61 +55,68 @@ class DemoLight(Light):
self._effect = effect self._effect = effect
@property @property
def should_poll(self): def should_poll(self) -> bool:
"""No polling needed for a demo light.""" """No polling needed for a demo light."""
return False return False
@property @property
def name(self): def name(self) -> str:
"""Return the name of the light if any.""" """Return the name of the light if any."""
return self._name return self._name
@property @property
def brightness(self): def available(self) -> bool:
"""Return availability."""
# This demo light is always available, but well-behaving components
# should implement this to inform Home Assistant accordingly.
return True
@property
def brightness(self) -> int:
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
return self._brightness return self._brightness
@property @property
def xy_color(self): def xy_color(self) -> tuple:
"""Return the XY color value [float, float].""" """Return the XY color value [float, float]."""
return self._xy_color return self._xy_color
@property @property
def rgb_color(self): def rgb_color(self) -> tuple:
"""Return the RBG color value.""" """Return the RBG color value."""
return self._rgb return self._rgb
@property @property
def color_temp(self): def color_temp(self) -> int:
"""Return the CT color temperature.""" """Return the CT color temperature."""
return self._ct return self._ct
@property @property
def white_value(self): def white_value(self) -> int:
"""Return the white value of this light between 0..255.""" """Return the white value of this light between 0..255."""
return self._white return self._white
@property @property
def effect_list(self): def effect_list(self) -> list:
"""Return the list of supported effects.""" """Return the list of supported effects."""
return self._effect_list return self._effect_list
@property @property
def effect(self): def effect(self) -> str:
"""Return the current effect.""" """Return the current effect."""
return self._effect return self._effect
@property @property
def is_on(self): def is_on(self) -> bool:
"""Return true if light is on.""" """Return true if light is on."""
return self._state return self._state
@property @property
def supported_features(self): def supported_features(self) -> int:
"""Flag supported features.""" """Flag supported features."""
return SUPPORT_DEMO return SUPPORT_DEMO
def turn_on(self, **kwargs): def turn_on(self, **kwargs) -> None:
"""Turn the light on.""" """Turn the light on."""
self._state = True self._state = True
@ -129,9 +138,14 @@ class DemoLight(Light):
if ATTR_EFFECT in kwargs: if ATTR_EFFECT in kwargs:
self._effect = kwargs[ATTR_EFFECT] self._effect = kwargs[ATTR_EFFECT]
# As we have disabled polling, we need to inform
# Home Assistant about updates in our state ourselves.
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self, **kwargs): def turn_off(self, **kwargs) -> None:
"""Turn the light off.""" """Turn the light off."""
self._state = False self._state = False
# As we have disabled polling, we need to inform
# Home Assistant about updates in our state ourselves.
self.schedule_update_ha_state() self.schedule_update_ha_state()