hass-core/homeassistant/components/light/demo.py

78 lines
1.8 KiB
Python
Raw Normal View History

2015-05-10 23:45:47 +02:00
"""
homeassistant.components.light.demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Demo platform that implements lights.
"""
import random
2015-06-13 16:42:09 -07:00
from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS, ATTR_XY_COLOR)
LIGHT_COLORS = [
2015-08-30 13:48:40 -07:00
[0.368, 0.180],
[0.460, 0.470],
]
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return demo lights. """
add_devices_callback([
2015-06-13 16:42:09 -07:00
DemoLight("Bed Light", False),
2015-08-30 13:48:40 -07:00
DemoLight("Ceiling Lights", True, LIGHT_COLORS[0]),
DemoLight("Kitchen Lights", True, LIGHT_COLORS[1])
])
2015-06-13 16:42:09 -07:00
class DemoLight(Light):
""" Provides a demo switch. """
def __init__(self, name, state, xy=None, brightness=180):
2015-06-13 16:42:09 -07:00
self._name = name
self._state = state
self._xy = xy or random.choice(LIGHT_COLORS)
self._brightness = brightness
@property
def should_poll(self):
""" No polling needed for a demo light. """
return False
@property
def name(self):
""" Returns the name of the device if any. """
return self._name
@property
2015-06-13 16:42:09 -07:00
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
@property
2015-06-13 16:42:09 -07:00
def color_xy(self):
""" XY color value. """
return self._xy
@property
def is_on(self):
""" True if device is on. """
2015-06-13 16:42:09 -07:00
return self._state
def turn_on(self, **kwargs):
""" Turn the device on. """
2015-06-13 16:42:09 -07:00
self._state = True
if ATTR_XY_COLOR in kwargs:
self._xy = kwargs[ATTR_XY_COLOR]
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]
2015-06-13 16:42:09 -07:00
self.update_ha_state()
def turn_off(self, **kwargs):
""" Turn the device off. """
2015-06-13 16:42:09 -07:00
self._state = False
self.update_ha_state()