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
pull/110770/head^2
J. Nick Koston 2024-02-17 10:37:46 -06:00 committed by GitHub
parent a4150fe8b2
commit c4653be2fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 6 deletions

View File

@ -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}
)