2015-02-28 15:31:39 +00:00
|
|
|
""" Provides demo lights. """
|
|
|
|
import random
|
|
|
|
|
2015-03-22 02:16:13 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2015-03-01 04:10:39 +00:00
|
|
|
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
|
2015-02-28 15:31:39 +00:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_XY_COLOR
|
|
|
|
|
|
|
|
|
|
|
|
LIGHT_COLORS = [
|
|
|
|
[0.861, 0.3259],
|
|
|
|
[0.6389, 0.3028],
|
|
|
|
[0.1684, 0.0416]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|
|
|
""" Find and return demo lights. """
|
|
|
|
add_devices_callback([
|
|
|
|
DemoLight("Bed Light", STATE_OFF),
|
|
|
|
DemoLight("Ceiling", STATE_ON),
|
|
|
|
DemoLight("Kitchen", STATE_ON)
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2015-03-22 02:16:13 +00:00
|
|
|
class DemoLight(ToggleEntity):
|
2015-02-28 15:31:39 +00:00
|
|
|
""" Provides a demo switch. """
|
|
|
|
def __init__(self, name, state, xy=None, brightness=180):
|
|
|
|
self._name = name or DEVICE_DEFAULT_NAME
|
|
|
|
self._state = state
|
|
|
|
self._xy = xy or random.choice(LIGHT_COLORS)
|
|
|
|
self._brightness = brightness
|
|
|
|
|
2015-03-17 05:20:31 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
""" No polling needed for a demo light. """
|
|
|
|
return False
|
|
|
|
|
2015-02-28 15:31:39 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the device if any. """
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the name of the device if any. """
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
""" Returns optional state attributes. """
|
|
|
|
if self.is_on:
|
2015-03-01 04:10:39 +00:00
|
|
|
return {
|
|
|
|
ATTR_BRIGHTNESS: self._brightness,
|
|
|
|
ATTR_XY_COLOR: self._xy,
|
|
|
|
}
|
2015-02-28 15:31:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" True if device is on. """
|
|
|
|
return self._state == STATE_ON
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
""" Turn the device on. """
|
|
|
|
self._state = STATE_ON
|
|
|
|
|
|
|
|
if ATTR_XY_COLOR in kwargs:
|
|
|
|
self._xy = kwargs[ATTR_XY_COLOR]
|
|
|
|
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
""" Turn the device off. """
|
|
|
|
self._state = STATE_OFF
|