mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Add trigger_variables to template trigger 'for' field (#136672)
* Add trigger_variables to template trigger for * address comments
This commit is contained in:
parent
a8ecdb3bff
commit
752c73a2ed
@ -48,6 +48,7 @@ async def async_attach_trigger(
|
|||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Listen for state changes based on configuration."""
|
"""Listen for state changes based on configuration."""
|
||||||
trigger_data = trigger_info["trigger_data"]
|
trigger_data = trigger_info["trigger_data"]
|
||||||
|
variables = trigger_info["variables"] or {}
|
||||||
value_template: Template = config[CONF_VALUE_TEMPLATE]
|
value_template: Template = config[CONF_VALUE_TEMPLATE]
|
||||||
time_delta = config.get(CONF_FOR)
|
time_delta = config.get(CONF_FOR)
|
||||||
delay_cancel = None
|
delay_cancel = None
|
||||||
@ -56,9 +57,7 @@ async def async_attach_trigger(
|
|||||||
|
|
||||||
# Arm at setup if the template is already false.
|
# Arm at setup if the template is already false.
|
||||||
try:
|
try:
|
||||||
if not result_as_boolean(
|
if not result_as_boolean(value_template.async_render(variables)):
|
||||||
value_template.async_render(trigger_info["variables"])
|
|
||||||
):
|
|
||||||
armed = True
|
armed = True
|
||||||
except exceptions.TemplateError as ex:
|
except exceptions.TemplateError as ex:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
@ -134,9 +133,12 @@ async def async_attach_trigger(
|
|||||||
call_action()
|
call_action()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
data = {"trigger": template_variables}
|
||||||
|
period_variables = {**variables, **data}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
period: timedelta = cv.positive_time_period(
|
period: timedelta = cv.positive_time_period(
|
||||||
template.render_complex(time_delta, {"trigger": template_variables})
|
template.render_complex(time_delta, period_variables)
|
||||||
)
|
)
|
||||||
except (exceptions.TemplateError, vol.Invalid) as ex:
|
except (exceptions.TemplateError, vol.Invalid) as ex:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
@ -150,7 +152,7 @@ async def async_attach_trigger(
|
|||||||
|
|
||||||
info = async_track_template_result(
|
info = async_track_template_result(
|
||||||
hass,
|
hass,
|
||||||
[TrackTemplate(value_template, trigger_info["variables"])],
|
[TrackTemplate(value_template, variables)],
|
||||||
template_listener,
|
template_listener,
|
||||||
)
|
)
|
||||||
unsub = info.async_remove
|
unsub = info.async_remove
|
||||||
|
@ -788,6 +788,39 @@ async def test_if_fires_on_change_with_for_template_3(
|
|||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(("count", "domain"), [(1, automation.DOMAIN)])
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"config",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
automation.DOMAIN: {
|
||||||
|
"trigger_variables": {
|
||||||
|
"seconds": 5,
|
||||||
|
"entity": "test.entity",
|
||||||
|
},
|
||||||
|
"trigger": {
|
||||||
|
"platform": "template",
|
||||||
|
"value_template": "{{ is_state(entity, 'world') }}",
|
||||||
|
"for": "{{ seconds }}",
|
||||||
|
},
|
||||||
|
"action": {"service": "test.automation"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@pytest.mark.usefixtures("start_ha")
|
||||||
|
async def test_if_fires_on_change_with_for_template_4(
|
||||||
|
hass: HomeAssistant, calls: list[ServiceCall]
|
||||||
|
) -> None:
|
||||||
|
"""Test for firing on change with for template."""
|
||||||
|
hass.states.async_set("test.entity", "world")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 0
|
||||||
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(("count", "domain"), [(1, automation.DOMAIN)])
|
@pytest.mark.parametrize(("count", "domain"), [(1, automation.DOMAIN)])
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"config",
|
"config",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user