core/homeassistant/components/light/__init__.py

298 lines
9.2 KiB
Python
Raw Normal View History

"""
Provides functionality to interact with lights.
2014-03-26 07:08:50 +00:00
2015-11-09 12:12:18 +00:00
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/light/
"""
import logging
2014-03-26 07:08:50 +00:00
import os
import csv
import voluptuous as vol
2016-01-19 13:10:17 +00:00
from homeassistant.components import (
group, discovery, wemo, wink, isy994,
2016-03-15 09:17:09 +00:00
zwave, insteon_hub, mysensors, tellstick, vera)
2015-09-27 06:17:04 +00:00
from homeassistant.config import load_yaml_config_file
from homeassistant.const import (
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
ATTR_ENTITY_ID)
2015-06-13 23:42:09 +00:00
from homeassistant.helpers.entity import ToggleEntity
2015-09-27 06:17:04 +00:00
from homeassistant.helpers.entity_component import EntityComponent
2016-03-28 01:48:51 +00:00
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
import homeassistant.helpers.config_validation as cv
2015-07-07 07:01:17 +00:00
import homeassistant.util.color as color_util
DOMAIN = "light"
2015-03-01 09:35:58 +00:00
SCAN_INTERVAL = 30
GROUP_NAME_ALL_LIGHTS = 'all lights'
ENTITY_ID_ALL_LIGHTS = group.ENTITY_ID_FORMAT.format('all_lights')
ENTITY_ID_FORMAT = DOMAIN + ".{}"
2016-03-07 21:08:21 +00:00
# Integer that represents transition time in seconds to make change.
ATTR_TRANSITION = "transition"
2016-03-07 21:08:21 +00:00
# Lists holding color values
ATTR_RGB_COLOR = "rgb_color"
ATTR_XY_COLOR = "xy_color"
ATTR_COLOR_TEMP = "color_temp"
2016-03-07 21:08:21 +00:00
# int with value 0 .. 255 representing brightness of the light.
ATTR_BRIGHTNESS = "brightness"
2016-03-07 21:08:21 +00:00
# String representing a profile (built-in ones or external defined).
2014-03-26 07:08:50 +00:00
ATTR_PROFILE = "profile"
2016-03-07 21:08:21 +00:00
# If the light should flash, can be FLASH_SHORT or FLASH_LONG.
ATTR_FLASH = "flash"
FLASH_SHORT = "short"
FLASH_LONG = "long"
2016-03-07 21:08:21 +00:00
# Apply an effect to the light, can be EFFECT_COLORLOOP.
ATTR_EFFECT = "effect"
EFFECT_COLORLOOP = "colorloop"
EFFECT_RANDOM = "random"
2015-11-01 15:04:23 +00:00
EFFECT_WHITE = "white"
2014-03-26 07:08:50 +00:00
LIGHT_PROFILES_FILE = "light_profiles.csv"
2016-03-07 21:08:21 +00:00
# Maps discovered services to their platforms.
DISCOVERY_PLATFORMS = {
wemo.DISCOVER_LIGHTS: 'wemo',
wink.DISCOVER_LIGHTS: 'wink',
2016-01-30 06:05:09 +00:00
insteon_hub.DISCOVER_LIGHTS: 'insteon_hub',
isy994.DISCOVER_LIGHTS: 'isy994',
discovery.SERVICE_HUE: 'hue',
2015-11-07 14:56:28 +00:00
zwave.DISCOVER_LIGHTS: 'zwave',
mysensors.DISCOVER_LIGHTS: 'mysensors',
tellstick.DISCOVER_LIGHTS: 'tellstick',
2016-03-15 09:17:09 +00:00
vera.DISCOVER_LIGHTS: 'vera',
}
2015-06-13 23:42:09 +00:00
PROP_TO_ATTR = {
'brightness': ATTR_BRIGHTNESS,
'color_temp': ATTR_COLOR_TEMP,
'rgb_color': ATTR_RGB_COLOR,
'xy_color': ATTR_XY_COLOR,
2015-06-13 23:42:09 +00:00
}
# Service call validation schemas
VALID_TRANSITION = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=900))
LIGHT_TURN_ON_SCHEMA = vol.Schema({
ATTR_ENTITY_ID: cv.entity_ids,
ATTR_PROFILE: str,
ATTR_TRANSITION: VALID_TRANSITION,
ATTR_BRIGHTNESS: cv.byte,
ATTR_RGB_COLOR: vol.All(vol.ExactSequence((cv.byte, cv.byte, cv.byte)),
vol.Coerce(tuple)),
ATTR_XY_COLOR: vol.All(vol.ExactSequence((cv.small_float, cv.small_float)),
vol.Coerce(tuple)),
ATTR_COLOR_TEMP: vol.All(int, vol.Range(min=154, max=500)),
ATTR_FLASH: vol.In([FLASH_SHORT, FLASH_LONG]),
ATTR_EFFECT: vol.In([EFFECT_COLORLOOP, EFFECT_RANDOM, EFFECT_WHITE]),
})
LIGHT_TURN_OFF_SCHEMA = vol.Schema({
ATTR_ENTITY_ID: cv.entity_ids,
ATTR_TRANSITION: VALID_TRANSITION,
})
LIGHT_TOGGLE_SCHEMA = vol.Schema({
ATTR_ENTITY_ID: cv.entity_ids,
ATTR_TRANSITION: VALID_TRANSITION,
})
PROFILE_SCHEMA = vol.Schema(
vol.ExactSequence((str, cv.small_float, cv.small_float, cv.byte))
)
2014-11-09 23:12:23 +00:00
_LOGGER = logging.getLogger(__name__)
def is_on(hass, entity_id=None):
2016-03-07 21:08:21 +00:00
"""Return if the lights are on based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_LIGHTS
return hass.states.is_state(entity_id, STATE_ON)
# pylint: disable=too-many-arguments
def turn_on(hass, entity_id=None, transition=None, brightness=None,
rgb_color=None, xy_color=None, color_temp=None, profile=None,
flash=None, effect=None):
2016-03-07 21:08:21 +00:00
"""Turn all or specified light on."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_PROFILE, profile),
(ATTR_TRANSITION, transition),
(ATTR_BRIGHTNESS, brightness),
(ATTR_RGB_COLOR, rgb_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
] if value is not None
}
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
def turn_off(hass, entity_id=None, transition=None):
2016-03-07 21:08:21 +00:00
"""Turn all or specified light off."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
}
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
def toggle(hass, entity_id=None, transition=None):
2016-03-07 21:08:21 +00:00
"""Toggle all or specified light."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
}
hass.services.call(DOMAIN, SERVICE_TOGGLE, data)
2015-11-02 22:51:17 +00:00
# pylint: disable=too-many-branches, too-many-locals, too-many-statements
def setup(hass, config):
2016-03-07 21:08:21 +00:00
"""Expose light control via statemachine and services."""
component = EntityComponent(
2015-03-01 09:35:58 +00:00
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, DISCOVERY_PLATFORMS,
GROUP_NAME_ALL_LIGHTS)
component.setup(config)
2014-11-26 07:16:07 +00:00
# Load built-in profiles and custom profiles
profile_paths = [os.path.join(os.path.dirname(__file__),
LIGHT_PROFILES_FILE),
hass.config.path(LIGHT_PROFILES_FILE)]
2014-11-26 07:16:07 +00:00
profiles = {}
for profile_path in profile_paths:
2015-08-03 15:42:28 +00:00
if not os.path.isfile(profile_path):
continue
with open(profile_path) as inp:
reader = csv.reader(inp)
# Skip the header
next(reader, None)
try:
for rec in reader:
profile, color_x, color_y, brightness = PROFILE_SCHEMA(rec)
profiles[profile] = (color_x, color_y, brightness)
except vol.MultipleInvalid as ex:
_LOGGER.error("Error parsing light profile from %s: %s",
profile_path, ex)
2015-08-03 15:42:28 +00:00
return False
2014-11-26 07:16:07 +00:00
def handle_light_service(service):
2016-03-07 21:08:21 +00:00
"""Hande a turn light on or off service call."""
# Get the validated data
params = service.data.copy()
# Convert the entity ids to valid light ids
target_lights = component.extract_from_service(service)
params.pop(ATTR_ENTITY_ID, None)
2014-11-26 05:28:43 +00:00
service_fun = None
if service.service == SERVICE_TURN_OFF:
service_fun = 'turn_off'
elif service.service == SERVICE_TOGGLE:
service_fun = 'toggle'
if service_fun:
for light in target_lights:
getattr(light, service_fun)(**params)
2015-08-04 16:13:55 +00:00
for light in target_lights:
if light.should_poll:
2015-08-03 15:42:28 +00:00
light.update_ha_state(True)
return
2016-03-07 21:08:21 +00:00
# Processing extra data for turn light on request.
profile = profiles.get(params.pop(ATTR_PROFILE, None))
2015-08-03 15:42:28 +00:00
if profile:
params.setdefault(ATTR_XY_COLOR, profile[:2])
params.setdefault(ATTR_BRIGHTNESS, profile[2])
2015-08-03 15:42:28 +00:00
for light in target_lights:
light.turn_on(**params)
for light in target_lights:
2015-06-13 23:42:09 +00:00
if light.should_poll:
light.update_ha_state(True)
2016-03-07 21:08:21 +00:00
# Listen for light on and light off service calls.
2015-09-27 06:17:04 +00:00
descriptions = load_yaml_config_file(
os.path.join(os.path.dirname(__file__), 'services.yaml'))
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_light_service,
descriptions.get(SERVICE_TURN_ON),
schema=LIGHT_TURN_ON_SCHEMA)
2015-09-27 06:17:04 +00:00
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_light_service,
descriptions.get(SERVICE_TURN_OFF),
schema=LIGHT_TURN_OFF_SCHEMA)
hass.services.register(DOMAIN, SERVICE_TOGGLE, handle_light_service,
descriptions.get(SERVICE_TOGGLE),
schema=LIGHT_TOGGLE_SCHEMA)
return True
2015-06-13 23:42:09 +00:00
class Light(ToggleEntity):
2016-03-07 21:08:21 +00:00
"""Representation of a light."""
2015-06-13 23:42:09 +00:00
2016-03-07 21:08:21 +00:00
# pylint: disable=no-self-use
2015-06-13 23:42:09 +00:00
@property
def brightness(self):
2016-03-07 21:08:21 +00:00
"""Return the brightness of this light between 0..255."""
2015-06-13 23:42:09 +00:00
return None
@property
def xy_color(self):
2016-03-07 21:08:21 +00:00
"""Return the XY color value [float, float]."""
2015-06-13 23:42:09 +00:00
return None
@property
def rgb_color(self):
2016-03-07 21:08:21 +00:00
"""Return the RGB color value [int, int, int]."""
return None
@property
def color_temp(self):
2016-03-20 02:44:20 +00:00
"""Return the CT color value in mireds."""
return None
2015-06-13 23:42:09 +00:00
@property
def state_attributes(self):
2016-03-07 21:08:21 +00:00
"""Return optional state attributes."""
2015-06-13 23:42:09 +00:00
data = {}
if self.is_on:
for prop, attr in PROP_TO_ATTR.items():
value = getattr(self, prop)
if value:
data[attr] = value
if ATTR_RGB_COLOR not in data and ATTR_XY_COLOR in data and \
ATTR_BRIGHTNESS in data:
data[ATTR_RGB_COLOR] = color_util.color_xy_brightness_to_RGB(
data[ATTR_XY_COLOR][0], data[ATTR_XY_COLOR][1],
data[ATTR_BRIGHTNESS])
2015-06-13 23:42:09 +00:00
return data