mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Freeze time for MQTT sensor expire tests (#99496)
This commit is contained in:
parent
1e46ecbb48
commit
3d1efaa4ad
@ -6,6 +6,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -146,7 +147,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
"""Run the basic expiry code."""
|
"""Run the basic expiry code."""
|
||||||
realnow = dt_util.utcnow()
|
realnow = dt_util.utcnow()
|
||||||
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
with freeze_time(now) as freezer:
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
async_fire_mqtt_message(hass, "test-topic", "ON")
|
async_fire_mqtt_message(hass, "test-topic", "ON")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -156,7 +158,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
# Time jump +3s
|
# Time jump +3s
|
||||||
now = now + timedelta(seconds=3)
|
now += timedelta(seconds=3)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -165,7 +168,9 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
# Next message resets timer
|
# Next message resets timer
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
# Time jump 0.5s
|
||||||
|
now += timedelta(seconds=0.5)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
async_fire_mqtt_message(hass, "test-topic", "OFF")
|
async_fire_mqtt_message(hass, "test-topic", "OFF")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -175,7 +180,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == STATE_OFF
|
assert state.state == STATE_OFF
|
||||||
|
|
||||||
# Time jump +3s
|
# Time jump +3s
|
||||||
now = now + timedelta(seconds=3)
|
now += timedelta(seconds=3)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -184,7 +190,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == STATE_OFF
|
assert state.state == STATE_OFF
|
||||||
|
|
||||||
# Time jump +2s
|
# Time jump +2s
|
||||||
now = now + timedelta(seconds=2)
|
now += timedelta(seconds=2)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -197,6 +204,7 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that binary_sensor with expire_after set behaves correctly on discovery and discovery update."""
|
"""Test that binary_sensor with expire_after set behaves correctly on discovery and discovery update."""
|
||||||
await mqtt_mock_entry()
|
await mqtt_mock_entry()
|
||||||
@ -212,11 +220,9 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||||||
# Set time and publish config message to create binary_sensor via discovery with 4 s expiry
|
# Set time and publish config message to create binary_sensor via discovery with 4 s expiry
|
||||||
realnow = dt_util.utcnow()
|
realnow = dt_util.utcnow()
|
||||||
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
async_fire_mqtt_message(
|
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla/config", config_msg)
|
||||||
hass, "homeassistant/binary_sensor/bla/config", config_msg
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Test that binary_sensor is not available
|
# Test that binary_sensor is not available
|
||||||
@ -224,7 +230,6 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
# Publish state message
|
# Publish state message
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
|
||||||
async_fire_mqtt_message(hass, "test-topic", "ON")
|
async_fire_mqtt_message(hass, "test-topic", "ON")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -233,8 +238,8 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
# Advance +3 seconds
|
# Advance +3 seconds
|
||||||
now = now + timedelta(seconds=3)
|
now += timedelta(seconds=3)
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -255,8 +260,8 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
|
|
||||||
# Add +2 seconds
|
# Add +2 seconds
|
||||||
now = now + timedelta(seconds=2)
|
now += timedelta(seconds=2)
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -265,10 +270,7 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
# Resend config message to update discovery
|
# Resend config message to update discovery
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla/config", config_msg)
|
||||||
async_fire_mqtt_message(
|
|
||||||
hass, "homeassistant/binary_sensor/bla/config", config_msg
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
# Test that binary_sensor is still expired
|
# Test that binary_sensor is still expired
|
||||||
|
@ -6,6 +6,7 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -360,7 +361,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
"""Run the basic expiry code."""
|
"""Run the basic expiry code."""
|
||||||
realnow = dt_util.utcnow()
|
realnow = dt_util.utcnow()
|
||||||
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
with freeze_time(now) as freezer:
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
async_fire_mqtt_message(hass, "test-topic", "100")
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -370,7 +372,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == "100"
|
assert state.state == "100"
|
||||||
|
|
||||||
# Time jump +3s
|
# Time jump +3s
|
||||||
now = now + timedelta(seconds=3)
|
now += timedelta(seconds=3)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -379,7 +382,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == "100"
|
assert state.state == "100"
|
||||||
|
|
||||||
# Next message resets timer
|
# Next message resets timer
|
||||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
now += timedelta(seconds=0.5)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
async_fire_mqtt_message(hass, "test-topic", "101")
|
async_fire_mqtt_message(hass, "test-topic", "101")
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -389,7 +393,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == "101"
|
assert state.state == "101"
|
||||||
|
|
||||||
# Time jump +3s
|
# Time jump +3s
|
||||||
now = now + timedelta(seconds=3)
|
now += timedelta(seconds=3)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
@ -398,7 +403,8 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||||||
assert state.state == "101"
|
assert state.state == "101"
|
||||||
|
|
||||||
# Time jump +2s
|
# Time jump +2s
|
||||||
now = now + timedelta(seconds=2)
|
now += timedelta(seconds=2)
|
||||||
|
freezer.move_to(now)
|
||||||
async_fire_time_changed(hass, now)
|
async_fire_time_changed(hass, now)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user