From 0ffebd4853c37cfca08f9d881b6c41cd76165273 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Mon, 1 Jul 2024 20:48:33 +0200 Subject: [PATCH] Use fixtures in deCONZ button tests (#120958) --- tests/components/deconz/conftest.py | 36 +++++++++++++----------- tests/components/deconz/test_button.py | 38 +++++++++----------------- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/tests/components/deconz/conftest.py b/tests/components/deconz/conftest.py index ec254ea1c1e..049dbed2963 100644 --- a/tests/components/deconz/conftest.py +++ b/tests/components/deconz/conftest.py @@ -85,6 +85,11 @@ def fixture_put_request( def fixture_get_request( aioclient_mock: AiohttpClientMocker, config_entry_data: MappingProxyType[str, Any], + config_payload: dict[str, Any], + alarm_system_payload: dict[str, Any], + group_payload: dict[str, Any], + light_payload: dict[str, Any], + sensor_payload: dict[str, Any], deconz_payload: dict[str, Any], ) -> Callable[[str], None]: """Mock default deCONZ requests responses.""" @@ -92,10 +97,21 @@ def fixture_get_request( _port = config_entry_data[CONF_PORT] _api_key = config_entry_data[CONF_API_KEY] + data = deconz_payload + data.setdefault("alarmsystems", alarm_system_payload) + data.setdefault("config", config_payload) + data.setdefault("groups", group_payload) + data.setdefault("lights", light_payload) + data.setdefault("sensors", sensor_payload) + def __mock_requests(host: str = "") -> None: url = f"http://{host or _host}:{_port}/api/{_api_key}" aioclient_mock.get( - url, json=deconz_payload, headers={"content-type": CONTENT_TYPE_JSON} + url, + json=deconz_payload | {"config": config_payload}, + headers={ + "content-type": CONTENT_TYPE_JSON, + }, ) return __mock_requests @@ -105,21 +121,9 @@ def fixture_get_request( @pytest.fixture(name="deconz_payload") -def fixture_data( - alarm_system_payload: dict[str, Any], - config_payload: dict[str, Any], - group_payload: dict[str, Any], - light_payload: dict[str, Any], - sensor_payload: dict[str, Any], -) -> dict[str, Any]: - """DeCONZ data.""" - return { - "alarmsystems": alarm_system_payload, - "config": config_payload, - "groups": group_payload, - "lights": light_payload, - "sensors": sensor_payload, - } +def fixture_data() -> dict[str, Any]: + """Combine multiple payloads with one fixture.""" + return {} @pytest.fixture(name="alarm_system_payload") diff --git a/tests/components/deconz/test_button.py b/tests/components/deconz/test_button.py index 4d85270ddca..1ddcbd8f105 100644 --- a/tests/components/deconz/test_button.py +++ b/tests/components/deconz/test_button.py @@ -1,31 +1,17 @@ """deCONZ button platform tests.""" -from unittest.mock import patch +from collections.abc import Callable import pytest from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er -from .test_gateway import ( - DECONZ_WEB_REQUEST, - mock_deconz_put_request, - setup_deconz_integration, -) - from tests.test_util.aiohttp import AiohttpClientMocker - -async def test_no_binary_sensors( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker -) -> None: - """Test that no sensors in deconz results in no sensor entities.""" - await setup_deconz_integration(hass, aioclient_mock) - assert len(hass.states.async_all()) == 0 - - TEST_DATA = [ ( # Store scene button { @@ -100,19 +86,17 @@ TEST_DATA = [ ] -@pytest.mark.parametrize(("raw_data", "expected"), TEST_DATA) +@pytest.mark.parametrize(("deconz_payload", "expected"), TEST_DATA) async def test_button( hass: HomeAssistant, entity_registry: er.EntityRegistry, device_registry: dr.DeviceRegistry, aioclient_mock: AiohttpClientMocker, - raw_data, + config_entry_setup: ConfigEntry, + mock_put_request: Callable[[str, str], AiohttpClientMocker], expected, ) -> None: """Test successful creation of button entities.""" - with patch.dict(DECONZ_WEB_REQUEST, raw_data): - config_entry = await setup_deconz_integration(hass, aioclient_mock) - assert len(hass.states.async_all()) == expected["entity_count"] # Verify state data @@ -129,13 +113,17 @@ async def test_button( # Verify device registry data assert ( - len(dr.async_entries_for_config_entry(device_registry, config_entry.entry_id)) + len( + dr.async_entries_for_config_entry( + device_registry, config_entry_setup.entry_id + ) + ) == expected["device_count"] ) # Verify button press - mock_deconz_put_request(aioclient_mock, config_entry.data, expected["request"]) + aioclient_mock = mock_put_request(expected["request"]) await hass.services.async_call( BUTTON_DOMAIN, @@ -147,11 +135,11 @@ async def test_button( # Unload entry - await hass.config_entries.async_unload(config_entry.entry_id) + await hass.config_entries.async_unload(config_entry_setup.entry_id) assert hass.states.get(expected["entity_id"]).state == STATE_UNAVAILABLE # Remove entry - await hass.config_entries.async_remove(config_entry.entry_id) + await hass.config_entries.async_remove(config_entry_setup.entry_id) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0