From 0a5fb475baa899d42e2b987e18e342fd4a2a7c49 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 30 Nov 2026 16:57:16 +0100 Subject: [PATCH] Set up the config entry in Elexa Guardian test fixtures (#195556) Co-authored-by: Claude --- tests/components/guardian/conftest.py | 24 +++++++++++-------- tests/components/guardian/test_config_flow.py | 8 +++---- tests/components/guardian/test_diagnostics.py | 2 +- tests/components/guardian/test_init.py | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/components/guardian/conftest.py b/tests/components/guardian/conftest.py index a176de0d9f1e..a44c790020ec 100644 --- a/tests/components/guardian/conftest.py +++ b/tests/components/guardian/conftest.py @@ -1,6 +1,6 @@ """Define fixtures for Elexa Guardian tests.""" -from collections.abc import AsyncGenerator, Generator +from collections.abc import Generator from typing import Any from unittest.mock import AsyncMock, patch @@ -10,7 +10,6 @@ from pytest_homeassistant.common import MockConfigEntry, load_json_object_fixtur from homeassistant.components.guardian import CONF_UID, DOMAIN from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT, Platform from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component from homeassistant.util.json import JsonObjectType @@ -102,10 +101,8 @@ def platforms_fixture() -> list[Platform]: return [] -@pytest.fixture(name="setup_guardian") -async def setup_guardian_fixture( - hass: HomeAssistant, - config: dict[str, Any], +@pytest.fixture(name="patch_guardian") +def patch_guardian_fixture( platforms: list[Platform], data_sensor_pair_dump: JsonObjectType, data_sensor_pair_sensor: JsonObjectType, @@ -115,8 +112,8 @@ async def setup_guardian_fixture( data_system_ping: JsonObjectType, data_valve_status: JsonObjectType, data_wifi_status: JsonObjectType, -) -> AsyncGenerator[None]: - """Define a fixture to set up Guardian.""" +) -> Generator[None]: + """Patch the Guardian library.""" with ( patch("aioguardian.client.Client.connect"), patch( @@ -159,11 +156,18 @@ async def setup_guardian_fixture( platforms, ), ): - assert await async_setup_component(hass, DOMAIN, config) - await hass.async_block_till_done() yield +@pytest.fixture(name="setup_guardian") +async def setup_guardian_fixture( + hass: HomeAssistant, config_entry: MockConfigEntry, patch_guardian: None +) -> None: + """Define a fixture to set up Guardian.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + @pytest.fixture(name="unique_id") def unique_id_fixture() -> str: """Define a config entry unique ID fixture.""" diff --git a/tests/components/guardian/test_config_flow.py b/tests/components/guardian/test_config_flow.py index 77bfc5c7a5d5..1e2da4d3209d 100644 --- a/tests/components/guardian/test_config_flow.py +++ b/tests/components/guardian/test_config_flow.py @@ -43,7 +43,7 @@ async def test_duplicate_error(hass: HomeAssistant, config: dict[str, Any]) -> N assert result["reason"] == "already_configured" -@pytest.mark.usefixtures("setup_guardian") +@pytest.mark.usefixtures("patch_guardian") async def test_connect_error(hass: HomeAssistant, config: dict[str, Any]) -> None: """Test that the config entry errors out if the device cannot connect.""" @@ -83,7 +83,7 @@ async def test_get_pin_from_uid() -> None: assert pin == "3456" -@pytest.mark.usefixtures("setup_guardian") +@pytest.mark.usefixtures("patch_guardian") async def test_step_user(hass: HomeAssistant, config: dict[str, Any]) -> None: """Test the user step.""" result = await hass.config_entries.flow.async_init( @@ -107,7 +107,7 @@ async def test_step_user(hass: HomeAssistant, config: dict[str, Any]) -> None: assert result["result"].unique_id == "guardian_3456" -@pytest.mark.usefixtures("setup_guardian") +@pytest.mark.usefixtures("patch_guardian") async def test_step_zeroconf(hass: HomeAssistant) -> None: """Test the zeroconf step.""" zeroconf_data = ZeroconfServiceInfo( @@ -164,7 +164,7 @@ async def test_step_zeroconf_already_in_progress(hass: HomeAssistant) -> None: assert result["reason"] == "already_in_progress" -@pytest.mark.usefixtures("setup_guardian") +@pytest.mark.usefixtures("patch_guardian") async def test_step_dhcp(hass: HomeAssistant) -> None: """Test the dhcp step.""" dhcp_data = DhcpServiceInfo( diff --git a/tests/components/guardian/test_diagnostics.py b/tests/components/guardian/test_diagnostics.py index 380b18514742..827cb2bad5b9 100644 --- a/tests/components/guardian/test_diagnostics.py +++ b/tests/components/guardian/test_diagnostics.py @@ -13,7 +13,7 @@ async def test_entry_diagnostics( hass: HomeAssistant, config_entry: MockConfigEntry, hass_client: ClientSessionGenerator, - setup_guardian: None, # relies on config_entry fixture + setup_guardian: None, ) -> None: """Test config entry diagnostics.""" data: GuardianData = config_entry.runtime_data diff --git a/tests/components/guardian/test_init.py b/tests/components/guardian/test_init.py index 6d3788a2d3e3..f4309041c77f 100644 --- a/tests/components/guardian/test_init.py +++ b/tests/components/guardian/test_init.py @@ -23,7 +23,7 @@ async def test_paired_sensor_via_device_id( freezer: FrozenDateTimeFactory, device_registry: dr.DeviceRegistry, config_entry: MockConfigEntry, - setup_guardian: None, # relies on config_entry fixture + setup_guardian: None, ) -> None: """Test that a paired sensor is linked to the valve controller via via_device_id.""" # Advance time so the sensor pair dump coordinator refreshes and discovers the