mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Use fixtures in deCONZ button tests (#120958)
This commit is contained in:
parent
383de96549
commit
0ffebd4853
@ -85,6 +85,11 @@ def fixture_put_request(
|
|||||||
def fixture_get_request(
|
def fixture_get_request(
|
||||||
aioclient_mock: AiohttpClientMocker,
|
aioclient_mock: AiohttpClientMocker,
|
||||||
config_entry_data: MappingProxyType[str, Any],
|
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],
|
deconz_payload: dict[str, Any],
|
||||||
) -> Callable[[str], None]:
|
) -> Callable[[str], None]:
|
||||||
"""Mock default deCONZ requests responses."""
|
"""Mock default deCONZ requests responses."""
|
||||||
@ -92,10 +97,21 @@ def fixture_get_request(
|
|||||||
_port = config_entry_data[CONF_PORT]
|
_port = config_entry_data[CONF_PORT]
|
||||||
_api_key = config_entry_data[CONF_API_KEY]
|
_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:
|
def __mock_requests(host: str = "") -> None:
|
||||||
url = f"http://{host or _host}:{_port}/api/{_api_key}"
|
url = f"http://{host or _host}:{_port}/api/{_api_key}"
|
||||||
aioclient_mock.get(
|
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
|
return __mock_requests
|
||||||
@ -105,21 +121,9 @@ def fixture_get_request(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="deconz_payload")
|
@pytest.fixture(name="deconz_payload")
|
||||||
def fixture_data(
|
def fixture_data() -> dict[str, Any]:
|
||||||
alarm_system_payload: dict[str, Any],
|
"""Combine multiple payloads with one fixture."""
|
||||||
config_payload: dict[str, Any],
|
return {}
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="alarm_system_payload")
|
@pytest.fixture(name="alarm_system_payload")
|
||||||
|
@ -1,31 +1,17 @@
|
|||||||
"""deCONZ button platform tests."""
|
"""deCONZ button platform tests."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from collections.abc import Callable
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
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.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
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
|
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 = [
|
TEST_DATA = [
|
||||||
( # Store scene button
|
( # 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(
|
async def test_button(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entity_registry: er.EntityRegistry,
|
entity_registry: er.EntityRegistry,
|
||||||
device_registry: dr.DeviceRegistry,
|
device_registry: dr.DeviceRegistry,
|
||||||
aioclient_mock: AiohttpClientMocker,
|
aioclient_mock: AiohttpClientMocker,
|
||||||
raw_data,
|
config_entry_setup: ConfigEntry,
|
||||||
|
mock_put_request: Callable[[str, str], AiohttpClientMocker],
|
||||||
expected,
|
expected,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test successful creation of button entities."""
|
"""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"]
|
assert len(hass.states.async_all()) == expected["entity_count"]
|
||||||
|
|
||||||
# Verify state data
|
# Verify state data
|
||||||
@ -129,13 +113,17 @@ async def test_button(
|
|||||||
# Verify device registry data
|
# Verify device registry data
|
||||||
|
|
||||||
assert (
|
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"]
|
== expected["device_count"]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify button press
|
# 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(
|
await hass.services.async_call(
|
||||||
BUTTON_DOMAIN,
|
BUTTON_DOMAIN,
|
||||||
@ -147,11 +135,11 @@ async def test_button(
|
|||||||
|
|
||||||
# Unload entry
|
# 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
|
assert hass.states.get(expected["entity_id"]).state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
# Remove entry
|
# 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()
|
await hass.async_block_till_done()
|
||||||
assert len(hass.states.async_all()) == 0
|
assert len(hass.states.async_all()) == 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user