2015-05-10 21:45:47 +00:00
|
|
|
"""
|
2016-02-24 09:38:06 +00:00
|
|
|
Demo light platform that implements lights.
|
2015-05-10 21:45:47 +00:00
|
|
|
|
2016-02-24 09:38:06 +00:00
|
|
|
For more details about this platform, please refer to the documentation
|
|
|
|
https://home-assistant.io/components/demo/
|
2015-05-10 21:45:47 +00:00
|
|
|
"""
|
2015-02-28 15:31:39 +00:00
|
|
|
import random
|
|
|
|
|
2015-06-13 23:42:09 +00:00
|
|
|
from homeassistant.components.light import (
|
2018-03-18 22:00:29 +00:00
|
|
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT, ATTR_HS_COLOR,
|
|
|
|
ATTR_WHITE_VALUE, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_EFFECT,
|
|
|
|
SUPPORT_COLOR, SUPPORT_WHITE_VALUE, Light)
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
LIGHT_COLORS = [
|
2018-03-18 22:00:29 +00:00
|
|
|
(56, 86),
|
|
|
|
(345, 75),
|
2015-02-28 15:31:39 +00:00
|
|
|
]
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
LIGHT_EFFECT_LIST = ['rainbow', 'none']
|
|
|
|
|
2015-11-07 09:25:33 +00:00
|
|
|
LIGHT_TEMPS = [240, 380]
|
2015-10-27 22:45:35 +00:00
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
SUPPORT_DEMO = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_EFFECT |
|
2018-03-18 22:00:29 +00:00
|
|
|
SUPPORT_COLOR | SUPPORT_WHITE_VALUE)
|
2016-08-16 06:07:07 +00:00
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the demo light platform."""
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities_callback([
|
2018-02-24 18:53:59 +00:00
|
|
|
DemoLight(1, "Bed Light", False, True, effect_list=LIGHT_EFFECT_LIST,
|
2016-11-28 01:15:28 +00:00
|
|
|
effect=LIGHT_EFFECT_LIST[0]),
|
2018-02-24 18:53:59 +00:00
|
|
|
DemoLight(2, "Ceiling Lights", True, True,
|
2017-02-08 20:13:07 +00:00
|
|
|
LIGHT_COLORS[0], LIGHT_TEMPS[1]),
|
2018-02-24 18:53:59 +00:00
|
|
|
DemoLight(3, "Kitchen Lights", True, True,
|
2017-02-08 20:13:07 +00:00
|
|
|
LIGHT_COLORS[1], LIGHT_TEMPS[0])
|
2015-02-28 15:31:39 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
|
2015-06-13 23:42:09 +00:00
|
|
|
class DemoLight(Light):
|
2017-02-08 20:13:07 +00:00
|
|
|
"""Representation of a demo light."""
|
2016-03-07 21:08:21 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
def __init__(self, unique_id, name, state, available=False, hs_color=None,
|
|
|
|
ct=None, brightness=180, white=200, effect_list=None,
|
|
|
|
effect=None):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Initialize the light."""
|
2018-02-24 18:53:59 +00:00
|
|
|
self._unique_id = unique_id
|
2015-06-13 23:42:09 +00:00
|
|
|
self._name = name
|
2015-02-28 15:31:39 +00:00
|
|
|
self._state = state
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = hs_color
|
2015-10-27 22:45:35 +00:00
|
|
|
self._ct = ct or random.choice(LIGHT_TEMPS)
|
2015-02-28 15:31:39 +00:00
|
|
|
self._brightness = brightness
|
2016-09-21 04:26:40 +00:00
|
|
|
self._white = white
|
2016-11-28 01:15:28 +00:00
|
|
|
self._effect_list = effect_list
|
|
|
|
self._effect = effect
|
2018-03-21 01:09:34 +00:00
|
|
|
self._available = True
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2015-03-17 05:20:31 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def should_poll(self) -> bool:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""No polling needed for a demo light."""
|
2015-03-17 05:20:31 +00:00
|
|
|
return False
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def name(self) -> str:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the name of the light if any."""
|
2015-02-28 15:31:39 +00:00
|
|
|
return self._name
|
|
|
|
|
2018-02-24 18:53:59 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID for light."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return availability."""
|
|
|
|
# This demo light is always available, but well-behaving components
|
|
|
|
# should implement this to inform Home Assistant accordingly.
|
2018-03-21 01:09:34 +00:00
|
|
|
return self._available
|
2017-02-08 20:13:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self) -> int:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2015-06-13 23:42:09 +00:00
|
|
|
return self._brightness
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self) -> tuple:
|
|
|
|
"""Return the hs color value."""
|
|
|
|
return self._hs_color
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2015-10-27 22:45:35 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def color_temp(self) -> int:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return the CT color temperature."""
|
2015-10-27 22:45:35 +00:00
|
|
|
return self._ct
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def white_value(self) -> int:
|
2016-09-21 04:26:40 +00:00
|
|
|
"""Return the white value of this light between 0..255."""
|
|
|
|
return self._white
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def effect_list(self) -> list:
|
2016-11-28 01:15:28 +00:00
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return self._effect_list
|
|
|
|
|
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def effect(self) -> str:
|
2016-11-28 01:15:28 +00:00
|
|
|
"""Return the current effect."""
|
|
|
|
return self._effect
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def is_on(self) -> bool:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Return true if light is on."""
|
2015-06-13 23:42:09 +00:00
|
|
|
return self._state
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
2017-02-08 20:13:07 +00:00
|
|
|
def supported_features(self) -> int:
|
2016-08-16 06:07:07 +00:00
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_DEMO
|
|
|
|
|
2017-02-08 20:13:07 +00:00
|
|
|
def turn_on(self, **kwargs) -> None:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Turn the light on."""
|
2015-06-13 23:42:09 +00:00
|
|
|
self._state = True
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
self._hs_color = kwargs[ATTR_HS_COLOR]
|
2015-02-28 15:31:39 +00:00
|
|
|
|
2015-10-28 23:12:16 +00:00
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
self._ct = kwargs[ATTR_COLOR_TEMP]
|
2015-10-27 22:45:35 +00:00
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
|
2016-09-21 04:26:40 +00:00
|
|
|
if ATTR_WHITE_VALUE in kwargs:
|
|
|
|
self._white = kwargs[ATTR_WHITE_VALUE]
|
|
|
|
|
2016-11-28 01:15:28 +00:00
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
self._effect = kwargs[ATTR_EFFECT]
|
|
|
|
|
2017-02-08 20:13:07 +00:00
|
|
|
# As we have disabled polling, we need to inform
|
|
|
|
# Home Assistant about updates in our state ourselves.
|
2016-11-30 21:33:38 +00:00
|
|
|
self.schedule_update_ha_state()
|
2015-06-13 23:42:09 +00:00
|
|
|
|
2017-02-08 20:13:07 +00:00
|
|
|
def turn_off(self, **kwargs) -> None:
|
2016-02-24 09:38:06 +00:00
|
|
|
"""Turn the light off."""
|
2015-06-13 23:42:09 +00:00
|
|
|
self._state = False
|
2017-02-08 20:13:07 +00:00
|
|
|
|
|
|
|
# As we have disabled polling, we need to inform
|
|
|
|
# Home Assistant about updates in our state ourselves.
|
2016-11-30 21:33:38 +00:00
|
|
|
self.schedule_update_ha_state()
|