From c4653be2faad5f03c6984f576581e9b9315ee3eb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Feb 2024 10:37:46 -0600 Subject: [PATCH] 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 --- .../components/template/coordinator.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/template/coordinator.py b/homeassistant/components/template/coordinator.py index 5ac2b7efa67..047d58d9208 100644 --- a/homeassistant/components/template/coordinator.py +++ b/homeassistant/components/template/coordinator.py @@ -74,21 +74,28 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator): if start_event is not 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.hass, self.config[CONF_TRIGGER], - self._handle_triggered, + action, DOMAIN, self.name, self.logger.log, start_event is not None, ) - async def _handle_triggered(self, run_variables, context=None): - if self._script: - script_result = await self._script.async_run(run_variables, context) - if script_result: - run_variables = script_result.variables + async def _handle_triggered_with_script(self, run_variables, context=None): + if script_result := await self._script.async_run(run_variables, context): + 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( {"run_variables": run_variables, "context": context} )