Use fixtures in deCONZ binary sensor tests (#120966)

pull/121204/head
Robert Svensson 2024-07-04 14:21:26 +02:00 committed by GitHub
parent 31ed32da6c
commit 873d96bab3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 80 additions and 93 deletions

View File

@ -168,8 +168,16 @@ def fixture_light_data() -> dict[str, Any]:
@pytest.fixture(name="sensor_payload")
def fixture_sensor_data() -> dict[str, Any]:
def fixture_sensor_data(sensor_1_payload: dict[str, Any]) -> dict[str, Any]:
"""Sensor data."""
if sensor_1_payload:
return {"1": sensor_1_payload}
return {}
@pytest.fixture(name="sensor_1_payload")
def fixture_sensor_1_data() -> dict[str, Any]:
"""Sensor 1 data."""
return {}

View File

@ -1,6 +1,7 @@
"""deCONZ binary sensor platform tests."""
from unittest.mock import patch
from collections.abc import Callable
from typing import Any
import pytest
@ -12,6 +13,7 @@ from homeassistant.components.deconz.const import (
DOMAIN as DECONZ_DOMAIN,
)
from homeassistant.components.deconz.services import SERVICE_DEVICE_REFRESH
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_DEVICE_CLASS,
STATE_OFF,
@ -22,23 +24,6 @@ from homeassistant.const import (
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_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 = [
( # Alarm binary sensor
{
@ -466,22 +451,17 @@ TEST_DATA = [
]
@pytest.mark.parametrize(("sensor_data", "expected"), TEST_DATA)
@pytest.mark.parametrize("config_entry_options", [{CONF_ALLOW_CLIP_SENSOR: True}])
@pytest.mark.parametrize(("sensor_1_payload", "expected"), TEST_DATA)
async def test_binary_sensors(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
mock_deconz_websocket,
sensor_data,
expected,
expected: dict[str, Any],
) -> None:
"""Test successful creation of binary sensor entities."""
with patch.dict(DECONZ_WEB_REQUEST, {"sensors": {"1": sensor_data}}):
config_entry = await setup_deconz_integration(
hass, aioclient_mock, options={CONF_ALLOW_CLIP_SENSOR: True}
)
assert len(hass.states.async_all()) == expected["entity_count"]
# Verify state data
@ -500,7 +480,11 @@ async def test_binary_sensors(
# 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"]
)
@ -519,46 +503,39 @@ async def test_binary_sensors(
# 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
async def test_not_allow_clip_sensor(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that CLIP sensors are not allowed."""
data = {
"sensors": {
"1": {
"name": "CLIP presence sensor",
"type": "CLIPPresence",
"state": {"presence": False},
"config": {},
"uniqueid": "00:00:00:00:00:00:00:02-00",
},
@pytest.mark.parametrize(
"sensor_1_payload",
[
{
"name": "CLIP presence sensor",
"type": "CLIPPresence",
"state": {"presence": False},
"config": {},
"uniqueid": "00:00:00:00:00:00:00:02-00",
}
}
with patch.dict(DECONZ_WEB_REQUEST, data):
await setup_deconz_integration(
hass, aioclient_mock, options={CONF_ALLOW_CLIP_SENSOR: False}
)
],
)
@pytest.mark.parametrize("config_entry_options", [{CONF_ALLOW_CLIP_SENSOR: False}])
@pytest.mark.usefixtures("config_entry_setup")
async def test_not_allow_clip_sensor(hass: HomeAssistant) -> None:
"""Test that CLIP sensors are not allowed."""
assert len(hass.states.async_all()) == 0
async def test_allow_clip_sensor(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that CLIP sensors can be allowed."""
data = {
"sensors": {
@pytest.mark.parametrize(
"sensor_payload",
[
{
"1": {
"name": "Presence sensor",
"type": "ZHAPresence",
@ -585,12 +562,11 @@ async def test_allow_clip_sensor(
"uniqueid": "/sensors/3",
},
}
}
with patch.dict(DECONZ_WEB_REQUEST, data):
config_entry = await setup_deconz_integration(
hass, aioclient_mock, options={CONF_ALLOW_CLIP_SENSOR: True}
)
],
)
@pytest.mark.parametrize("config_entry_options", [{CONF_ALLOW_CLIP_SENSOR: True}])
async def test_allow_clip_sensor(hass: HomeAssistant, config_entry_setup) -> None:
"""Test that CLIP sensors can be allowed."""
assert len(hass.states.async_all()) == 3
assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF
@ -600,7 +576,7 @@ async def test_allow_clip_sensor(
# Disallow clip sensors
hass.config_entries.async_update_entry(
config_entry, options={CONF_ALLOW_CLIP_SENSOR: False}
config_entry_setup, options={CONF_ALLOW_CLIP_SENSOR: False}
)
await hass.async_block_till_done()
@ -611,7 +587,7 @@ async def test_allow_clip_sensor(
# Allow clip sensors
hass.config_entries.async_update_entry(
config_entry, options={CONF_ALLOW_CLIP_SENSOR: True}
config_entry_setup, options={CONF_ALLOW_CLIP_SENSOR: True}
)
await hass.async_block_till_done()
@ -620,10 +596,13 @@ async def test_allow_clip_sensor(
assert hass.states.get("binary_sensor.clip_flag_boot_time").state == STATE_ON
@pytest.mark.usefixtures("config_entry_setup")
async def test_add_new_binary_sensor(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket
hass: HomeAssistant, mock_deconz_websocket
) -> None:
"""Test that adding a new binary sensor works."""
assert len(hass.states.async_all()) == 0
event_added_sensor = {
"t": "event",
"e": "added",
@ -638,10 +617,6 @@ async def test_add_new_binary_sensor(
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
}
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
await mock_deconz_websocket(data=event_added_sensor)
await hass.async_block_till_done()
@ -649,10 +624,15 @@ async def test_add_new_binary_sensor(
assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF
@pytest.mark.parametrize(
"config_entry_options", [{CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False}]
)
async def test_add_new_binary_sensor_ignored_load_entities_on_service_call(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
deconz_payload: dict[str, Any],
mock_requests: Callable[[str], None],
mock_deconz_websocket,
) -> None:
"""Test that adding a new binary sensor is not allowed."""
@ -671,12 +651,6 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_service_call(
"sensor": sensor,
}
config_entry = await setup_deconz_integration(
hass,
aioclient_mock,
options={CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False},
)
assert len(hass.states.async_all()) == 0
await mock_deconz_websocket(data=event_added_sensor)
@ -686,13 +660,16 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_service_call(
assert not hass.states.get("binary_sensor.presence_sensor")
assert (
len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
len(
er.async_entries_for_config_entry(
entity_registry, config_entry_setup.entry_id
)
)
== 0
)
aioclient_mock.clear_requests()
data = {"config": {}, "groups": {}, "lights": {}, "sensors": {"1": sensor}}
mock_deconz_request(aioclient_mock, config_entry.data, data)
deconz_payload["sensors"] = {"1": sensor}
mock_requests()
await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH)
await hass.async_block_till_done()
@ -701,10 +678,15 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_service_call(
assert hass.states.get("binary_sensor.presence_sensor")
@pytest.mark.parametrize(
"config_entry_options", [{CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False}]
)
async def test_add_new_binary_sensor_ignored_load_entities_on_options_change(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry,
deconz_payload: dict[str, Any],
mock_requests: Callable[[str], None],
mock_deconz_websocket,
) -> None:
"""Test that adding a new binary sensor is not allowed."""
@ -723,12 +705,6 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_options_change(
"sensor": sensor,
}
config_entry = await setup_deconz_integration(
hass,
aioclient_mock,
options={CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False},
)
assert len(hass.states.async_all()) == 0
await mock_deconz_websocket(data=event_added_sensor)
@ -738,16 +714,19 @@ async def test_add_new_binary_sensor_ignored_load_entities_on_options_change(
assert not hass.states.get("binary_sensor.presence_sensor")
assert (
len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
len(
er.async_entries_for_config_entry(
entity_registry, config_entry_setup.entry_id
)
)
== 0
)
aioclient_mock.clear_requests()
data = {"config": {}, "groups": {}, "lights": {}, "sensors": {"1": sensor}}
mock_deconz_request(aioclient_mock, config_entry.data, data)
deconz_payload["sensors"] = {"1": sensor}
mock_requests()
hass.config_entries.async_update_entry(
config_entry, options={CONF_ALLOW_NEW_DEVICES: True}
config_entry_setup, options={CONF_ALLOW_NEW_DEVICES: True}
)
await hass.async_block_till_done()