Skip to content

Commit

Permalink
Use fixtures in deCONZ button tests (#120958)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 authored Jul 1, 2024
1 parent 383de96 commit 0ffebd4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
36 changes: 20 additions & 16 deletions tests/components/deconz/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,33 @@ def __mock_requests(path: str, host: str = "") -> AiohttpClientMocker:
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."""
_host = config_entry_data[CONF_HOST]
_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
Expand All @@ -105,21 +121,9 @@ def __mock_requests(host: str = "") -> None:


@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")
Expand Down
38 changes: 13 additions & 25 deletions tests/components/deconz/test_button.py
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -100,19 +86,17 @@ async def test_no_binary_sensors(
]


@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
Expand All @@ -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,
Expand All @@ -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

0 comments on commit 0ffebd4

Please sign in to comment.