2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2014-01-05 01:55:05 +00:00
|
|
|
homeassistant.components.light
|
2013-12-11 08:07:30 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to interact with lights.
|
2014-03-26 07:08:50 +00:00
|
|
|
|
|
|
|
It offers the following services:
|
|
|
|
|
|
|
|
TURN_OFF - Turns one or multiple lights off.
|
|
|
|
|
|
|
|
Supports following parameters:
|
|
|
|
- transition
|
|
|
|
Integer that represents the time the light should take to transition to
|
|
|
|
the new state.
|
|
|
|
- entity_id
|
|
|
|
String or list of strings that point at entity_ids of lights.
|
|
|
|
|
|
|
|
TURN_ON - Turns one or multiple lights on and change attributes.
|
|
|
|
|
|
|
|
Supports following parameters:
|
|
|
|
- transition
|
|
|
|
Integer that represents the time the light should take to transition to
|
|
|
|
the new state.
|
|
|
|
|
|
|
|
- entity_id
|
|
|
|
String or list of strings that point at entity_ids of lights.
|
|
|
|
|
|
|
|
- profile
|
|
|
|
String with the name of one of the built-in profiles (relax, energize,
|
|
|
|
concentrate, reading) or one of the custom profiles defined in
|
|
|
|
light_profiles.csv in the current working directory.
|
|
|
|
|
|
|
|
Light profiles define a xy color and a brightness.
|
|
|
|
|
|
|
|
If a profile is given and a brightness or xy color then the profile values
|
|
|
|
will be overwritten.
|
|
|
|
|
|
|
|
- xy_color
|
|
|
|
A list containing two floats representing the xy color you want the light
|
|
|
|
to be.
|
|
|
|
|
|
|
|
- rgb_color
|
|
|
|
A list containing three integers representing the xy color you want the
|
|
|
|
light to be.
|
|
|
|
|
|
|
|
- brightness
|
|
|
|
Integer between 0 and 255 representing how bright you want the light to be.
|
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2014-03-26 07:08:50 +00:00
|
|
|
import os
|
|
|
|
import csv
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
import homeassistant.util as util
|
2014-12-07 07:57:02 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
|
|
|
|
from homeassistant.helpers import (
|
|
|
|
extract_entity_ids, platform_devices_from_config)
|
|
|
|
from homeassistant.components import group
|
2014-03-15 07:24:28 +00:00
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
DOMAIN = "light"
|
2014-08-13 12:28:45 +00:00
|
|
|
DEPENDENCIES = []
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
GROUP_NAME_ALL_LIGHTS = 'all_lights'
|
|
|
|
ENTITY_ID_ALL_LIGHTS = group.ENTITY_ID_FORMAT.format(
|
|
|
|
GROUP_NAME_ALL_LIGHTS)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-20 07:37:40 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-03-16 22:00:59 +00:00
|
|
|
# integer that represents transition time in seconds to make change
|
|
|
|
ATTR_TRANSITION = "transition"
|
|
|
|
|
|
|
|
# lists holding color values
|
|
|
|
ATTR_RGB_COLOR = "rgb_color"
|
|
|
|
ATTR_XY_COLOR = "xy_color"
|
|
|
|
|
|
|
|
# int with value 0 .. 255 representing brightness of the light
|
|
|
|
ATTR_BRIGHTNESS = "brightness"
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
# String representing a profile (built-in ones or external defined)
|
|
|
|
ATTR_PROFILE = "profile"
|
|
|
|
|
2014-12-09 07:02:38 +00:00
|
|
|
# If the light should flash, can be FLASH_SHORT or FLASH_LONG
|
|
|
|
ATTR_FLASH = "flash"
|
|
|
|
FLASH_SHORT = "short"
|
|
|
|
FLASH_LONG = "long"
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
LIGHT_PROFILES_FILE = "light_profiles.csv"
|
|
|
|
|
2014-11-09 23:12:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def is_on(hass, entity_id=None):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Returns if the lights are on based on the statemachine. """
|
2014-01-21 07:23:02 +00:00
|
|
|
entity_id = entity_id or ENTITY_ID_ALL_LIGHTS
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-03-16 22:00:59 +00:00
|
|
|
# pylint: disable=too-many-arguments
|
2014-04-24 07:40:45 +00:00
|
|
|
def turn_on(hass, entity_id=None, transition=None, brightness=None,
|
2014-12-09 07:02:38 +00:00
|
|
|
rgb_color=None, xy_color=None, profile=None, flash=None):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Turns all or specified light on. """
|
2014-12-09 07:02:38 +00:00
|
|
|
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_FLASH, flash),
|
|
|
|
] if value is not None
|
|
|
|
}
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-01 02:42:52 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def turn_off(hass, entity_id=None, transition=None):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Turns all or specified light off. """
|
2014-12-09 07:02:38 +00:00
|
|
|
data = {
|
|
|
|
key: value for key, value in [
|
|
|
|
(ATTR_ENTITY_ID, entity_id),
|
|
|
|
(ATTR_TRANSITION, transition),
|
|
|
|
] if value is not None
|
|
|
|
}
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-12-01 02:42:52 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
# pylint: disable=too-many-branches, too-many-locals
|
2014-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Exposes light control via statemachine and services. """
|
|
|
|
|
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.get_config_path(LIGHT_PROFILES_FILE)]
|
|
|
|
profiles = {}
|
|
|
|
|
|
|
|
for profile_path in profile_paths:
|
|
|
|
|
|
|
|
if os.path.isfile(profile_path):
|
|
|
|
with open(profile_path) as inp:
|
|
|
|
reader = csv.reader(inp)
|
|
|
|
|
|
|
|
# Skip the header
|
|
|
|
next(reader, None)
|
|
|
|
|
|
|
|
try:
|
|
|
|
for profile_id, color_x, color_y, brightness in reader:
|
|
|
|
profiles[profile_id] = (float(color_x), float(color_y),
|
|
|
|
int(brightness))
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
# ValueError if not 4 values per row
|
|
|
|
# ValueError if convert to float/int failed
|
|
|
|
_LOGGER.error(
|
|
|
|
"Error parsing light profiles from %s", profile_path)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2015-01-06 07:02:41 +00:00
|
|
|
lights = platform_devices_from_config(
|
|
|
|
config, DOMAIN, hass, ENTITY_ID_FORMAT, _LOGGER)
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-12-07 07:57:02 +00:00
|
|
|
if not lights:
|
2014-11-09 23:12:23 +00:00
|
|
|
return False
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-11-09 23:12:23 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def update_lights_state(now):
|
|
|
|
""" Update the states of all the lights. """
|
2015-01-06 07:02:41 +00:00
|
|
|
for light in lights.values():
|
2014-11-09 23:12:23 +00:00
|
|
|
light.update_ha_state(hass)
|
|
|
|
|
|
|
|
update_lights_state(None)
|
|
|
|
|
|
|
|
# Track all lights in a group
|
2015-01-09 04:02:34 +00:00
|
|
|
group.Group(hass, GROUP_NAME_ALL_LIGHTS, lights.keys(), False)
|
2014-11-09 23:12:23 +00:00
|
|
|
|
2014-01-05 01:55:05 +00:00
|
|
|
def handle_light_service(service):
|
2013-12-11 08:07:30 +00:00
|
|
|
""" Hande a turn light on or off service call. """
|
2014-03-16 22:00:59 +00:00
|
|
|
# Get and validate data
|
|
|
|
dat = service.data
|
|
|
|
|
2014-03-25 03:34:35 +00:00
|
|
|
# Convert the entity ids to valid light ids
|
2015-01-06 07:02:41 +00:00
|
|
|
target_lights = [lights[entity_id] for entity_id
|
|
|
|
in extract_entity_ids(hass, service)
|
|
|
|
if entity_id in lights]
|
2014-03-25 03:34:35 +00:00
|
|
|
|
2015-01-06 07:02:41 +00:00
|
|
|
if not target_lights:
|
|
|
|
target_lights = lights.values()
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-11-26 05:28:43 +00:00
|
|
|
params = {}
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
transition = util.convert(dat.get(ATTR_TRANSITION), int)
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-11-26 05:28:43 +00:00
|
|
|
if transition is not None:
|
|
|
|
params[ATTR_TRANSITION] = transition
|
|
|
|
|
2014-03-16 22:00:59 +00:00
|
|
|
if service.service == SERVICE_TURN_OFF:
|
2015-01-06 07:02:41 +00:00
|
|
|
for light in target_lights:
|
2014-11-26 05:28:43 +00:00
|
|
|
# pylint: disable=star-args
|
|
|
|
light.turn_off(**params)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
else:
|
2014-03-16 22:00:59 +00:00
|
|
|
# Processing extra data for turn light on request
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
# We process the profile first so that we get the desired
|
|
|
|
# behavior that extra service data attributes overwrite
|
|
|
|
# profile values
|
|
|
|
profile = profiles.get(dat.get(ATTR_PROFILE))
|
|
|
|
|
|
|
|
if profile:
|
2014-11-26 05:28:43 +00:00
|
|
|
*params[ATTR_XY_COLOR], params[ATTR_BRIGHTNESS] = profile
|
2014-03-26 07:08:50 +00:00
|
|
|
|
|
|
|
if ATTR_BRIGHTNESS in dat:
|
2014-11-26 05:28:43 +00:00
|
|
|
# We pass in the old value as the default parameter if parsing
|
|
|
|
# of the new one goes wrong.
|
|
|
|
params[ATTR_BRIGHTNESS] = util.convert(
|
|
|
|
dat.get(ATTR_BRIGHTNESS), int, params.get(ATTR_BRIGHTNESS))
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
if ATTR_XY_COLOR in dat:
|
2014-03-16 22:00:59 +00:00
|
|
|
try:
|
|
|
|
# xy_color should be a list containing 2 floats
|
2014-11-26 05:28:43 +00:00
|
|
|
xycolor = dat.get(ATTR_XY_COLOR)
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-11-26 05:38:47 +00:00
|
|
|
# Without this check, a xycolor with value '99' would work
|
|
|
|
if not isinstance(xycolor, str):
|
2014-11-26 05:28:43 +00:00
|
|
|
params[ATTR_XY_COLOR] = [float(val) for val in xycolor]
|
2014-03-16 22:00:59 +00:00
|
|
|
|
|
|
|
except (TypeError, ValueError):
|
2014-04-15 06:48:00 +00:00
|
|
|
# TypeError if xy_color is not iterable
|
2014-03-16 22:00:59 +00:00
|
|
|
# ValueError if value could not be converted to float
|
|
|
|
pass
|
|
|
|
|
2014-03-26 07:08:50 +00:00
|
|
|
if ATTR_RGB_COLOR in dat:
|
2014-03-16 22:00:59 +00:00
|
|
|
try:
|
|
|
|
# rgb_color should be a list containing 3 ints
|
2014-04-15 06:48:00 +00:00
|
|
|
rgb_color = dat.get(ATTR_RGB_COLOR)
|
2014-03-16 22:00:59 +00:00
|
|
|
|
|
|
|
if len(rgb_color) == 3:
|
2014-11-26 05:28:43 +00:00
|
|
|
params[ATTR_XY_COLOR] = \
|
|
|
|
util.color_RGB_to_xy(int(rgb_color[0]),
|
|
|
|
int(rgb_color[1]),
|
|
|
|
int(rgb_color[2]))
|
2014-03-16 22:00:59 +00:00
|
|
|
|
|
|
|
except (TypeError, ValueError):
|
2014-04-15 06:48:00 +00:00
|
|
|
# TypeError if rgb_color is not iterable
|
2014-03-26 07:08:50 +00:00
|
|
|
# ValueError if not all values can be converted to int
|
2014-03-25 03:34:35 +00:00
|
|
|
pass
|
2014-03-16 22:00:59 +00:00
|
|
|
|
2014-12-09 07:02:38 +00:00
|
|
|
if ATTR_FLASH in dat:
|
|
|
|
if dat[ATTR_FLASH] == FLASH_SHORT:
|
|
|
|
params[ATTR_FLASH] = FLASH_SHORT
|
|
|
|
|
|
|
|
elif dat[ATTR_FLASH] == FLASH_LONG:
|
|
|
|
params[ATTR_FLASH] = FLASH_LONG
|
|
|
|
|
2015-01-06 07:02:41 +00:00
|
|
|
for light in target_lights:
|
2014-11-26 05:28:43 +00:00
|
|
|
# pylint: disable=star-args
|
|
|
|
light.turn_on(**params)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-01-06 07:02:41 +00:00
|
|
|
for light in target_lights:
|
2014-11-09 23:12:23 +00:00
|
|
|
light.update_ha_state(hass, True)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-03-15 07:24:28 +00:00
|
|
|
# Update light state every 30 seconds
|
2014-04-24 07:40:45 +00:00
|
|
|
hass.track_time_change(update_lights_state, second=[0, 30])
|
2014-03-15 07:24:28 +00:00
|
|
|
|
|
|
|
# Listen for light on and light off service calls
|
2014-04-24 07:40:45 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_TURN_ON,
|
|
|
|
handle_light_service)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
hass.services.register(DOMAIN, SERVICE_TURN_OFF,
|
|
|
|
handle_light_service)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
return True
|