mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Fix template triggers from time events (#44603)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
@@ -11,6 +11,7 @@ from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import (
|
||||
assert_setup_component,
|
||||
async_fire_time_changed,
|
||||
@@ -626,6 +627,7 @@ async def test_if_fires_on_change_with_for_0_advanced(hass, calls):
|
||||
|
||||
async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
context = Context()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
@@ -636,17 +638,33 @@ async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": 5,
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
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
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - 0:00:05"
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_with_for(hass, calls):
|
||||
@@ -811,3 +829,58 @@ async def test_invalid_for_template_1(hass, calls):
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert mock_logger.error.called
|
||||
|
||||
|
||||
async def test_if_fires_on_time_change(hass, calls):
|
||||
"""Test for firing on time changes."""
|
||||
start_time = dt_util.utcnow() + timedelta(hours=24)
|
||||
time_that_will_not_match_right_away = start_time.replace(minute=1, second=0)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ utcnow().minute % 2 == 0 }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# Trigger once (match template)
|
||||
first_time = start_time.replace(minute=2, second=0)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=first_time):
|
||||
async_fire_time_changed(hass, first_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# Trigger again (match template)
|
||||
second_time = start_time.replace(minute=4, second=0)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=second_time):
|
||||
async_fire_time_changed(hass, second_time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# Trigger again (do not match template)
|
||||
third_time = start_time.replace(minute=5, second=0)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=third_time):
|
||||
async_fire_time_changed(hass, third_time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# Trigger again (match template)
|
||||
forth_time = start_time.replace(minute=8, second=0)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=forth_time):
|
||||
async_fire_time_changed(hass, forth_time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
Reference in New Issue
Block a user