Switch async_track_same_state to use async_call_later (#99219)

* Switch async_track_same_state to use async_call_later

There was no need to use async_track_point_in_utc_time here since
we only need a delay

* update trigger tests

* remove some more utcnow patching

* remove some more utcnow patching

* remove some more utcnow patching
pull/99238/head
J. Nick Koston 2023-08-28 15:30:20 -05:00 committed by GitHub
parent 95c03b4192
commit 0e6b3d6583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 249 additions and 280 deletions

View File

@ -1327,9 +1327,7 @@ def async_track_same_state(
if not async_check_same_func(entity, from_state, to_state):
clear_listener()
async_remove_state_for_listener = async_track_point_in_utc_time(
hass, state_for_listener, dt_util.utcnow() + period
)
async_remove_state_for_listener = async_call_later(hass, period, state_for_listener)
if entity_ids == MATCH_ALL:
async_remove_state_for_cancel = hass.bus.async_listen(

View File

@ -3,6 +3,7 @@ from datetime import timedelta
import logging
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
import voluptuous as vol
@ -1147,7 +1148,7 @@ async def test_if_fails_setup_for_without_above_below(
),
)
async def test_if_not_fires_on_entity_change_with_for(
hass: HomeAssistant, calls, above, below
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, above, below
) -> None:
"""Test for not firing on entity change with for."""
assert await async_setup_component(
@ -1171,7 +1172,8 @@ async def test_if_not_fires_on_entity_change_with_for(
await hass.async_block_till_done()
hass.states.async_set("test.entity", 15)
await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 0
@ -1244,7 +1246,7 @@ async def test_if_not_fires_on_entities_change_with_for_after_stop(
),
)
async def test_if_fires_on_entity_change_with_for_attribute_change(
hass: HomeAssistant, calls, above, below
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, above, below
) -> None:
"""Test for firing on entity change with for and attribute change."""
hass.states.async_set("test.entity", 0)
@ -1267,20 +1269,17 @@ async def test_if_fires_on_entity_change_with_for_attribute_change(
},
)
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=4)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity", 9, attributes={"mock_attr": "attr_change"})
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=4)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
hass.states.async_set("test.entity", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=4))
async_fire_time_changed(hass)
hass.states.async_set("test.entity", 9, attributes={"mock_attr": "attr_change"})
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=4))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
@pytest.mark.parametrize(
@ -1374,7 +1373,7 @@ async def test_wait_template_with_trigger(hass: HomeAssistant, calls, above) ->
),
)
async def test_if_fires_on_entities_change_no_overlap(
hass: HomeAssistant, calls, above, below
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, above, below
) -> None:
"""Test for firing on entities change with no overlap."""
hass.states.async_set("test.entity_1", 0)
@ -1402,24 +1401,21 @@ async def test_if_fires_on_entities_change_no_overlap(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=10)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=10)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
@pytest.mark.parametrize(
@ -1432,7 +1428,7 @@ async def test_if_fires_on_entities_change_no_overlap(
),
)
async def test_if_fires_on_entities_change_overlap(
hass: HomeAssistant, calls, above, below
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, above, below
) -> None:
"""Test for firing on entities change with overlap."""
hass.states.async_set("test.entity_1", 0)
@ -1460,35 +1456,32 @@ async def test_if_fires_on_entities_change_overlap(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 15)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 15)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
@pytest.mark.parametrize(
@ -1699,7 +1692,7 @@ async def test_invalid_for_template(hass: HomeAssistant, calls, above, below) ->
),
)
async def test_if_fires_on_entities_change_overlap_for_template(
hass: HomeAssistant, calls, above, below
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, above, below
) -> None:
"""Test for firing on entities change with overlap and for template."""
hass.states.async_set("test.entity_1", 0)
@ -1730,39 +1723,36 @@ async def test_if_fires_on_entities_change_overlap_for_template(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.util.dt.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 15)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 15)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
mock_utcnow.return_value += timedelta(seconds=5)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"
async def test_below_above(hass: HomeAssistant) -> None:
@ -1861,7 +1851,9 @@ async def test_attribute_if_not_fires_on_entities_change_with_for_after_stop(
("above", "below"),
((8, 12),),
)
async def test_variables_priority(hass: HomeAssistant, calls, above, below) -> None:
async def test_variables_priority(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls, above, below
) -> None:
"""Test an externally defined trigger variable is overridden."""
hass.states.async_set("test.entity_1", 0)
hass.states.async_set("test.entity_2", 0)
@ -1892,29 +1884,26 @@ async def test_variables_priority(hass: HomeAssistant, calls, above, below) -> N
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.util.dt.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 15)
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
hass.states.async_set("test.entity_1", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 15)
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", 9)
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
@pytest.mark.parametrize("multiplier", (1, 5))

View File

