From f502739df279c2fbb44fde4d126559429b2f2ff5 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 11 Nov 2025 12:04:59 +0100 Subject: [PATCH] Use pytest.mark.freeze_time in zha tests (#156358) --- tests/components/zha/test_button.py | 23 +++++++++++++--------- tests/components/zha/test_websocket_api.py | 18 +++++++++++++++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/tests/components/zha/test_button.py b/tests/components/zha/test_button.py index 8c41a914f5a..ad88e84fd3a 100644 --- a/tests/components/zha/test_button.py +++ b/tests/components/zha/test_button.py @@ -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) diff --git a/tests/components/zha/test_websocket_api.py b/tests/components/zha/test_websocket_api.py index df945b8afc2..34152a3f36b 100644 --- a/tests/components/zha/test_websocket_api.py +++ b/tests/components/zha/test_websocket_api.py @@ -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"})