mirror of
https://github.com/home-assistant/core.git
synced 2025-11-12 20:40:18 +00:00
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
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user