mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Convert template light to use async_track_template_result (#39045)
* Convert template lock to use async_track_template_result * Convert template switch to use async_track_template_result * Convert template fan to use async_track_template_result * Convert template binary_sensor to use async_track_template_result Co-Authored-By: Penny Wood <Swamp-Ig@users.noreply.github.com> * Convert template cover to use async_track_template_result * Convert template light to use async_track_template_result Co-authored-by: Penny Wood <Swamp-Ig@users.noreply.github.com>
This commit is contained in:
parent
e861957937
commit
b4f0485be2
@ -23,8 +23,6 @@ from homeassistant.const import (
|
|||||||
CONF_LIGHTS,
|
CONF_LIGHTS,
|
||||||
CONF_UNIQUE_ID,
|
CONF_UNIQUE_ID,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
EVENT_HOMEASSISTANT_START,
|
|
||||||
MATCH_ALL,
|
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
@ -33,11 +31,10 @@ from homeassistant.exceptions import TemplateError
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
||||||
from homeassistant.helpers.entity import async_generate_entity_id
|
from homeassistant.helpers.entity import async_generate_entity_id
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
|
|
||||||
from . import extract_entities, initialise_templates
|
|
||||||
from .const import CONF_AVAILABILITY_TEMPLATE
|
from .const import CONF_AVAILABILITY_TEMPLATE
|
||||||
|
from .template_entity import TemplateEntityWithAvailabilityAndImages
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
|
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
|
||||||
@ -108,22 +105,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
white_value_action = device_config.get(CONF_WHITE_VALUE_ACTION)
|
white_value_action = device_config.get(CONF_WHITE_VALUE_ACTION)
|
||||||
white_value_template = device_config.get(CONF_WHITE_VALUE_TEMPLATE)
|
white_value_template = device_config.get(CONF_WHITE_VALUE_TEMPLATE)
|
||||||
|
|
||||||
templates = {
|
|
||||||
CONF_VALUE_TEMPLATE: state_template,
|
|
||||||
CONF_ICON_TEMPLATE: icon_template,
|
|
||||||
CONF_ENTITY_PICTURE_TEMPLATE: entity_picture_template,
|
|
||||||
CONF_AVAILABILITY_TEMPLATE: availability_template,
|
|
||||||
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)
|
|
||||||
entity_ids = extract_entities(
|
|
||||||
device, "light", device_config.get(CONF_ENTITY_ID), templates
|
|
||||||
)
|
|
||||||
|
|
||||||
lights.append(
|
lights.append(
|
||||||
LightTemplate(
|
LightTemplate(
|
||||||
hass,
|
hass,
|
||||||
@ -137,7 +118,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
off_action,
|
off_action,
|
||||||
level_action,
|
level_action,
|
||||||
level_template,
|
level_template,
|
||||||
entity_ids,
|
|
||||||
temperature_action,
|
temperature_action,
|
||||||
temperature_template,
|
temperature_template,
|
||||||
color_action,
|
color_action,
|
||||||
@ -151,7 +131,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async_add_entities(lights)
|
async_add_entities(lights)
|
||||||
|
|
||||||
|
|
||||||
class LightTemplate(LightEntity):
|
class LightTemplate(TemplateEntityWithAvailabilityAndImages, LightEntity):
|
||||||
"""Representation of a templated Light, including dimmable."""
|
"""Representation of a templated Light, including dimmable."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -167,7 +147,6 @@ class LightTemplate(LightEntity):
|
|||||||
off_action,
|
off_action,
|
||||||
level_action,
|
level_action,
|
||||||
level_template,
|
level_template,
|
||||||
entity_ids,
|
|
||||||
temperature_action,
|
temperature_action,
|
||||||
temperature_template,
|
temperature_template,
|
||||||
color_action,
|
color_action,
|
||||||
@ -177,15 +156,14 @@ class LightTemplate(LightEntity):
|
|||||||
unique_id,
|
unique_id,
|
||||||
):
|
):
|
||||||
"""Initialize the light."""
|
"""Initialize the light."""
|
||||||
self.hass = hass
|
super().__init__(
|
||||||
|
availability_template, icon_template, entity_picture_template,
|
||||||
|
)
|
||||||
self.entity_id = async_generate_entity_id(
|
self.entity_id = async_generate_entity_id(
|
||||||
ENTITY_ID_FORMAT, device_id, hass=hass
|
ENTITY_ID_FORMAT, device_id, hass=hass
|
||||||
)
|
)
|
||||||
self._name = friendly_name
|
self._name = friendly_name
|
||||||
self._template = state_template
|
self._template = state_template
|
||||||
self._icon_template = icon_template
|
|
||||||
self._entity_picture_template = entity_picture_template
|
|
||||||
self._availability_template = availability_template
|
|
||||||
domain = __name__.split(".")[-2]
|
domain = __name__.split(".")[-2]
|
||||||
self._on_script = Script(hass, on_action, friendly_name, domain)
|
self._on_script = Script(hass, on_action, friendly_name, domain)
|
||||||
self._off_script = Script(hass, off_action, friendly_name, domain)
|
self._off_script = Script(hass, off_action, friendly_name, domain)
|
||||||
@ -211,14 +189,10 @@ class LightTemplate(LightEntity):
|
|||||||
self._white_value_template = white_value_template
|
self._white_value_template = white_value_template
|
||||||
|
|
||||||
self._state = False
|
self._state = False
|
||||||
self._icon = None
|
|
||||||
self._entity_picture = None
|
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
self._temperature = None
|
self._temperature = None
|
||||||
self._color = None
|
self._color = None
|
||||||
self._white_value = None
|
self._white_value = None
|
||||||
self._entities = entity_ids
|
|
||||||
self._available = True
|
|
||||||
self._unique_id = unique_id
|
self._unique_id = unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -270,56 +244,46 @@ class LightTemplate(LightEntity):
|
|||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Return the polling state."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return if the device is available."""
|
|
||||||
return self._available
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
|
||||||
@callback
|
if self._template:
|
||||||
def template_light_state_listener(event):
|
self.add_template_attribute(
|
||||||
"""Handle target device state changes."""
|
"_state", self._template, None, self._update_state
|
||||||
self.async_schedule_update_ha_state(True)
|
)
|
||||||
|
if self._level_template:
|
||||||
@callback
|
self.add_template_attribute(
|
||||||
def template_light_startup(event):
|
"_brightness",
|
||||||
"""Update template on startup."""
|
self._level_template,
|
||||||
if (
|
None,
|
||||||
self._template is not None
|
self._update_brightness,
|
||||||
or self._level_template is not None
|
none_on_template_error=True,
|
||||||
or self._temperature_template is not None
|
)
|
||||||
or self._color_template is not None
|
if self._temperature_template:
|
||||||
or self._white_value_template is not None
|
self.add_template_attribute(
|
||||||
or self._availability_template is not None
|
"_temperature",
|
||||||
):
|
self._temperature_template,
|
||||||
if self._entities != MATCH_ALL:
|
None,
|
||||||
# Track state change only for valid templates
|
self._update_temperature,
|
||||||
async_track_state_change_event(
|
none_on_template_error=True,
|
||||||
self.hass, self._entities, template_light_state_listener
|
)
|
||||||
)
|
if self._color_template:
|
||||||
|
self.add_template_attribute(
|
||||||
self.async_schedule_update_ha_state(True)
|
"_color",
|
||||||
|
self._color_template,
|
||||||
self.hass.bus.async_listen_once(
|
None,
|
||||||
EVENT_HOMEASSISTANT_START, template_light_startup
|
self._update_color,
|
||||||
)
|
none_on_template_error=True,
|
||||||
|
)
|
||||||
|
if self._white_value_template:
|
||||||
|
self.add_template_attribute(
|
||||||
|
"_white_value",
|
||||||
|
self._white_value_template,
|
||||||
|
None,
|
||||||
|
self._update_white_value,
|
||||||
|
none_on_template_error=True,
|
||||||
|
)
|
||||||
|
await super().async_added_to_hass()
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
@ -382,61 +346,10 @@ class LightTemplate(LightEntity):
|
|||||||
self._state = False
|
self._state = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Update from templates."""
|
|
||||||
self.update_state()
|
|
||||||
|
|
||||||
self.update_brightness()
|
|
||||||
|
|
||||||
self.update_temperature()
|
|
||||||
|
|
||||||
self.update_color()
|
|
||||||
|
|
||||||
self.update_white_value()
|
|
||||||
|
|
||||||
for property_name, template in (
|
|
||||||
("_icon", self._icon_template),
|
|
||||||
("_entity_picture", self._entity_picture_template),
|
|
||||||
("_available", self._availability_template),
|
|
||||||
):
|
|
||||||
if template is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
value = template.async_render()
|
|
||||||
if property_name == "_available":
|
|
||||||
value = value.lower() == "true"
|
|
||||||
setattr(self, property_name, value)
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_brightness(self):
|
def _update_brightness(self, brightness):
|
||||||
"""Update the brightness from the template."""
|
"""Update the brightness from the template."""
|
||||||
if self._level_template is None:
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
brightness = self._level_template.async_render()
|
|
||||||
if brightness in ("None", ""):
|
if brightness in ("None", ""):
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
return
|
return
|
||||||
@ -453,17 +366,11 @@ class LightTemplate(LightEntity):
|
|||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
except TemplateError:
|
|
||||||
_LOGGER.error("Invalid template", exc_info=True)
|
|
||||||
self._brightness = None
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_white_value(self):
|
def _update_white_value(self, white_value):
|
||||||
"""Update the white value from the template."""
|
"""Update the white value from the template."""
|
||||||
if self._white_value_template is None:
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
white_value = self._white_value_template.async_render()
|
|
||||||
if white_value in ("None", ""):
|
if white_value in ("None", ""):
|
||||||
self._white_value = None
|
self._white_value = None
|
||||||
return
|
return
|
||||||
@ -480,37 +387,34 @@ class LightTemplate(LightEntity):
|
|||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
self._white_value = None
|
self._white_value = None
|
||||||
except TemplateError as ex:
|
|
||||||
_LOGGER.error(ex)
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_state(self):
|
def _update_state(self, result):
|
||||||
"""Update the state from the template."""
|
"""Update the state from the template."""
|
||||||
if self._template is None:
|
|
||||||
|
if isinstance(result, TemplateError):
|
||||||
|
# This behavior is legacy
|
||||||
|
self._state = False
|
||||||
|
if not self._availability_template:
|
||||||
|
self._available = True
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
state = self._template.async_render().lower()
|
state = str(result).lower()
|
||||||
if state in _VALID_STATES:
|
if state in _VALID_STATES:
|
||||||
self._state = state in ("true", STATE_ON)
|
self._state = state in ("true", STATE_ON)
|
||||||
else:
|
else:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Received invalid light is_on state: %s. Expected: %s",
|
"Received invalid light is_on state: %s. Expected: %s",
|
||||||
state,
|
state,
|
||||||
", ".join(_VALID_STATES),
|
", ".join(_VALID_STATES),
|
||||||
)
|
)
|
||||||
self._state = None
|
|
||||||
except TemplateError as ex:
|
|
||||||
_LOGGER.error(ex)
|
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_temperature(self):
|
def _update_temperature(self, render):
|
||||||
"""Update the temperature from the template."""
|
"""Update the temperature from the template."""
|
||||||
if self._temperature_template is None:
|
|
||||||
return
|
|
||||||
try:
|
try:
|
||||||
render = self._temperature_template.async_render()
|
|
||||||
if render in ("None", ""):
|
if render in ("None", ""):
|
||||||
self._temperature = None
|
self._temperature = None
|
||||||
return
|
return
|
||||||
@ -530,41 +434,30 @@ class LightTemplate(LightEntity):
|
|||||||
exc_info=True,
|
exc_info=True,
|
||||||
)
|
)
|
||||||
self._temperature = None
|
self._temperature = None
|
||||||
except TemplateError:
|
|
||||||
_LOGGER.error("Cannot evaluate temperature template", exc_info=True)
|
|
||||||
self._temperature = None
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_color(self):
|
def _update_color(self, render):
|
||||||
"""Update the hs_color from the template."""
|
"""Update the hs_color from the template."""
|
||||||
if self._color_template is None:
|
if render in ("None", ""):
|
||||||
return
|
self._color = None
|
||||||
|
return
|
||||||
try:
|
h_str, s_str = map(
|
||||||
render = self._color_template.async_render()
|
float, render.replace("(", "").replace(")", "").split(",", 1)
|
||||||
if render in ("None", ""):
|
)
|
||||||
self._color = None
|
if (
|
||||||
return
|
h_str is not None
|
||||||
h_str, s_str = map(
|
and s_str is not None
|
||||||
float, render.replace("(", "").replace(")", "").split(",", 1)
|
and 0 <= h_str <= 360
|
||||||
)
|
and 0 <= s_str <= 100
|
||||||
if (
|
):
|
||||||
h_str is not None
|
self._color = (h_str, s_str)
|
||||||
and s_str is not None
|
elif h_str is not None and s_str is not None:
|
||||||
and 0 <= h_str <= 360
|
_LOGGER.error(
|
||||||
and 0 <= s_str <= 100
|
"Received invalid hs_color : (%s, %s). Expected: (0-360, 0-100)",
|
||||||
):
|
h_str,
|
||||||
self._color = (h_str, s_str)
|
s_str,
|
||||||
elif h_str is not None and s_str is not None:
|
)
|
||||||
_LOGGER.error(
|
self._color = None
|
||||||
"Received invalid hs_color : (%s, %s). Expected: (0-360, 0-100)",
|
else:
|
||||||
h_str,
|
_LOGGER.error("Received invalid hs_color : (%s)", render)
|
||||||
s_str,
|
|
||||||
)
|
|
||||||
self._color = None
|
|
||||||
else:
|
|
||||||
_LOGGER.error("Received invalid hs_color : (%s)", render)
|
|
||||||
self._color = None
|
|
||||||
except TemplateError:
|
|
||||||
_LOGGER.error("Cannot evaluate hs_color template", exc_info=True)
|
|
||||||
self._color = None
|
self._color = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user