@ -2,6 +2,7 @@
from datetime import timedelta
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
import homeassistant.components.automation as automation
@ -695,7 +696,7 @@ async def test_if_not_fires_on_entities_change_with_for_after_stop(
async def test_if_fires_on_entity_change_with_for_attribute_change(
hass: HomeAssistant, calls
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on entity change with for and attribute change."""
assert await async_setup_component(
@ -715,26 +716,23 @@ async def test_if_fires_on_entity_change_with_for_attribute_change(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=4)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set(
"test.entity", "world", attributes={"mock_attr": "attr_change"}
)
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=4)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
hass.states.async_set("test.entity", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=4))
async_fire_time_changed(hass)
hass.states.async_set(
"test.entity", "world", attributes={"mock_attr": "attr_change"}
)
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=4))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_if_fires_on_entity_change_with_for_multiple_force_update(
hass: HomeAssistant, calls
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on entity change with for and force update."""
assert await async_setup_component(
@ -754,21 +752,18 @@ async def test_if_fires_on_entity_change_with_for_multiple_force_update(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.force_entity", "world", None, True)
await hass.async_block_till_done()
for _ in range(4):
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.force_entity", "world", None, True)
await hass.async_block_till_done()
for _ in range(4):
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.force_entity", "world", None, True)
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=4)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert len(calls) == 0
freezer.tick(timedelta(seconds=4))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_if_fires_on_entity_change_with_for(hass: HomeAssistant, calls) -> None:
@ -837,7 +832,7 @@ async def test_if_fires_on_entity_change_with_for_without_to(
async def test_if_does_not_fires_on_entity_change_with_for_without_to_2(
hass: HomeAssistant, calls
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on entity change with for."""
assert await async_setup_component(
@ -856,17 +851,12 @@ async def test_if_does_not_fires_on_entity_change_with_for_without_to_2(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
for i in range(10):
hass.states.async_set("test.entity", str(i))
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
for i in range(10):
hass.states.async_set("test.entity", str(i))
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 0
@ -1110,7 +1100,7 @@ async def test_wait_template_with_trigger(hass: HomeAssistant, calls) -> None:
async def test_if_fires_on_entities_change_no_overlap(
hass: HomeAssistant, calls
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on entities change with no overlap."""
assert await async_setup_component(
@ -1133,27 +1123,26 @@ async def test_if_fires_on_entities_change_no_overlap(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=10)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=10)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=10))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
async def test_if_fires_on_entities_change_overlap(hass: HomeAssistant, calls) -> None:
async def test_if_fires_on_entities_change_overlap(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on entities change with overlap."""
assert await async_setup_component(
hass,
@ -1175,35 +1164,32 @@ async def test_if_fires_on_entities_change_overlap(hass: HomeAssistant, calls) -
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "hello")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "hello")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1"
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2"
async def test_if_fires_on_change_with_for_template_1(
@ -1402,7 +1388,7 @@ async def test_invalid_for_template_1(hass: HomeAssistant, calls) -> None:
async def test_if_fires_on_entities_change_overlap_for_template(
hass: HomeAssistant, calls
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test for firing on entities change with overlap and for template."""
assert await async_setup_component(
@ -1428,39 +1414,36 @@ async def test_if_fires_on_entities_change_overlap_for_template(
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "hello")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "hello")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
mock_utcnow.return_value += timedelta(seconds=5)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"
async def test_attribute_if_fires_on_entity_change_with_both_filters(
@ -1702,7 +1685,9 @@ async def test_attribute_if_fires_on_entity_change_with_both_filters_boolean(
assert len(calls) == 1
async def test_variables_priority(hass: HomeAssistant, calls) -> None:
async def test_variables_priority(
hass: HomeAssistant, freezer: FrozenDateTimeFactory, calls
) -> None:
"""Test an externally defined trigger variable is overridden."""
assert await async_setup_component(
hass,
@ -1728,36 +1713,33 @@ async def test_variables_priority(hass: HomeAssistant, calls) -> None:
)
await hass.async_block_till_done()
utcnow = dt_util.utcnow()
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
mock_utcnow.return_value = utcnow
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "hello")
await hass.async_block_till_done()
mock_utcnow.return_value += timedelta(seconds=1)
async_fire_time_changed(hass, mock_utcnow.return_value)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
assert len(calls) == 0
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
hass.states.async_set("test.entity_1", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "hello")
await hass.async_block_till_done()
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
hass.states.async_set("test.entity_2", "world")
await hass.async_block_till_done()
assert len(calls) == 0
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "test.entity_1 - 0:00:05"
mock_utcnow.return_value += timedelta(seconds=3)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 1
mock_utcnow.return_value += timedelta(seconds=5)
async_fire_time_changed(hass, mock_utcnow.return_value)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"
freezer.tick(timedelta(seconds=3))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 1
freezer.tick(timedelta(seconds=5))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "test.entity_2 - 0:00:10"