Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions homeassistant/components/template/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_HS_COLOR,
ATTR_WHITE_VALUE,
ENTITY_ID_FORMAT,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_COLOR_TEMP,
SUPPORT_WHITE_VALUE,
Light,
)
from homeassistant.const import (
Expand Down Expand Up @@ -46,6 +48,8 @@
CONF_TEMPERATURE_ACTION = "set_temperature"
CONF_COLOR_TEMPLATE = "color_template"
CONF_COLOR_ACTION = "set_color"
CONF_WHITE_VALUE_TEMPLATE = "white_value_template"
CONF_WHITE_VALUE_ACTION = "set_white_value"

LIGHT_SCHEMA = vol.Schema(
{
Expand All @@ -63,6 +67,8 @@
vol.Optional(CONF_TEMPERATURE_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_COLOR_TEMPLATE): cv.template,
vol.Optional(CONF_COLOR_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_WHITE_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_WHITE_VALUE_ACTION): cv.SCRIPT_SCHEMA,
}
)

Expand Down Expand Up @@ -95,6 +101,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
color_action = device_config.get(CONF_COLOR_ACTION)
color_template = device_config.get(CONF_COLOR_TEMPLATE)

white_value_action = device_config.get(CONF_WHITE_VALUE_ACTION)
white_value_template = device_config.get(CONF_WHITE_VALUE_TEMPLATE)

templates = {
CONF_VALUE_TEMPLATE: state_template,
CONF_ICON_TEMPLATE: icon_template,
Expand All @@ -103,6 +112,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
CONF_LEVEL_TEMPLATE: level_template,
CONF_TEMPERATURE_TEMPLATE: temperature_template,
CONF_COLOR_TEMPLATE: color_template,
CONF_WHITE_VALUE_TEMPLATE: white_value_template,
}

initialise_templates(hass, templates)
Expand All @@ -128,6 +138,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
temperature_template,
color_action,
color_template,
white_value_action,
white_value_template,
)
)

Expand Down Expand Up @@ -155,6 +167,8 @@ def __init__(
temperature_template,
color_action,
color_template,
white_value_action,
white_value_template,
):
"""Initialize the light."""
self.hass = hass
Expand All @@ -180,13 +194,17 @@ def __init__(
if color_action is not None:
self._color_script = Script(hass, color_action)
self._color_template = color_template
if white_value_action is not None:
self._white_value_script = Script(hass, white_value_action)
self._white_value_template = white_value_template

self._state = False
self._icon = None
self._entity_picture = None
self._brightness = None
self._temperature = None
self._color = None
self._white_value = None
self._entities = entity_ids
self._available = True

Expand All @@ -200,6 +218,11 @@ def color_temp(self):
"""Return the CT color value in mireds."""
return self._temperature

@property
def white_value(self):
"""Return the CT color value in mireds."""
Comment thread
elahd marked this conversation as resolved.
Outdated
return self._white_value

@property
def hs_color(self):
"""Return the hue and saturation color value [float, float]."""
Expand All @@ -220,6 +243,8 @@ def supported_features(self):
supported_features |= SUPPORT_COLOR_TEMP
if self._color_script is not None:
supported_features |= SUPPORT_COLOR
if self._white_value_script is not None:
supported_features |= SUPPORT_WHITE_VALUE
return supported_features

@property
Expand Down Expand Up @@ -263,6 +288,7 @@ def template_light_startup(event):
or self._level_template is not None
or self._temperature_template is not None
or self._color_template is not None
or self._white_value_template is not None
or self._availability_template is not None
):
async_track_state_change(
Expand Down Expand Up @@ -290,6 +316,13 @@ async def async_turn_on(self, **kwargs):
self._brightness = kwargs[ATTR_BRIGHTNESS]
optimistic_set = True

if self._white_value_template is None and ATTR_WHITE_VALUE in kwargs:
_LOGGER.info(
"Optimistically setting white value to %s", kwargs[ATTR_WHITE_VALUE]
)
self._white_value = kwargs[ATTR_WHITE_VALUE]
optimistic_set = True

if self._temperature_template is None and ATTR_COLOR_TEMP in kwargs:
_LOGGER.info(
"Optimistically setting color temperature to %s",
Expand All @@ -306,6 +339,10 @@ async def async_turn_on(self, **kwargs):
await self._temperature_script.async_run(
{"color_temp": kwargs[ATTR_COLOR_TEMP]}, context=self._context
)
elif ATTR_WHITE_VALUE in kwargs and self._white_value_script:
await self._white_value_script.async_run(
{"white_value": kwargs[ATTR_WHITE_VALUE]}, context=self._context
)
elif ATTR_HS_COLOR in kwargs and self._color_script:
hs_value = kwargs[ATTR_HS_COLOR]
await self._color_script.async_run(
Expand Down Expand Up @@ -335,6 +372,8 @@ async def async_update(self):

self.update_color()

self.update_white_value()

for property_name, template in (
("_icon", self._icon_template),
("_entity_picture", self._entity_picture_template),
Expand Down Expand Up @@ -389,6 +428,24 @@ def update_brightness(self):
_LOGGER.error(ex)
self._state = None

@callback
def update_white_value(self):
"""Update the white value from the template."""
if self._white_value_template is None:
return
try:
white_value = self._white_value_template.async_render()
if 0 <= int(white_value) <= 255:
Comment thread
MartinHjelmare marked this conversation as resolved.
self._white_value = int(white_value)
else:
_LOGGER.error(
"Received invalid white value : %s. Expected: 0-255", white_value
)
self._white_value = None
except TemplateError as ex:
Comment thread
MartinHjelmare marked this conversation as resolved.
_LOGGER.error(ex)
self._state = None

@callback
def update_state(self):
"""Update the state from the template."""
Expand Down