Freeze time for MQTT sensor expire tests (#99496)
This commit is contained in:
parent
1e46ecbb48
commit
3d1efaa4ad
2 changed files with 90 additions and 82 deletions
|
@ -6,6 +6,7 @@ from pathlib import Path
|
|||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun import freeze_time
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
|
@ -146,57 +147,64 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||
"""Run the basic expiry code."""
|
||||
realnow = dt_util.utcnow()
|
||||
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_mqtt_message(hass, "test-topic", "ON")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value was set correctly.
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_ON
|
||||
# Value was set correctly.
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
# Time jump +3s
|
||||
now = now + timedelta(seconds=3)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
# Time jump +3s
|
||||
now += timedelta(seconds=3)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_ON
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
# Next message resets timer
|
||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
||||
# Next message resets timer
|
||||
# Time jump 0.5s
|
||||
now += timedelta(seconds=0.5)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
async_fire_mqtt_message(hass, "test-topic", "OFF")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value was updated correctly.
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_OFF
|
||||
# Value was updated correctly.
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
# Time jump +3s
|
||||
now = now + timedelta(seconds=3)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
# Time jump +3s
|
||||
now += timedelta(seconds=3)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_OFF
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
# Time jump +2s
|
||||
now = now + timedelta(seconds=2)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
# Time jump +2s
|
||||
now += timedelta(seconds=2)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value is expired now
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
# Value is expired now
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
||||
hass: HomeAssistant,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test that binary_sensor with expire_after set behaves correctly on discovery and discovery update."""
|
||||
await mqtt_mock_entry()
|
||||
|
@ -212,31 +220,28 @@ 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
|
||||
realnow = dt_util.utcnow()
|
||||
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
||||
async_fire_time_changed(hass, now)
|
||||
async_fire_mqtt_message(
|
||||
hass, "homeassistant/binary_sensor/bla/config", config_msg
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla/config", config_msg)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test that binary_sensor is not available
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
# Publish state message
|
||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
||||
async_fire_mqtt_message(hass, "test-topic", "ON")
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "test-topic", "ON")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test that binary_sensor has correct state
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
# Advance +3 seconds
|
||||
now = now + timedelta(seconds=3)
|
||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
now += timedelta(seconds=3)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# binary_sensor is not yet expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
|
@ -255,21 +260,18 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
|||
assert state.state == STATE_ON
|
||||
|
||||
# Add +2 seconds
|
||||
now = now + timedelta(seconds=2)
|
||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
now += timedelta(seconds=2)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test that binary_sensor has expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
# 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
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla/config", config_msg)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Test that binary_sensor is still expired
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
|
|
|
@ -6,6 +6,7 @@ from pathlib import Path
|
|||
from typing import Any
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from freezegun import freeze_time
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
|
@ -360,51 +361,56 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
|||
"""Run the basic expiry code."""
|
||||
realnow = dt_util.utcnow()
|
||||
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_mqtt_message(hass, "test-topic", "100")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value was set correctly.
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "100"
|
||||
# Value was set correctly.
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "100"
|
||||
|
||||
# Time jump +3s
|
||||
now = now + timedelta(seconds=3)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
# Time jump +3s
|
||||
now += timedelta(seconds=3)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "100"
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "100"
|
||||
|
||||
# Next message resets timer
|
||||
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
||||
# Next message resets timer
|
||||
now += timedelta(seconds=0.5)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
async_fire_mqtt_message(hass, "test-topic", "101")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value was updated correctly.
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "101"
|
||||
# Value was updated correctly.
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "101"
|
||||
|
||||
# Time jump +3s
|
||||
now = now + timedelta(seconds=3)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
# Time jump +3s
|
||||
now += timedelta(seconds=3)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "101"
|
||||
# Value is not yet expired
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == "101"
|
||||
|
||||
# Time jump +2s
|
||||
now = now + timedelta(seconds=2)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
# Time jump +2s
|
||||
now += timedelta(seconds=2)
|
||||
freezer.move_to(now)
|
||||
async_fire_time_changed(hass, now)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Value is expired now
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
# Value is expired now
|
||||
state = hass.states.get("sensor.test")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue