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 taskpull/110770/head^2
parent
a4150fe8b2
commit
c4653be2fa
|
@ -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}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue