mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Make template trigger callbacks when nothing needs to be awaited (#110771)
async_initialize_triggers supports a coro or a callback, and in most cases the user will not have configured a script so we can avoid creating a task
This commit is contained in:
parent
a4150fe8b2
commit
c4653be2fa
@ -74,21 +74,28 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
if start_event is not None:
|
if start_event is not None:
|
||||||
self._unsub_start = None
|
self._unsub_start = None
|
||||||
|
|
||||||
|
if self._script:
|
||||||
|
action: Callable = self._handle_triggered_with_script
|
||||||
|
else:
|
||||||
|
action = self._handle_triggered
|
||||||
|
|
||||||
self._unsub_trigger = await trigger_helper.async_initialize_triggers(
|
self._unsub_trigger = await trigger_helper.async_initialize_triggers(
|
||||||
self.hass,
|
self.hass,
|
||||||
self.config[CONF_TRIGGER],
|
self.config[CONF_TRIGGER],
|
||||||
self._handle_triggered,
|
action,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
self.name,
|
self.name,
|
||||||
self.logger.log,
|
self.logger.log,
|
||||||
start_event is not None,
|
start_event is not None,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _handle_triggered(self, run_variables, context=None):
|
async def _handle_triggered_with_script(self, run_variables, context=None):
|
||||||
if self._script:
|
if script_result := await self._script.async_run(run_variables, context):
|
||||||
script_result = await self._script.async_run(run_variables, context)
|
|
||||||
if script_result:
|
|
||||||
run_variables = script_result.variables
|
run_variables = script_result.variables
|
||||||
|
self._handle_triggered(run_variables, context)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_triggered(self, run_variables, context=None):
|
||||||
self.async_set_updated_data(
|
self.async_set_updated_data(
|
||||||
{"run_variables": run_variables, "context": context}
|
{"run_variables": run_variables, "context": context}
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user