Use pytest.mark.freeze_time in zha tests (#156358)

This commit is contained in:
Erik Montnemery
2025-11-11 12:04:59 +01:00
committed by GitHub
parent 0f2ff29378
commit f502739df2
2 changed files with 30 additions and 11 deletions
+14 -9
View File
@@ -3,7 +3,6 @@
from collections.abc import Callable, Coroutine
from unittest.mock import patch
from freezegun import freeze_time
import pytest
from zigpy.const import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE
from zigpy.device import Device
@@ -46,23 +45,29 @@ def button_platform_only():
@pytest.fixture
async def setup_zha_integration(
hass: HomeAssistant, setup_zha: Callable[..., Coroutine[None]]
):
"""Set up ZHA component."""
def speed_up_radio_mgr():
"""Speed up the radio manager connection time by removing delays.
# if we call this in the test itself the test hangs forever
await setup_zha()
This fixture replaces the fixture in conftest.py by patching the connect
and shutdown delays to 0 to allow waiting for the patched delays when
running tests with time frozen, which otherwise blocks forever.
"""
with (
patch("homeassistant.components.zha.radio_manager.CONNECT_DELAY_S", 0),
patch("zha.application.gateway.SHUT_DOWN_DELAY_S", 0),
):
yield
@freeze_time("2021-11-04 17:37:00", tz_offset=-1)
@pytest.mark.freeze_time("2021-11-04 17:37:00", tz_offset=-1)
async def test_button(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
setup_zha_integration, # pylint: disable=unused-argument
setup_zha: Callable[..., Coroutine[None]],
zigpy_device_mock: Callable[..., Device],
) -> None:
"""Test ZHA button platform."""
await setup_zha()
gateway = get_zha_gateway(hass)
gateway_proxy: ZHAGatewayProxy = get_zha_gateway_proxy(hass)
+16 -2
View File
@@ -8,7 +8,6 @@ from copy import deepcopy
from typing import TYPE_CHECKING
from unittest.mock import ANY, AsyncMock, MagicMock, call, patch
from freezegun import freeze_time
import pytest
import voluptuous as vol
from zha.application.const import (
@@ -94,6 +93,21 @@ def required_platform_only():
yield
@pytest.fixture
def speed_up_radio_mgr():
"""Speed up the radio manager connection time by removing delays.
This fixture replaces the fixture in conftest.py by patching the connect
and shutdown delays to 0 to allow waiting for the patched delays when
running tests with time frozen, which otherwise blocks forever.
"""
with (
patch("homeassistant.components.zha.radio_manager.CONNECT_DELAY_S", 0),
patch("zha.application.gateway.SHUT_DOWN_DELAY_S", 0),
):
yield
@pytest.fixture
async def zha_client(
hass: HomeAssistant,
@@ -217,7 +231,7 @@ async def test_device_cluster_commands(zha_client) -> None:
assert command[TYPE] is not None
@freeze_time("2023-09-23 20:16:00+00:00")
@pytest.mark.freeze_time("2023-09-23 20:16:00+00:00")
async def test_list_devices(zha_client) -> None:
"""Test getting ZHA devices."""
await zha_client.send_json({ID: 5, TYPE: "zha/devices"})