2017-05-24 18:32:22 +00:00
|
|
|
"""
|
|
|
|
Support for Template lights.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/light.template/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS, ENTITY_ID_FORMAT, Light, SUPPORT_BRIGHTNESS)
|
|
|
|
from homeassistant.const import (
|
2017-10-30 16:28:37 +00:00
|
|
|
CONF_VALUE_TEMPLATE, CONF_ICON_TEMPLATE, CONF_ENTITY_PICTURE_TEMPLATE,
|
|
|
|
CONF_ENTITY_ID, CONF_FRIENDLY_NAME, STATE_ON, STATE_OFF,
|
|
|
|
EVENT_HOMEASSISTANT_START, MATCH_ALL, CONF_LIGHTS
|
|
|
|
)
|
2017-05-24 18:32:22 +00:00
|
|
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.exceptions import TemplateError
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import async_generate_entity_id
|
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
|
|
|
from homeassistant.helpers.script import Script
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']
|
|
|
|
|
|
|
|
CONF_ON_ACTION = 'turn_on'
|
|
|
|
CONF_OFF_ACTION = 'turn_off'
|
|
|
|
CONF_LEVEL_ACTION = 'set_level'
|
|
|
|
CONF_LEVEL_TEMPLATE = 'level_template'
|
|
|
|
|
|
|
|
LIGHT_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_ON_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Required(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
|
2018-02-17 09:29:14 +00:00
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_LEVEL_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Optional(CONF_LEVEL_TEMPLATE): cv.template,
|
2018-01-03 22:52:36 +00:00
|
|
|
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
2017-05-24 18:32:22 +00:00
|
|
|
vol.Optional(CONF_ENTITY_ID): cv.entity_ids
|
|
|
|
})
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_LIGHTS): vol.Schema({cv.slug: LIGHT_SCHEMA}),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
|
|
|
discovery_info=None):
|
2017-10-18 16:41:14 +00:00
|
|
|
"""Set up the Template Lights."""
|
2017-05-24 18:32:22 +00:00
|
|
|
lights = []
|
|
|
|
|
|
|
|
for device, device_config in config[CONF_LIGHTS].items():
|
|
|
|
friendly_name = device_config.get(CONF_FRIENDLY_NAME, device)
|
2018-02-17 09:29:14 +00:00
|
|
|
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
2017-10-30 16:28:37 +00:00
|
|
|
icon_template = device_config.get(CONF_ICON_TEMPLATE)
|
|
|
|
entity_picture_template = device_config.get(
|
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE)
|
2017-05-24 18:32:22 +00:00
|
|
|
on_action = device_config[CONF_ON_ACTION]
|
|
|
|
off_action = device_config[CONF_OFF_ACTION]
|
2017-06-12 05:58:20 +00:00
|
|
|
level_action = device_config.get(CONF_LEVEL_ACTION)
|
2018-02-17 09:29:14 +00:00
|
|
|
level_template = device_config.get(CONF_LEVEL_TEMPLATE)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
template_entity_ids = set()
|
|
|
|
|
|
|
|
if state_template is not None:
|
|
|
|
temp_ids = state_template.extract_entities()
|
|
|
|
if str(temp_ids) != MATCH_ALL:
|
|
|
|
template_entity_ids |= set(temp_ids)
|
|
|
|
|
|
|
|
if level_template is not None:
|
|
|
|
temp_ids = level_template.extract_entities()
|
|
|
|
if str(temp_ids) != MATCH_ALL:
|
|
|
|
template_entity_ids |= set(temp_ids)
|
|
|
|
|
2017-10-30 16:28:37 +00:00
|
|
|
if icon_template is not None:
|
|
|
|
temp_ids = icon_template.extract_entities()
|
|
|
|
if str(temp_ids) != MATCH_ALL:
|
|
|
|
template_entity_ids |= set(temp_ids)
|
|
|
|
|
|
|
|
if entity_picture_template is not None:
|
|
|
|
temp_ids = entity_picture_template.extract_entities()
|
|
|
|
if str(temp_ids) != MATCH_ALL:
|
|
|
|
template_entity_ids |= set(temp_ids)
|
|
|
|
|
2017-05-24 18:32:22 +00:00
|
|
|
if not template_entity_ids:
|
|
|
|
template_entity_ids = MATCH_ALL
|
|
|
|
|
|
|
|
entity_ids = device_config.get(CONF_ENTITY_ID, template_entity_ids)
|
|
|
|
|
|
|
|
lights.append(
|
|
|
|
LightTemplate(
|
|
|
|
hass, device, friendly_name, state_template,
|
2017-10-30 16:28:37 +00:00
|
|
|
icon_template, entity_picture_template, on_action,
|
|
|
|
off_action, level_action, level_template, entity_ids)
|
2017-05-24 18:32:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if not lights:
|
|
|
|
_LOGGER.error("No lights added")
|
|
|
|
return False
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(lights)
|
2017-05-24 18:32:22 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class LightTemplate(Light):
|
|
|
|
"""Representation of a templated Light, including dimmable."""
|
|
|
|
|
|
|
|
def __init__(self, hass, device_id, friendly_name, state_template,
|
2017-10-30 16:28:37 +00:00
|
|
|
icon_template, entity_picture_template, on_action,
|
|
|
|
off_action, level_action, level_template, entity_ids):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Initialize the light."""
|
|
|
|
self.hass = hass
|
|
|
|
self.entity_id = async_generate_entity_id(
|
|
|
|
ENTITY_ID_FORMAT, device_id, hass=hass)
|
|
|
|
self._name = friendly_name
|
|
|
|
self._template = state_template
|
2017-10-30 16:28:37 +00:00
|
|
|
self._icon_template = icon_template
|
|
|
|
self._entity_picture_template = entity_picture_template
|
2017-05-24 18:32:22 +00:00
|
|
|
self._on_script = Script(hass, on_action)
|
|
|
|
self._off_script = Script(hass, off_action)
|
2017-06-12 05:58:20 +00:00
|
|
|
self._level_script = None
|
|
|
|
if level_action is not None:
|
|
|
|
self._level_script = Script(hass, level_action)
|
2017-05-24 18:32:22 +00:00
|
|
|
self._level_template = level_template
|
|
|
|
|
|
|
|
self._state = False
|
2017-10-30 16:28:37 +00:00
|
|
|
self._icon = None
|
|
|
|
self._entity_picture = None
|
2017-05-24 18:32:22 +00:00
|
|
|
self._brightness = None
|
|
|
|
self._entities = entity_ids
|
|
|
|
|
|
|
|
if self._template is not None:
|
|
|
|
self._template.hass = self.hass
|
|
|
|
if self._level_template is not None:
|
|
|
|
self._level_template.hass = self.hass
|
2017-10-30 16:28:37 +00:00
|
|
|
if self._icon_template is not None:
|
|
|
|
self._icon_template.hass = self.hass
|
|
|
|
if self._entity_picture_template is not None:
|
|
|
|
self._entity_picture_template.hass = self.hass
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of the light."""
|
|
|
|
return self._brightness
|
|
|
|
|
2017-09-14 08:13:01 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
|
|
|
return self._name
|
|
|
|
|
2017-05-24 18:32:22 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
if self._level_script is not None:
|
|
|
|
return SUPPORT_BRIGHTNESS
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the polling state."""
|
|
|
|
return False
|
|
|
|
|
2017-10-30 16:28:37 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_picture(self):
|
|
|
|
"""Return the entity picture to use in the frontend, if any."""
|
|
|
|
return self._entity_picture
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Register callbacks."""
|
|
|
|
@callback
|
|
|
|
def template_light_state_listener(entity, old_state, new_state):
|
|
|
|
"""Handle target device state changes."""
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def template_light_startup(event):
|
|
|
|
"""Update template on startup."""
|
|
|
|
if (self._template is not None or
|
|
|
|
self._level_template is not None):
|
|
|
|
async_track_state_change(
|
|
|
|
self.hass, self._entities, template_light_state_listener)
|
|
|
|
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
self.hass.bus.async_listen_once(
|
|
|
|
EVENT_HOMEASSISTANT_START, template_light_startup)
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Turn the light on."""
|
|
|
|
optimistic_set = False
|
|
|
|
# set optimistic states
|
|
|
|
if self._template is None:
|
|
|
|
self._state = True
|
|
|
|
optimistic_set = True
|
|
|
|
|
|
|
|
if self._level_template is None and ATTR_BRIGHTNESS in kwargs:
|
|
|
|
_LOGGER.info("Optimistically setting brightness to %s",
|
|
|
|
kwargs[ATTR_BRIGHTNESS])
|
|
|
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
optimistic_set = True
|
|
|
|
|
|
|
|
if ATTR_BRIGHTNESS in kwargs and self._level_script:
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._level_script.async_run(
|
|
|
|
{"brightness": kwargs[ATTR_BRIGHTNESS]}, context=self._context)
|
2017-05-24 18:32:22 +00:00
|
|
|
else:
|
2018-10-01 06:56:50 +00:00
|
|
|
await self._on_script.async_run()
|
2017-05-24 18:32:22 +00:00
|
|
|
|
|
|
|
if optimistic_set:
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Turn the light off."""
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._off_script.async_run(context=self._context)
|
2017-05-24 18:32:22 +00:00
|
|
|
if self._template is None:
|
|
|
|
self._state = False
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-24 18:32:22 +00:00
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_update(self):
|
2017-05-24 18:32:22 +00:00
|
|
|
"""Update the state from the template."""
|
|
|
|
if self._template is not None:
|
|
|
|
try:
|
|
|
|
state = self._template.async_render().lower()
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error(ex)
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
if state in _VALID_STATES:
|
|
|
|
self._state = state in ('true', STATE_ON)
|
|
|
|
else:
|
|
|
|
_LOGGER.error(
|
2018-07-17 17:34:29 +00:00
|
|
|
'Received invalid light is_on state: %s. Expected: %s',
|
2017-05-24 18:32:22 +00:00
|
|
|
state, ', '.join(_VALID_STATES))
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
if self._level_template is not None:
|
|
|
|
try:
|
|
|
|
brightness = self._level_template.async_render()
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error(ex)
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
if 0 <= int(brightness) <= 255:
|
2018-02-16 17:32:11 +00:00
|
|
|
self._brightness = int(brightness)
|
2017-05-24 18:32:22 +00:00
|
|
|
else:
|
|
|
|
_LOGGER.error(
|
2018-07-17 17:34:29 +00:00
|
|
|
'Received invalid brightness : %s. Expected: 0-255',
|
2017-05-24 18:32:22 +00:00
|
|
|
brightness)
|
|
|
|
self._brightness = None
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
for property_name, template in (
|
|
|
|
('_icon', self._icon_template),
|
|
|
|
('_entity_picture', self._entity_picture_template)):
|
|
|
|
if template is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
setattr(self, property_name, template.async_render())
|
|
|
|
except TemplateError as ex:
|
|
|
|
friendly_property_name = property_name[1:].replace('_', ' ')
|
|
|
|
if ex.args and ex.args[0].startswith(
|
|
|
|
"UndefinedError: 'None' has no attribute"):
|
|
|
|
# Common during HA startup - so just a warning
|
|
|
|
_LOGGER.warning('Could not render %s template %s,'
|
|
|
|
' the state is unknown.',
|
|
|
|
friendly_property_name, self._name)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
setattr(self, property_name,
|
|
|
|
getattr(super(), property_name))
|
|
|
|
except AttributeError:
|
|
|
|
_LOGGER.error('Could not render %s template %s: %s',
|
|
|
|
friendly_property_name, self._name, ex)
|