mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Update state helper test to avoid patching utcnow (#93484)
This commit is contained in:
parent
ee62584570
commit
f5eb872eaf
@ -3,6 +3,7 @@ import asyncio
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.sun import STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON
|
from homeassistant.components.sun import STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON
|
||||||
@ -33,18 +34,15 @@ async def test_async_track_states(
|
|||||||
point2 = point1 + timedelta(seconds=5)
|
point2 = point1 + timedelta(seconds=5)
|
||||||
point3 = point2 + timedelta(seconds=5)
|
point3 = point2 + timedelta(seconds=5)
|
||||||
|
|
||||||
with patch("homeassistant.core.dt_util.utcnow") as mock_utcnow:
|
with freeze_time(point2) as freezer, state.AsyncTrackStates(hass) as states:
|
||||||
mock_utcnow.return_value = point2
|
freezer.move_to(point1)
|
||||||
|
|
||||||
with state.AsyncTrackStates(hass) as states:
|
|
||||||
mock_utcnow.return_value = point1
|
|
||||||
hass.states.async_set("light.test", "on")
|
hass.states.async_set("light.test", "on")
|
||||||
|
|
||||||
mock_utcnow.return_value = point2
|
freezer.move_to(point2)
|
||||||
hass.states.async_set("light.test2", "on")
|
hass.states.async_set("light.test2", "on")
|
||||||
state2 = hass.states.get("light.test2")
|
state2 = hass.states.get("light.test2")
|
||||||
|
|
||||||
mock_utcnow.return_value = point3
|
freezer.move_to(point3)
|
||||||
hass.states.async_set("light.test3", "on")
|
hass.states.async_set("light.test3", "on")
|
||||||
state3 = hass.states.get("light.test3")
|
state3 = hass.states.get("light.test3")
|
||||||
|
|
||||||
@ -92,15 +90,15 @@ async def test_get_changed_since(
|
|||||||
point2 = point1 + timedelta(seconds=5)
|
point2 = point1 + timedelta(seconds=5)
|
||||||
point3 = point2 + timedelta(seconds=5)
|
point3 = point2 + timedelta(seconds=5)
|
||||||
|
|
||||||
with patch("homeassistant.core.dt_util.utcnow", return_value=point1):
|
with freeze_time(point1) as freezer:
|
||||||
hass.states.async_set("light.test", "on")
|
hass.states.async_set("light.test", "on")
|
||||||
state1 = hass.states.get("light.test")
|
state1 = hass.states.get("light.test")
|
||||||
|
|
||||||
with patch("homeassistant.core.dt_util.utcnow", return_value=point2):
|
freezer.move_to(point2)
|
||||||
hass.states.async_set("light.test2", "on")
|
hass.states.async_set("light.test2", "on")
|
||||||
state2 = hass.states.get("light.test2")
|
state2 = hass.states.get("light.test2")
|
||||||
|
|
||||||
with patch("homeassistant.core.dt_util.utcnow", return_value=point3):
|
freezer.move_to(point3)
|
||||||
hass.states.async_set("light.test3", "on")
|
hass.states.async_set("light.test3", "on")
|
||||||
state3 = hass.states.get("light.test3")
|
state3 = hass.states.get("light.test3")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user