1 """Data update coordinator for trigger based template entities."""
3 from collections.abc
import Callable, Mapping
5 from typing
import TYPE_CHECKING, Any
15 from .const
import CONF_ACTION, CONF_CONDITION, CONF_TRIGGER, DOMAIN, PLATFORMS
17 _LOGGER = logging.getLogger(__name__)
21 """Data update coordinator for trigger based template entities."""
23 REMOVE_TRIGGER = object()
25 def __init__(self, hass: HomeAssistant, config: dict[str, Any]) ->
None:
26 """Instantiate trigger data."""
28 hass, _LOGGER, config_entry=
None, name=
"Trigger Update Coordinator"
31 self.
_cond_func_cond_func: Callable[[Mapping[str, Any] |
None], bool] |
None =
None
32 self.
_unsub_start_unsub_start: Callable[[],
None] |
None =
None
33 self.
_unsub_trigger_unsub_trigger: Callable[[],
None] |
None =
None
34 self.
_script_script: Script |
None =
None
38 """Return unique ID for the entity."""
43 """Signal that the entities need to remove themselves."""
50 """Set up the trigger and create entities."""
51 if self.
hasshass.state
is CoreState.running:
58 for platform_domain
in PLATFORMS:
59 if platform_domain
in self.
configconfig:
60 self.
hasshass.async_create_task(
61 discovery.async_load_platform(
65 {
"coordinator": self,
"entities": self.
configconfig[platform_domain]},
72 """Attach the triggers."""
73 if CONF_ACTION
in self.
configconfig:
76 self.
configconfig[CONF_ACTION],
81 if CONF_CONDITION
in self.
configconfig:
82 self.
_cond_func_cond_func = await condition.async_conditions_from_config(
83 self.
hasshass, self.
configconfig[CONF_CONDITION], _LOGGER,
"template entity"
86 if start_event
is not None:
94 self.
_unsub_trigger_unsub_trigger = await trigger_helper.async_initialize_triggers(
96 self.
configconfig[CONF_TRIGGER],
101 start_event
is not None,
105 self, run_variables: TemplateVarsType, context: Context |
None =
None
110 trigger_context_id =
None if context
is None else context.id
111 script_context =
Context(parent_id=trigger_context_id)
114 assert self.
_script_script
is not None
115 if script_result := await self.
_script_script.
async_run(run_variables, script_context):
116 run_variables = script_result.variables
120 self, run_variables: TemplateVarsType, context: Context |
None =
None
129 condition_result = self.
_cond_func_cond_func(run_variables)
130 if condition_result
is False:
132 "Conditions not met, aborting template trigger update. Condition summary: %s",
135 return condition_result
139 self, run_variables: TemplateVarsType, context: Context |
None =
None
142 {
"run_variables": run_variables,
"context": context}
None _handle_triggered_with_script(self, TemplateVarsType run_variables, Context|None context=None)
bool _check_condition(self, TemplateVarsType run_variables)
None async_setup(self, ConfigType hass_config)
None _attach_triggers(self, Event|None start_event=None)
None _handle_triggered(self, TemplateVarsType run_variables, Context|None context=None)
None _execute_update(self, TemplateVarsType run_variables, Context|None context=None)
None __init__(self, HomeAssistant hass, dict[str, Any] config)
None async_set_updated_data(self, _DataT data)
web.Response get(self, web.Request request, str config_key)
dict[str, deque[TraceElement]]|None trace_get(bool clear=True)
def async_run(config_dir)