1 """TemplateEntity utility class."""
3 from __future__
import annotations
9 import voluptuous
as vol
13 DEVICE_CLASSES_SCHEMA,
25 CONF_UNIT_OF_MEASUREMENT,
31 from .
import config_validation
as cv
32 from .entity
import Entity
33 from .template
import TemplateStateFromEntityId, render_complex
34 from .typing
import ConfigType
36 CONF_AVAILABILITY =
"availability"
37 CONF_ATTRIBUTES =
"attributes"
38 CONF_PICTURE =
"picture"
42 CONF_NAME: ATTR_FRIENDLY_NAME,
43 CONF_PICTURE: ATTR_ENTITY_PICTURE,
46 TEMPLATE_ENTITY_BASE_SCHEMA = vol.Schema(
48 vol.Optional(CONF_ICON): cv.template,
49 vol.Optional(CONF_NAME): cv.template,
50 vol.Optional(CONF_PICTURE): cv.template,
51 vol.Optional(CONF_UNIQUE_ID): cv.string,
57 """Return a schema with default name."""
60 vol.Optional(CONF_ICON): cv.template,
61 vol.Optional(CONF_NAME, default=default_name): cv.template,
62 vol.Optional(CONF_PICTURE): cv.template,
63 vol.Optional(CONF_UNIQUE_ID): cv.string,
68 TEMPLATE_SENSOR_BASE_SCHEMA = vol.Schema(
70 vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
71 vol.Optional(CONF_STATE_CLASS): STATE_CLASSES_SCHEMA,
72 vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
74 ).extend(TEMPLATE_ENTITY_BASE_SCHEMA.schema)
78 """Template Base entity based on trigger data."""
81 extra_template_keys: tuple[str, ...] |
None =
None
82 extra_template_keys_complex: tuple[str, ...] |
None =
None
83 _unique_id: str |
None
90 """Initialize the entity."""
98 self._to_render_simple: list[str] = []
99 self._to_render_complex: list[str] = []
107 if itm
not in config
or config[itm]
is None:
109 if config[itm].is_static:
112 self._to_render_simple.append(itm)
114 if self.extra_template_keys
is not None:
115 self._to_render_simple.extend(self.extra_template_keys)
117 if self.extra_template_keys_complex
is not None:
118 self._to_render_complex.extend(self.extra_template_keys_complex)
127 """Name of the entity."""
132 """Return unique ID of the entity."""
142 """Return entity picture."""
147 """Return availability of the entity."""
152 self.
_rendered_rendered.
get(CONF_AVAILABILITY)
is not False
157 """Return extra attributes."""
165 """Restore attributes."""
166 for conf_key, attr
in CONF_TO_ATTRIBUTE.items():
167 if conf_key
not in self.
_config_config
or attr
not in last_state.attributes:
169 self.
_rendered_rendered[conf_key] = last_state.attributes[attr]
171 if CONF_ATTRIBUTES
in self.
_config_config:
172 extra_state_attributes = {}
173 for attr
in self.
_config_config[CONF_ATTRIBUTES]:
174 if attr
not in last_state.attributes:
176 extra_state_attributes[attr] = last_state.attributes[attr]
177 self.
_rendered_rendered[CONF_ATTRIBUTES] = extra_state_attributes
180 """Render templates."""
184 for key
in self._to_render_simple:
185 rendered[key] = self.
_config_config[key].async_render(
190 for key
in self._to_render_complex:
196 if CONF_ATTRIBUTES
in self.
_config_config:
198 self.
_config_config[CONF_ATTRIBUTES],
203 except TemplateError
as err:
204 logging.getLogger(f
"{__package__}.{self.entity_id.split('.')[0]}").error(
205 "Error rendering %s template for %s: %s", key, self.
entity_identity_id, err
211 """Template entity based on manual trigger data."""
218 """Initialize the entity."""
219 TriggerBaseEntity.__init__(self, hass, config)
221 self.
_rendered_rendered[CONF_NAME] = config[CONF_NAME].async_render(
228 """Process new data manually.
230 Implementing class should call this last in update method to render templates.
231 Ex: self._process_manual_data(payload)
234 run_variables: dict[str, Any] = {
"value": value}
236 with contextlib.suppress(*JSON_DECODE_EXCEPTIONS):
237 run_variables[
"value_json"] =
json_loads(run_variables[
"value"])
240 **(run_variables
or {}),
247 """Template entity based on manual trigger data for sensor."""
254 """Initialize the sensor entity."""
255 ManualTriggerEntity.__init__(self, hass, config)
None __init__(self, HomeAssistant hass, ConfigType config)
None _process_manual_data(self, Any|None value=None)
None __init__(self, HomeAssistant hass, ConfigType config)
_attr_native_unit_of_measurement
str|None entity_picture(self)
dict[str, Any]|None extra_state_attributes(self)
None _render_templates(self, dict[str, Any] variables)
None _set_unique_id(self, str|None unique_id)
None __init__(self, HomeAssistant hass, ConfigType config)
None restore_attributes(self, State last_state)
web.Response get(self, web.Request request, str config_key)
Any render_complex(Any value, TemplateVarsType variables=None, bool limited=False, bool parse_result=True)
vol.Schema make_template_entity_base_schema(str default_name)