mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Create base TriggerEntity (#91128)
* Trigger entity base class * mods * TriggerEntity to CoordinatorTriggerEntity * variables to variable * mypy * unique_id * Fix * docstring * _render_templates * split manual vs coordinator * name * ManualTriggerEntity * value * use super * Remove ManualTriggerEntity * Use super()
This commit is contained in:
parent
bd22e0bd43
commit
afc9e4303a
@ -16,6 +16,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant, State, callback
|
from homeassistant.core import HomeAssistant, State, callback
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import TriggerUpdateCoordinator
|
from . import TriggerUpdateCoordinator
|
||||||
@ -28,29 +29,23 @@ CONF_TO_ATTRIBUTE = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class TriggerEntity(CoordinatorEntity[TriggerUpdateCoordinator]):
|
class TriggerBaseEntity(Entity):
|
||||||
"""Template entity based on trigger data."""
|
"""Template Base entity based on trigger data."""
|
||||||
|
|
||||||
domain: str
|
domain: str
|
||||||
extra_template_keys: tuple | None = None
|
extra_template_keys: tuple | None = None
|
||||||
extra_template_keys_complex: tuple | None = None
|
extra_template_keys_complex: tuple | None = None
|
||||||
|
_unique_id: str | None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
coordinator: TriggerUpdateCoordinator,
|
|
||||||
config: dict,
|
config: dict,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
super().__init__(coordinator)
|
self.hass = hass
|
||||||
|
|
||||||
entity_unique_id = config.get(CONF_UNIQUE_ID)
|
self._set_unique_id(config.get(CONF_UNIQUE_ID))
|
||||||
|
|
||||||
self._unique_id: str | None
|
|
||||||
if entity_unique_id and coordinator.unique_id:
|
|
||||||
self._unique_id = f"{coordinator.unique_id}-{entity_unique_id}"
|
|
||||||
else:
|
|
||||||
self._unique_id = entity_unique_id
|
|
||||||
|
|
||||||
self._config = config
|
self._config = config
|
||||||
|
|
||||||
@ -125,9 +120,10 @@ class TriggerEntity(CoordinatorEntity[TriggerUpdateCoordinator]):
|
|||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Handle being added to Home Assistant."""
|
"""Handle being added to Home Assistant."""
|
||||||
template.attach(self.hass, self._config)
|
template.attach(self.hass, self._config)
|
||||||
await super().async_added_to_hass()
|
|
||||||
if self.coordinator.data is not None:
|
def _set_unique_id(self, unique_id: str | None) -> None:
|
||||||
self._process_data()
|
"""Set unique id."""
|
||||||
|
self._unique_id = unique_id
|
||||||
|
|
||||||
def restore_attributes(self, last_state: State) -> None:
|
def restore_attributes(self, last_state: State) -> None:
|
||||||
"""Restore attributes."""
|
"""Restore attributes."""
|
||||||
@ -144,16 +140,8 @@ class TriggerEntity(CoordinatorEntity[TriggerUpdateCoordinator]):
|
|||||||
extra_state_attributes[attr] = last_state.attributes[attr]
|
extra_state_attributes[attr] = last_state.attributes[attr]
|
||||||
self._rendered[CONF_ATTRIBUTES] = extra_state_attributes
|
self._rendered[CONF_ATTRIBUTES] = extra_state_attributes
|
||||||
|
|
||||||
@callback
|
def _render_templates(self, variables: dict[str, Any]) -> None:
|
||||||
def _process_data(self) -> None:
|
"""Render templates."""
|
||||||
"""Process new data."""
|
|
||||||
|
|
||||||
this = None
|
|
||||||
if state := self.hass.states.get(self.entity_id):
|
|
||||||
this = state.as_dict()
|
|
||||||
run_variables = self.coordinator.data["run_variables"]
|
|
||||||
variables = {"this": this, **(run_variables or {})}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rendered = dict(self._static_rendered)
|
rendered = dict(self._static_rendered)
|
||||||
|
|
||||||
@ -182,6 +170,46 @@ class TriggerEntity(CoordinatorEntity[TriggerUpdateCoordinator]):
|
|||||||
)
|
)
|
||||||
self._rendered = self._static_rendered
|
self._rendered = self._static_rendered
|
||||||
|
|
||||||
|
|
||||||
|
class TriggerEntity(TriggerBaseEntity, CoordinatorEntity[TriggerUpdateCoordinator]):
|
||||||
|
"""Template entity based on trigger data."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
coordinator: TriggerUpdateCoordinator,
|
||||||
|
config: dict,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the entity."""
|
||||||
|
super(CoordinatorEntity, self).__init__(coordinator)
|
||||||
|
super().__init__(hass, config)
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Handle being added to Home Assistant."""
|
||||||
|
await super().async_added_to_hass()
|
||||||
|
await super(CoordinatorEntity, self).async_added_to_hass()
|
||||||
|
if self.coordinator.data is not None:
|
||||||
|
self._process_data()
|
||||||
|
|
||||||
|
def _set_unique_id(self, unique_id: str | None) -> None:
|
||||||
|
"""Set unique id."""
|
||||||
|
if unique_id and self.coordinator.unique_id:
|
||||||
|
self._unique_id = f"{self.coordinator.unique_id}-{unique_id}"
|
||||||
|
else:
|
||||||
|
self._unique_id = unique_id
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _process_data(self) -> None:
|
||||||
|
"""Process new data."""
|
||||||
|
|
||||||
|
this = None
|
||||||
|
if state := self.hass.states.get(self.entity_id):
|
||||||
|
this = state.as_dict()
|
||||||
|
run_variables = self.coordinator.data["run_variables"]
|
||||||
|
variables = {"this": this, **(run_variables or {})}
|
||||||
|
|
||||||
|
self._render_templates(variables)
|
||||||
|
|
||||||
self.async_set_context(self.coordinator.data["context"])
|
self.async_set_context(self.coordinator.data["context"])
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user