Do not trigger when template is true at startup (#46423)
This commit is contained in:
parent
855bd653b4
commit
f8f86fbe48
2 changed files with 94 additions and 6 deletions
|
@ -37,21 +37,49 @@ async def async_attach_trigger(
|
|||
template.attach(hass, time_delta)
|
||||
delay_cancel = None
|
||||
job = HassJob(action)
|
||||
armed = False
|
||||
|
||||
# Arm at setup if the template is already false.
|
||||
try:
|
||||
if not result_as_boolean(value_template.async_render()):
|
||||
armed = True
|
||||
except exceptions.TemplateError as ex:
|
||||
_LOGGER.warning(
|
||||
"Error initializing 'template' trigger for '%s': %s",
|
||||
automation_info["name"],
|
||||
ex,
|
||||
)
|
||||
|
||||
@callback
|
||||
def template_listener(event, updates):
|
||||
"""Listen for state changes and calls action."""
|
||||
nonlocal delay_cancel
|
||||
nonlocal delay_cancel, armed
|
||||
result = updates.pop().result
|
||||
|
||||
if isinstance(result, exceptions.TemplateError):
|
||||
_LOGGER.warning(
|
||||
"Error evaluating 'template' trigger for '%s': %s",
|
||||
automation_info["name"],
|
||||
result,
|
||||
)
|
||||
return
|
||||
|
||||
if delay_cancel:
|
||||
# pylint: disable=not-callable
|
||||
delay_cancel()
|
||||
delay_cancel = None
|
||||
|
||||
if not result_as_boolean(result):
|
||||
armed = True
|
||||
return
|
||||
|
||||
# Only fire when previously armed.
|
||||
if not armed:
|
||||
return
|
||||
|
||||
# Fire!
|
||||
armed = False
|
||||
|
||||
entity_id = event and event.data.get("entity_id")
|
||||
from_s = event and event.data.get("old_state")
|
||||
to_s = event and event.data.get("new_state")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue