Migrate mqtt tests to use freezegun (#105414)
This commit is contained in:
parent
1cc47c0553
commit
ff85d0c290
2 changed files with 41 additions and 41 deletions
|
@ -8,6 +8,7 @@ from pathlib import Path
|
|||
from typing import Any
|
||||
from unittest.mock import ANY, MagicMock, patch
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
import yaml
|
||||
|
@ -31,6 +32,7 @@ from homeassistant.generated.mqtt import MQTT
|
|||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_mqtt_message
|
||||
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
|
||||
|
@ -1320,9 +1322,8 @@ async def help_test_entity_debug_info_max_messages(
|
|||
"subscriptions"
|
||||
]
|
||||
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0)
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
with freeze_time(start_dt):
|
||||
for i in range(0, debug_info.STORED_MESSAGES + 1):
|
||||
async_fire_mqtt_message(hass, "test-topic", f"{i}")
|
||||
|
||||
|
@ -1396,7 +1397,7 @@ async def help_test_entity_debug_info_message(
|
|||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0)
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
|
||||
if state_topic is not None:
|
||||
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
|
||||
|
@ -1404,8 +1405,7 @@ async def help_test_entity_debug_info_message(
|
|||
"subscriptions"
|
||||
]
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
with freeze_time(start_dt):
|
||||
async_fire_mqtt_message(hass, str(state_topic), state_payload)
|
||||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
|
@ -1426,8 +1426,7 @@ async def help_test_entity_debug_info_message(
|
|||
expected_transmissions = []
|
||||
if service:
|
||||
# Trigger an outgoing MQTT message
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
with freeze_time(start_dt):
|
||||
if service:
|
||||
service_data = {ATTR_ENTITY_ID: f"{domain}.beer_test"}
|
||||
if service_parameters:
|
||||
|
|
|
@ -8,6 +8,7 @@ import ssl
|
|||
from typing import Any, TypedDict
|
||||
from unittest.mock import ANY, MagicMock, call, mock_open, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -40,6 +41,7 @@ from homeassistant.helpers.entity import Entity
|
|||
from homeassistant.helpers.entity_platform import async_get_platforms
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .test_common import help_all_subscribe_calls
|
||||
|
@ -3256,6 +3258,7 @@ async def test_debug_info_wildcard(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test debug info."""
|
||||
await mqtt_mock_entry()
|
||||
|
@ -3279,9 +3282,8 @@ async def test_debug_info_wildcard(
|
|||
"subscriptions"
|
||||
]
|
||||
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0)
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
freezer.move_to(start_dt)
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
|
@ -3304,6 +3306,7 @@ async def test_debug_info_filter_same(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test debug info removes messages with same timestamp."""
|
||||
await mqtt_mock_entry()
|
||||
|
@ -3327,13 +3330,12 @@ async def test_debug_info_filter_same(
|
|||
"subscriptions"
|
||||
]
|
||||
|
||||
dt1 = datetime(2019, 1, 1, 0, 0, 0)
|
||||
dt2 = datetime(2019, 1, 1, 0, 0, 1)
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = dt1
|
||||
dt1 = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
dt2 = datetime(2019, 1, 1, 0, 0, 1, tzinfo=dt_util.UTC)
|
||||
freezer.move_to(dt1)
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
dt_utcnow.return_value = dt2
|
||||
freezer.move_to(dt2)
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
|
@ -3364,6 +3366,7 @@ async def test_debug_info_same_topic(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test debug info."""
|
||||
await mqtt_mock_entry()
|
||||
|
@ -3388,9 +3391,8 @@ async def test_debug_info_same_topic(
|
|||
"subscriptions"
|
||||
]
|
||||
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0)
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
freezer.move_to(start_dt)
|
||||
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
|
||||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
|
@ -3408,9 +3410,8 @@ async def test_debug_info_same_topic(
|
|||
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0)
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
freezer.move_to(start_dt)
|
||||
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
|
||||
|
||||
|
||||
|
@ -3418,6 +3419,7 @@ async def test_debug_info_qos_retain(
|
|||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
mqtt_mock_entry: MqttMockHAClientGenerator,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test debug info."""
|
||||
await mqtt_mock_entry()
|
||||
|
@ -3441,9 +3443,8 @@ async def test_debug_info_qos_retain(
|
|||
"subscriptions"
|
||||
]
|
||||
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0)
|
||||
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
|
||||
dt_utcnow.return_value = start_dt
|
||||
start_dt = datetime(2019, 1, 1, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
freezer.move_to(start_dt)
|
||||
# simulate the first message was replayed from the broker with retained flag
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=True)
|
||||
# simulate an update message
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue