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 @@ 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,10 +3282,9 @@ 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
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
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)
|
||||
assert len(debug_info_data["entities"][0]["subscriptions"]) >= 1
|
||||
|
@ -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,14 +3330,13 @@ 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
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
dt_utcnow.return_value = dt2
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
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")
|
||||
freezer.move_to(dt2)
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123")
|
||||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
|
||||
|
@ -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,10 +3391,9 @@ 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
|
||||
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
|
||||
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)
|
||||
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
|
||||
|
@ -3408,16 +3410,16 @@ 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
|
||||
async_fire_mqtt_message(hass, "sensor/status", "123", qos=0, retain=False)
|
||||
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)
|
||||
|
||||
|
||||
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,19 +3443,18 @@ 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
|
||||
# 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
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=False)
|
||||
# simpulate someone else subscribed and retained messages were replayed
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=1, retain=True)
|
||||
# simulate an update message
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=1, retain=False)
|
||||
# simulate an update message
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=2, retain=False)
|
||||
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
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=0, retain=False)
|
||||
# simpulate someone else subscribed and retained messages were replayed
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=1, retain=True)
|
||||
# simulate an update message
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=1, retain=False)
|
||||
# simulate an update message
|
||||
async_fire_mqtt_message(hass, "sensor/abc", "123", qos=2, retain=False)
|
||||
|
||||
debug_info_data = debug_info.info_for_device(hass, device.id)
|
||||
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue