Add config flow for template sensor (#98970)

* Add config flow for template sensor

* Tweak error reporting

* Improve validation

* Fix test

* Rename translation strings

* Improve validation

* Fix sensor async_setup_entry

* Avoid duplicating sensor device class translations

* Avoid duplicating sensor device class translations

* Add config flow tests

* Include all units from DEVICE_CLASS_UNITS in unit_of_measurement select

* Address review comments
This commit is contained in:
Erik Montnemery 2023-08-30 16:22:52 +02:00 committed by GitHub
parent bc5f934f35
commit 63c538b024
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 1174 additions and 54 deletions

View file

@ -6,6 +6,7 @@ from collections.abc import Callable
import logging
from homeassistant import config as conf_util
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_UNIQUE_ID,
EVENT_HOMEASSISTANT_START,
@ -60,6 +61,27 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry."""
await hass.config_entries.async_forward_entry_setups(
entry, (entry.options["template_type"],)
)
entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
return True
async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener, called when the config entry options are changed."""
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(
entry, (entry.options["template_type"],)
)
async def _process_config(hass: HomeAssistant, hass_config: ConfigType) -> None:
"""Process config."""
coordinators: list[TriggerUpdateCoordinator] | None = hass.data.pop(DOMAIN, None)

View file

@ -254,13 +254,14 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
)
self._state = None
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._template:
self.add_template_attribute(
"_state", self._template, None, self._update_state
)
await super().async_added_to_hass()
super()._async_setup_templates()
async def _async_alarm_arm(self, state, script, code):
"""Arm the panel to specified state with supplied script."""

View file

@ -224,14 +224,18 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
self._delay_off_raw = config.get(CONF_DELAY_OFF)
async def async_added_to_hass(self) -> None:
"""Restore state and register callbacks."""
"""Restore state."""
if (
(self._delay_on_raw is not None or self._delay_off_raw is not None)
and (last_state := await self.async_get_last_state()) is not None
and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE)
):
self._state = last_state.state == STATE_ON
await super().async_added_to_hass()
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute("_state", self._template, None, self._update_state)
if self._delay_on_raw is not None:
@ -250,7 +254,7 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity, RestoreEntity):
"_delay_off", self._delay_off_raw, cv.positive_time_period
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_state(self, result):

View file

@ -0,0 +1,344 @@
"""Config flow for the Template integration."""
from __future__ import annotations
from collections.abc import Callable, Coroutine, Mapping
from typing import Any, cast
import voluptuous as vol
from homeassistant.components import websocket_api
from homeassistant.components.sensor import (
CONF_STATE_CLASS,
DEVICE_CLASS_STATE_CLASSES,
DEVICE_CLASS_UNITS,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
CONF_DEVICE_CLASS,
CONF_NAME,
CONF_STATE,
CONF_UNIT_OF_MEASUREMENT,
Platform,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import selector
from homeassistant.helpers.schema_config_entry_flow import (
SchemaCommonFlowHandler,
SchemaConfigFlowHandler,
SchemaFlowFormStep,
SchemaFlowMenuStep,
)
from .const import DOMAIN
from .sensor import async_create_preview_sensor
from .template_entity import TemplateEntity
NONE_SENTINEL = "none"
def generate_schema(domain: str) -> dict[vol.Marker, Any]:
"""Generate schema."""
schema: dict[vol.Marker, Any] = {}
if domain == Platform.SENSOR:
schema = {
vol.Optional(
CONF_UNIT_OF_MEASUREMENT, default=NONE_SENTINEL
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted(
{
str(unit)
for units in DEVICE_CLASS_UNITS.values()
for unit in units
if unit is not None
},
key=str.casefold,
),
],
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="sensor_unit_of_measurement",
custom_value=True,
),
),
vol.Optional(
CONF_DEVICE_CLASS, default=NONE_SENTINEL
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted(
[
cls.value
for cls in SensorDeviceClass
if cls != SensorDeviceClass.ENUM
],
key=str.casefold,
),
],
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="sensor_device_class",
),
),
vol.Optional(
CONF_STATE_CLASS, default=NONE_SENTINEL
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
NONE_SENTINEL,
*sorted([cls.value for cls in SensorStateClass]),
],
mode=selector.SelectSelectorMode.DROPDOWN,
translation_key="sensor_state_class",
),
),
}
return schema
def options_schema(domain: str) -> vol.Schema:
"""Generate options schema."""
return vol.Schema(
{vol.Required(CONF_STATE): selector.TemplateSelector()}
| generate_schema(domain),
)
def config_schema(domain: str) -> vol.Schema:
"""Generate config schema."""
return vol.Schema(
{
vol.Required(CONF_NAME): selector.TextSelector(),
vol.Required(CONF_STATE): selector.TemplateSelector(),
}
| generate_schema(domain),
)
async def choose_options_step(options: dict[str, Any]) -> str:
"""Return next step_id for options flow according to template_type."""
return cast(str, options["template_type"])
def _strip_sentinel(options: dict[str, Any]) -> None:
"""Convert sentinel to None."""
for key in (CONF_DEVICE_CLASS, CONF_STATE_CLASS, CONF_UNIT_OF_MEASUREMENT):
if key not in options:
continue
if options[key] == NONE_SENTINEL:
options.pop(key)
def _validate_unit(options: dict[str, Any]) -> None:
"""Validate unit of measurement."""
if (
(device_class := options.get(CONF_DEVICE_CLASS))
and (units := DEVICE_CLASS_UNITS.get(device_class)) is not None
and (unit := options.get(CONF_UNIT_OF_MEASUREMENT)) not in units
):
units_string = sorted(
[str(unit) if unit else "no unit of measurement" for unit in units],
key=str.casefold,
)
raise vol.Invalid(
f"'{unit}' is not a valid unit for device class '{device_class}'; "
f"expected one of {', '.join(units_string)}"
)
def _validate_state_class(options: dict[str, Any]) -> None:
"""Validate state class."""
if (
(device_class := options.get(CONF_DEVICE_CLASS))
and (state_classes := DEVICE_CLASS_STATE_CLASSES.get(device_class)) is not None
and (state_class := options.get(CONF_STATE_CLASS)) not in state_classes
):
state_classes_string = sorted(
[str(state_class) for state_class in state_classes],
key=str.casefold,
)
raise vol.Invalid(
f"'{state_class}' is not a valid state class for device class "
f"'{device_class}'; expected one of {', '.join(state_classes_string)}"
)
def validate_user_input(
template_type: str,
) -> Callable[
[SchemaCommonFlowHandler, dict[str, Any]],
Coroutine[Any, Any, dict[str, Any]],
]:
"""Do post validation of user input.
For sensors: Strip none-sentinels and validate unit of measurement.
For all domaines: Set template type.
"""
async def _validate_user_input(
_: SchemaCommonFlowHandler,
user_input: dict[str, Any],
) -> dict[str, Any]:
"""Add template type to user input."""
if template_type == Platform.SENSOR:
_strip_sentinel(user_input)
_validate_unit(user_input)
_validate_state_class(user_input)
return {"template_type": template_type} | user_input
return _validate_user_input
TEMPLATE_TYPES = [
"sensor",
]
CONFIG_FLOW = {
"user": SchemaFlowMenuStep(TEMPLATE_TYPES),
Platform.SENSOR: SchemaFlowFormStep(
config_schema(Platform.SENSOR),
preview="template",
validate_user_input=validate_user_input(Platform.SENSOR),
),
}
OPTIONS_FLOW = {
"init": SchemaFlowFormStep(next_step=choose_options_step),
Platform.SENSOR: SchemaFlowFormStep(
options_schema(Platform.SENSOR),
preview="template",
validate_user_input=validate_user_input(Platform.SENSOR),
),
}
CREATE_PREVIEW_ENTITY: dict[
str,
Callable[[HomeAssistant, str, dict[str, Any]], TemplateEntity],
] = {
"sensor": async_create_preview_sensor,
}
class TemplateConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
"""Handle config flow for template helper."""
config_flow = CONFIG_FLOW
options_flow = OPTIONS_FLOW
@callback
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
"""Return config entry title."""
return cast(str, options["name"])
@staticmethod
async def async_setup_preview(hass: HomeAssistant) -> None:
"""Set up preview WS API."""
websocket_api.async_register_command(hass, ws_start_preview)
@websocket_api.websocket_command(
{
vol.Required("type"): "template/start_preview",
vol.Required("flow_id"): str,
vol.Required("flow_type"): vol.Any("config_flow", "options_flow"),
vol.Required("user_input"): dict,
}
)
@callback
def ws_start_preview(
hass: HomeAssistant,
connection: websocket_api.ActiveConnection,
msg: dict[str, Any],
) -> None:
"""Generate a preview."""
def _validate(schema: vol.Schema, domain: str, user_input: dict[str, Any]) -> Any:
errors = {}
key: vol.Marker
for key, validator in schema.schema.items():
if key.schema not in user_input:
continue
try:
validator(user_input[key.schema])
except vol.Invalid as ex:
errors[key.schema] = str(ex.msg)
if domain == Platform.SENSOR:
_strip_sentinel(user_input)
try:
_validate_unit(user_input)
except vol.Invalid as ex:
errors[CONF_UNIT_OF_MEASUREMENT] = str(ex.msg)
try:
_validate_state_class(user_input)
except vol.Invalid as ex:
errors[CONF_STATE_CLASS] = str(ex.msg)
return errors
if msg["flow_type"] == "config_flow":
flow_status = hass.config_entries.flow.async_get(msg["flow_id"])
template_type = flow_status["step_id"]
form_step = cast(SchemaFlowFormStep, CONFIG_FLOW[template_type])
schema = cast(vol.Schema, form_step.schema)
name = msg["user_input"]["name"]
else:
flow_status = hass.config_entries.options.async_get(msg["flow_id"])
config_entry = hass.config_entries.async_get_entry(flow_status["handler"])
if not config_entry:
raise HomeAssistantError
template_type = config_entry.options["template_type"]
name = config_entry.options["name"]
schema = cast(vol.Schema, OPTIONS_FLOW[template_type].schema)
errors = _validate(schema, template_type, msg["user_input"])
@callback
def async_preview_updated(
state: str | None,
attributes: Mapping[str, Any] | None,
error: str | None,
) -> None:
"""Forward config entry state events to websocket."""
if error is not None:
connection.send_message(
websocket_api.event_message(
msg["id"],
{"error": error},
)
)
return
connection.send_message(
websocket_api.event_message(
msg["id"],
{"attributes": attributes, "state": state},
)
)
if errors:
connection.send_message(
{
"id": msg["id"],
"type": websocket_api.const.TYPE_RESULT,
"success": False,
"error": {"code": "invalid_user_input", "message": errors},
}
)
return
_strip_sentinel(msg["user_input"])
preview_entity = CREATE_PREVIEW_ENTITY[template_type](hass, name, msg["user_input"])
preview_entity.hass = hass
connection.send_result(msg["id"])
connection.subscriptions[msg["id"]] = preview_entity.async_start_preview(
async_preview_updated
)

View file

@ -182,8 +182,9 @@ class CoverTemplate(TemplateEntity, CoverEntity):
self._is_closing = False
self._tilt_value = None
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._template:
self.add_template_attribute(
"_position", self._template, None, self._update_state
@ -204,7 +205,7 @@ class CoverTemplate(TemplateEntity, CoverEntity):
self._update_tilt,
none_on_template_error=True,
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_state(self, result):

View file

@ -351,8 +351,9 @@ class TemplateFan(TemplateEntity, FanEntity):
self._state = False
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._template:
self.add_template_attribute(
"_state", self._template, None, self._update_state
@ -390,7 +391,7 @@ class TemplateFan(TemplateEntity, FanEntity):
self._update_direction,
none_on_template_error=True,
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_percentage(self, percentage):

View file

@ -108,10 +108,11 @@ class StateImageEntity(TemplateEntity, ImageEntity):
self._cached_image = None
self._attr_image_url = result
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute("_url", self._url_template, None, self._update_url)
await super().async_added_to_hass()
super()._async_setup_templates()
class TriggerImageEntity(TriggerEntity, ImageEntity):

View file

@ -268,8 +268,9 @@ class LightTemplate(TemplateEntity, LightEntity):
"""Return true if device is on."""
return self._state
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._template:
self.add_template_attribute(
"_state", self._template, None, self._update_state
@ -338,7 +339,7 @@ class LightTemplate(TemplateEntity, LightEntity):
self._update_supports_transition,
none_on_template_error=True,
)
await super().async_added_to_hass()
super()._async_setup_templates()
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""

View file

@ -133,12 +133,13 @@ class TemplateLock(TemplateEntity, LockEntity):
self._state = None
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute(
"_state", self._state_template, None, self._update_state
)
await super().async_added_to_hass()
super()._async_setup_templates()
async def async_lock(self, **kwargs: Any) -> None:
"""Lock the device."""

View file

@ -3,7 +3,9 @@
"name": "Template",
"after_dependencies": ["group"],
"codeowners": ["@PhracturedBlue", "@tetienne", "@home-assistant/core"],
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/template",
"integration_type": "helper",
"iot_class": "local_push",
"quality_scale": "internal"
}

View file

@ -18,7 +18,7 @@ from homeassistant.components.number import (
NumberEntity,
)
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_STATE, CONF_UNIQUE_ID
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.script import Script
@ -124,8 +124,9 @@ class TemplateNumber(TemplateEntity, NumberEntity):
self._attr_native_min_value = DEFAULT_MIN_VALUE
self._attr_native_max_value = DEFAULT_MAX_VALUE
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute(
"_attr_native_value",
self._value_template,
@ -152,7 +153,7 @@ class TemplateNumber(TemplateEntity, NumberEntity):
validator=vol.Coerce(float),
none_on_template_error=True,
)
await super().async_added_to_hass()
super()._async_setup_templates()
async def async_set_native_value(self, value: float) -> None:
"""Set value of the number."""

View file

@ -13,7 +13,7 @@ from homeassistant.components.select import (
SelectEntity,
)
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_STATE, CONF_UNIQUE_ID
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.script import Script
@ -114,8 +114,9 @@ class TemplateSelect(TemplateEntity, SelectEntity):
self._attr_options = []
self._attr_current_option = None
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute(
"_attr_current_option",
self._value_template,
@ -128,7 +129,7 @@ class TemplateSelect(TemplateEntity, SelectEntity):
validator=vol.All(cv.ensure_list, [cv.string]),
none_on_template_error=True,
)
await super().async_added_to_hass()
super()._async_setup_templates()
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""

View file

@ -17,6 +17,7 @@ from homeassistant.components.sensor import (
SensorEntity,
)
from homeassistant.components.sensor.helpers import async_parse_date_datetime
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_DEVICE_CLASS,
@ -195,6 +196,28 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Initialize config entry."""
_options = dict(config_entry.options)
_options.pop("template_type")
validated_config = SENSOR_SCHEMA(_options)
async_add_entities([SensorTemplate(hass, validated_config, config_entry.entry_id)])
@callback
def async_create_preview_sensor(
hass: HomeAssistant, name: str, config: dict[str, Any]
) -> SensorTemplate:
"""Create a preview sensor."""
validated_config = SENSOR_SCHEMA(config | {CONF_NAME: name})
entity = SensorTemplate(hass, validated_config, None)
return entity
class SensorTemplate(TemplateEntity, SensorEntity):
"""Representation of a Template Sensor."""
@ -217,13 +240,14 @@ class SensorTemplate(TemplateEntity, SensorEntity):
ENTITY_ID_FORMAT, object_id, hass=hass
)
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
self.add_template_attribute(
"_attr_native_value", self._template, None, self._update_state
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_state(self, result):

View file

@ -1,4 +1,107 @@
{
"config": {
"step": {
"sensor": {
"data": {
"device_class": "Device class",
"name": "[%key:common::config_flow::data::name%]",
"state_class": "[%key:component::sensor::entity_component::_::state_attributes::state_class::name%]",
"state_template": "State template",
"unit_of_measurement": "Unit of measurement"
},
"title": "Template sensor"
},
"user": {
"description": "This helper allow you to create helper entities that define their state using a template.",
"menu_options": {
"sensor": "Template a sensor"
},
"title": "Template helper"
}
}
},
"options": {
"step": {
"sensor": {
"data": {
"device_class": "[%key:component::template::config::step::sensor::data::device_class%]",
"state_class": "[%key:component::template::config::step::sensor::data::state_class%]",
"state_template": "[%key:component::template::config::step::sensor::data::state_template%]",
"unit_of_measurement": "[%key:component::template::config::step::sensor::data::unit_of_measurement%]"
},
"title": "[%key:component::template::config::step::sensor::title%]"
}
}
},
"selector": {
"sensor_device_class": {
"options": {
"none": "No device class",
"apparent_power": "[%key:component::sensor::entity_component::apparent_power::name%]",
"aqi": "[%key:component::sensor::entity_component::aqi::name%]",
"atmospheric_pressure": "[%key:component::sensor::entity_component::atmospheric_pressure::name%]",
"battery": "[%key:component::sensor::entity_component::battery::name%]",
"carbon_dioxide": "[%key:component::sensor::entity_component::carbon_dioxide::name%]",
"carbon_monoxide": "[%key:component::sensor::entity_component::carbon_monoxide::name%]",
"current": "[%key:component::sensor::entity_component::current::name%]",
"data_rate": "[%key:component::sensor::entity_component::data_rate::name%]",
"data_size": "[%key:component::sensor::entity_component::data_size::name%]",
"date": "[%key:component::sensor::entity_component::date::name%]",
"distance": "[%key:component::sensor::entity_component::distance::name%]",
"duration": "[%key:component::sensor::entity_component::duration::name%]",
"energy": "[%key:component::sensor::entity_component::energy::name%]",
"energy_storage": "[%key:component::sensor::entity_component::energy_storage::name%]",
"frequency": "[%key:component::sensor::entity_component::frequency::name%]",
"gas": "[%key:component::sensor::entity_component::gas::name%]",
"humidity": "[%key:component::sensor::entity_component::humidity::name%]",
"illuminance": "[%key:component::sensor::entity_component::illuminance::name%]",
"irradiance": "[%key:component::sensor::entity_component::irradiance::name%]",
"moisture": "[%key:component::sensor::entity_component::moisture::name%]",
"monetary": "[%key:component::sensor::entity_component::monetary::name%]",
"nitrogen_dioxide": "[%key:component::sensor::entity_component::nitrogen_dioxide::name%]",
"nitrogen_monoxide": "[%key:component::sensor::entity_component::nitrogen_monoxide::name%]",
"nitrous_oxide": "[%key:component::sensor::entity_component::nitrous_oxide::name%]",
"ozone": "[%key:component::sensor::entity_component::ozone::name%]",
"ph": "[%key:component::sensor::entity_component::ph::name%]",
"pm1": "[%key:component::sensor::entity_component::pm1::name%]",
"pm10": "[%key:component::sensor::entity_component::pm10::name%]",
"pm25": "[%key:component::sensor::entity_component::pm25::name%]",
"power": "[%key:component::sensor::entity_component::power::name%]",
"power_factor": "[%key:component::sensor::entity_component::power_factor::name%]",
"precipitation": "[%key:component::sensor::entity_component::precipitation::name%]",
"precipitation_intensity": "[%key:component::sensor::entity_component::precipitation_intensity::name%]",
"pressure": "[%key:component::sensor::entity_component::pressure::name%]",
"reactive_power": "[%key:component::sensor::entity_component::reactive_power::name%]",
"signal_strength": "[%key:component::sensor::entity_component::signal_strength::name%]",
"sound_pressure": "[%key:component::sensor::entity_component::sound_pressure::name%]",
"speed": "[%key:component::sensor::entity_component::speed::name%]",
"sulphur_dioxide": "[%key:component::sensor::entity_component::sulphur_dioxide::name%]",
"temperature": "[%key:component::sensor::entity_component::temperature::name%]",
"timestamp": "[%key:component::sensor::entity_component::timestamp::name%]",
"volatile_organic_compounds": "[%key:component::sensor::entity_component::volatile_organic_compounds::name%]",
"volatile_organic_compounds_parts": "[%key:component::sensor::entity_component::volatile_organic_compounds_parts::name%]",
"voltage": "[%key:component::sensor::entity_component::voltage::name%]",
"volume": "[%key:component::sensor::entity_component::volume::name%]",
"volume_storage": "[%key:component::sensor::entity_component::volume_storage::name%]",
"water": "[%key:component::sensor::entity_component::water::name%]",
"weight": "[%key:component::sensor::entity_component::weight::name%]",
"wind_speed": "[%key:component::sensor::entity_component::wind_speed::name%]"
}
},
"sensor_state_class": {
"options": {
"none": "No state class",
"measurement": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::measurement%]",
"total": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::total%]",
"total_increasing": "[%key:component::sensor::entity_component::_::state_attributes::state_class::state::total_increasing%]"
}
},
"sensor_unit_of_measurement": {
"options": {
"none": "No unit of measurement"
}
}
},
"services": {
"reload": {
"name": "[%key:common::action::reload%]",

View file

@ -138,14 +138,17 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
await super().async_added_to_hass()
if state := await self.async_get_last_state():
self._state = state.state == STATE_ON
await super().async_added_to_hass()
# no need to listen for events
else:
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._template is not None:
self.add_template_attribute(
"_state", self._template, None, self._update_state
)
await super().async_added_to_hass()
super()._async_setup_templates()
@property
def is_on(self) -> bool | None:

View file

@ -1,7 +1,7 @@
"""TemplateEntity utility class."""
from __future__ import annotations
from collections.abc import Callable
from collections.abc import Callable, Mapping
import contextlib
import itertools
import logging
@ -18,7 +18,14 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START,
STATE_UNKNOWN,
)
from homeassistant.core import Context, CoreState, HomeAssistant, State, callback
from homeassistant.core import (
CALLBACK_TYPE,
Context,
CoreState,
HomeAssistant,
State,
callback,
)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
@ -256,6 +263,9 @@ class TemplateEntity(Entity):
self._attr_extra_state_attributes = {}
self._self_ref_update_count = 0
self._attr_unique_id = unique_id
self._preview_callback: Callable[
[str | None, dict[str, Any] | None, str | None], None
] | None = None
if config is None:
self._attribute_templates = attribute_templates
self._availability_template = availability_template
@ -408,9 +418,14 @@ class TemplateEntity(Entity):
event, update.template, update.last_result, update.result
)
self.async_write_ha_state()
if not self._preview_callback:
self.async_write_ha_state()
return
async def _async_template_startup(self, *_: Any) -> None:
self._preview_callback(*self._async_generate_attributes(), None)
@callback
def _async_template_startup(self, *_: Any) -> None:
template_var_tups: list[TrackTemplate] = []
has_availability_template = False
@ -441,8 +456,9 @@ class TemplateEntity(Entity):
self._async_update = result_info.async_refresh
result_info.async_refresh()
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._availability_template is not None:
self.add_template_attribute(
"_attr_available",
@ -467,8 +483,29 @@ class TemplateEntity(Entity):
):
self.add_template_attribute("_attr_name", self._friendly_name_template)
@callback
def async_start_preview(
self,
preview_callback: Callable[
[str | None, Mapping[str, Any] | None, str | None], None
],
) -> CALLBACK_TYPE:
"""Render a preview."""
self._preview_callback = preview_callback
self._async_setup_templates()
try:
self._async_template_startup()
except Exception as err: # pylint: disable=broad-exception-caught
preview_callback(None, None, str(err))
return self._call_on_remove_callbacks
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
self._async_setup_templates()
if self.hass.state == CoreState.running:
await self._async_template_startup()
self._async_template_startup()
return
self.hass.bus.async_listen_once(

View file

@ -264,8 +264,9 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
self._attr_fan_speed_list,
)
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._template is not None:
self.add_template_attribute(
"_state", self._template, None, self._update_state
@ -285,7 +286,7 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
self._update_battery_level,
none_on_template_error=True,
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_state(self, result):

View file

@ -301,8 +301,9 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
return "Powered by Home Assistant"
return self._attribution
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def _async_setup_templates(self) -> None:
"""Set up templates."""
if self._condition_template:
self.add_template_attribute(
@ -398,7 +399,7 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
validator=partial(self._validate_forecast, "twice_daily"),
)
await super().async_added_to_hass()
super()._async_setup_templates()
@callback
def _update_forecast(

View file

@ -10,6 +10,7 @@ FLOWS = {
"integration",
"min_max",
"switch_as_x",
"template",
"threshold",
"tod",
"utility_meter",

View file

@ -5669,12 +5669,6 @@
"config_flow": false,
"iot_class": "local_polling"
},
"template": {
"name": "Template",
"integration_type": "hub",
"config_flow": false,
"iot_class": "local_push"
},
"tensorflow": {
"name": "TensorFlow",
"integration_type": "hub",
@ -6717,6 +6711,12 @@
"config_flow": true,
"iot_class": "calculated"
},
"template": {
"name": "Template",
"integration_type": "helper",
"config_flow": true,
"iot_class": "local_push"
},
"threshold": {
"integration_type": "helper",
"config_flow": true,

View file

@ -0,0 +1,569 @@
"""Test the Switch config flow."""
from typing import Any
from unittest.mock import patch
import pytest
from homeassistant import config_entries
from homeassistant.components.template import DOMAIN, async_setup_entry
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator
@pytest.mark.parametrize(
(
"template_type",
"state_template",
"template_state",
"input_states",
"input_attributes",
"extra_input",
"extra_options",
"extra_attrs",
),
(
(
"sensor",
"{{ float(states('sensor.one')) + float(states('sensor.two')) }}",
"50.0",
{"one": "30.0", "two": "20.0"},
{},
{},
{},
{},
),
),
)
async def test_config_flow(
hass: HomeAssistant,
template_type,
state_template,
template_state,
input_states,
input_attributes,
extra_input,
extra_options,
extra_attrs,
) -> None:
"""Test the config flow."""
input_entities = ["one", "two"]
for input_entity in input_entities:
hass.states.async_set(
f"{template_type}.{input_entity}",
input_states[input_entity],
input_attributes.get(input_entity, {}),
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": template_type},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == template_type
with patch(
"homeassistant.components.template.async_setup_entry", wraps=async_setup_entry
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"name": "My template",
"state": state_template,
**extra_input,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "My template"
assert result["data"] == {}
assert result["options"] == {
"name": "My template",
"state": state_template,
"template_type": template_type,
**extra_options,
}
assert len(mock_setup_entry.mock_calls) == 1
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
assert config_entry.data == {}
assert config_entry.options == {
"name": "My template",
"state": state_template,
"template_type": template_type,
**extra_options,
}
state = hass.states.get(f"{template_type}.my_template")
assert state.state == template_state
for key in extra_attrs:
assert state.attributes[key] == extra_attrs[key]
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise Exception
@pytest.mark.parametrize(
(
"template_type",
"old_state_template",
"new_state_template",
"input_states",
"extra_options",
"options_options",
),
(
(
"sensor",
"{{ float(states('sensor.one')) + float(states('sensor.two')) }}",
"{{ float(states('sensor.one')) - float(states('sensor.two')) }}",
{"one": "30.0", "two": "20.0"},
{},
{},
),
),
)
async def test_options(
hass: HomeAssistant,
template_type,
old_state_template,
new_state_template,
input_states,
extra_options,
options_options,
) -> None:
"""Test reconfiguring."""
input_entities = ["one", "two"]
for input_entity in input_entities:
hass.states.async_set(
f"{template_type}.{input_entity}", input_states[input_entity], {}
)
template_config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"name": "My template",
"state": old_state_template,
"template_type": template_type,
**extra_options,
},
title="My template",
)
template_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(f"{template_type}.my_template")
assert state.state == "50.0"
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == template_type
assert get_suggested(result["data_schema"].schema, "state") == old_state_template
assert "name" not in result["data_schema"].schema
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"state": new_state_template, **options_options},
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["data"] == {
"name": "My template",
"state": new_state_template,
"template_type": template_type,
**extra_options,
}
assert config_entry.data == {}
assert config_entry.options == {
"name": "My template",
"state": new_state_template,
"template_type": template_type,
**extra_options,
}
assert config_entry.title == "My template"
# Check config entry is reloaded with new options
await hass.async_block_till_done()
state = hass.states.get(f"{template_type}.my_template")
assert state.state == "10.0"
# Check we don't get suggestions from another entry
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": template_type},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == template_type
assert get_suggested(result["data_schema"].schema, "name") is None
assert get_suggested(result["data_schema"].schema, "state") is None
@pytest.mark.parametrize(
(
"template_type",
"state_template",
"extra_user_input",
"input_states",
"template_state",
"extra_attributes",
),
(
(
"sensor",
"{{ float(states('sensor.one')) + float(states('sensor.two')) }}",
{},
{"one": "30.0", "two": "20.0"},
"50.0",
[{}, {}],
),
),
)
async def test_config_flow_preview(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
template_type: str,
state_template: str,
extra_user_input: dict[str, Any],
input_states: list[str],
template_state: str,
extra_attributes: list[dict[str, Any]],
) -> None:
"""Test the config flow preview."""
client = await hass_ws_client(hass)
input_entities = ["one", "two"]
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": template_type},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == template_type
assert result["errors"] is None
assert result["preview"] == "template"
await client.send_json_auto_id(
{
"type": "template/start_preview",
"flow_id": result["flow_id"],
"flow_type": "config_flow",
"user_input": {"name": "My template", "state": state_template}
| extra_user_input,
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
msg = await client.receive_json()
assert msg["event"] == {
"attributes": {"friendly_name": "My template"} | extra_attributes[0],
"state": "unavailable",
}
for input_entity in input_entities:
hass.states.async_set(
f"{template_type}.{input_entity}", input_states[input_entity], {}
)
msg = await client.receive_json()
assert msg["event"] == {
"attributes": {"friendly_name": "My template"}
| extra_attributes[0]
| extra_attributes[1],
"state": template_state,
}
assert len(hass.states.async_all()) == 2
EARLY_END_ERROR = "invalid template (TemplateSyntaxError: unexpected 'end of template')"
@pytest.mark.parametrize(
("template_type", "state_template", "extra_user_input", "error"),
[
("sensor", "{{", {}, {"state": EARLY_END_ERROR}),
(
"sensor",
"",
{"device_class": "temperature", "unit_of_measurement": "cats"},
{
"state_class": (
"'None' is not a valid state class for device class 'temperature'; "
"expected one of measurement"
),
"unit_of_measurement": (
"'cats' is not a valid unit for device class 'temperature'; "
"expected one of K, °C, °F"
),
},
),
],
)
async def test_config_flow_preview_bad_input(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
template_type: str,
state_template: str,
extra_user_input: dict[str, str],
error: str,
) -> None:
"""Test the config flow preview."""
client = await hass_ws_client(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": template_type},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == template_type
assert result["errors"] is None
assert result["preview"] == "template"
await client.send_json_auto_id(
{
"type": "template/start_preview",
"flow_id": result["flow_id"],
"flow_type": "config_flow",
"user_input": {"name": "My template", "state": state_template}
| extra_user_input,
}
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"] == {
"code": "invalid_user_input",
"message": error,
}
@pytest.mark.parametrize(
(
"template_type",
"state_template",
"extra_user_input",
),
(
(
"sensor",
"{{ states('sensor.one') }}",
{"unit_of_measurement": "°C"},
),
),
)
async def test_config_flow_preview_bad_state(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
template_type: str,
state_template: str,
extra_user_input: dict[str, Any],
) -> None:
"""Test the config flow preview."""
client = await hass_ws_client(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": template_type},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == template_type
assert result["errors"] is None
assert result["preview"] == "template"
await client.send_json_auto_id(
{
"type": "template/start_preview",
"flow_id": result["flow_id"],
"flow_type": "config_flow",
"user_input": {"name": "My template", "state": state_template}
| extra_user_input,
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
msg = await client.receive_json()
assert msg["event"] == {
"error": (
"Sensor None has device class 'None', state class 'None' unit '°C' "
"and suggested precision 'None' thus indicating it has a numeric "
"value; however, it has the non-numeric value: 'unknown' (<class "
"'str'>)"
),
}
@pytest.mark.parametrize(
(
"template_type",
"old_state_template",
"new_state_template",
"extra_config_flow_data",
"extra_user_input",
"input_states",
"template_state",
"extra_attributes",
),
[
(
"sensor",
"{{ float(states('sensor.one')) + float(states('sensor.two')) }}",
"{{ float(states('sensor.one')) - float(states('sensor.two')) }}",
{},
{},
{"one": "30.0", "two": "20.0"},
"10.0",
{},
),
],
)
async def test_option_flow_preview(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
template_type: str,
old_state_template: str,
new_state_template: str,
extra_config_flow_data: dict[str, Any],
extra_user_input: dict[str, Any],
input_states: list[str],
template_state: str,
extra_attributes: dict[str, Any],
) -> None:
"""Test the option flow preview."""
client = await hass_ws_client(hass)
input_entities = input_entities = ["one", "two"]
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"name": "My template",
"state": old_state_template,
"template_type": template_type,
}
| extra_config_flow_data,
title="My template",
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
assert result["preview"] == "template"
for input_entity in input_entities:
hass.states.async_set(
f"{template_type}.{input_entity}", input_states[input_entity], {}
)
await client.send_json_auto_id(
{
"type": "template/start_preview",
"flow_id": result["flow_id"],
"flow_type": "options_flow",
"user_input": {"state": new_state_template} | extra_user_input,
}
)
msg = await client.receive_json()
assert msg["success"]
assert msg["result"] is None
msg = await client.receive_json()
assert msg["event"] == {
"attributes": {"friendly_name": "My template"} | extra_attributes,
"state": template_state,
}
assert len(hass.states.async_all()) == 3
async def test_option_flow_sensor_preview_config_entry_removed(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None:
"""Test the option flow preview where the config entry is removed."""
client = await hass_ws_client(hass)
# Setup the config entry
config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"name": "My template",
"state": "Hello!",
"template_type": "sensor",
},
title="My template",
)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
assert result["preview"] == "template"
await hass.config_entries.async_remove(config_entry.entry_id)
await client.send_json_auto_id(
{
"type": "template/start_preview",
"flow_id": result["flow_id"],
"flow_type": "options_flow",
"user_input": {"state": "Goodbye!"},
}
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"] == {"code": "unknown_error", "message": "Unknown error"}

View file

@ -725,8 +725,9 @@ async def test_this_variable_early_hass_not_running(
# Signal hass started
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
await hass.async_block_till_done()
# Re-render icon, name, pciture + other templates now rendered
# icon, name, picture + other templates now re-rendered
state = hass.states.get(entity_id)
assert state.state == "sensor.none_false: sensor.none_false"
assert state.attributes == {