Use fixtures in deCONZ select tests (#120943)

This commit is contained in:
Robert Svensson 2024-07-01 17:29:57 +02:00 committed by GitHub
parent ce54ca9c8e
commit 77fc1c991c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
"""deCONZ select platform tests.""" """deCONZ select platform tests."""
from unittest.mock import patch from collections.abc import Callable
from typing import Any
from pydeconz.models.sensor.presence import ( from pydeconz.models.sensor.presence import (
PresenceConfigDeviceMode, PresenceConfigDeviceMode,
@ -13,31 +14,16 @@ from homeassistant.components.select import (
DOMAIN as SELECT_DOMAIN, DOMAIN as SELECT_DOMAIN,
SERVICE_SELECT_OPTION, SERVICE_SELECT_OPTION,
) )
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_select_entities(
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 = [
( # Presence Device Mode ( # Presence Device Mode
{ {
"sensors": {
"1": { "1": {
"config": { "config": {
"devicemode": "undirected", "devicemode": "undirected",
@ -61,7 +47,6 @@ TEST_DATA = [
"type": "ZHAPresence", "type": "ZHAPresence",
"uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406", "uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406",
} }
}
}, },
{ {
"entity_count": 5, "entity_count": 5,
@ -80,7 +65,6 @@ TEST_DATA = [
), ),
( # Presence Sensitivity ( # Presence Sensitivity
{ {
"sensors": {
"1": { "1": {
"config": { "config": {
"devicemode": "undirected", "devicemode": "undirected",
@ -104,7 +88,6 @@ TEST_DATA = [
"type": "ZHAPresence", "type": "ZHAPresence",
"uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406", "uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406",
} }
}
}, },
{ {
"entity_count": 5, "entity_count": 5,
@ -123,7 +106,6 @@ TEST_DATA = [
), ),
( # Presence Trigger Distance ( # Presence Trigger Distance
{ {
"sensors": {
"1": { "1": {
"config": { "config": {
"devicemode": "undirected", "devicemode": "undirected",
@ -147,7 +129,6 @@ TEST_DATA = [
"type": "ZHAPresence", "type": "ZHAPresence",
"uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406", "uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406",
} }
}
}, },
{ {
"entity_count": 5, "entity_count": 5,
@ -167,19 +148,16 @@ TEST_DATA = [
] ]
@pytest.mark.parametrize(("raw_data", "expected"), TEST_DATA) @pytest.mark.parametrize(("sensor_payload", "expected"), TEST_DATA)
async def test_select( async def test_select(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker, config_entry_setup: ConfigEntry,
raw_data, mock_put_request: Callable[[str, str], AiohttpClientMocker],
expected, expected: dict[str, Any],
) -> 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
@ -196,13 +174,16 @@ async def test_select(
# 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 selecting option # Verify selecting option
aioclient_mock = mock_put_request(expected["request"])
mock_deconz_put_request(aioclient_mock, config_entry.data, expected["request"])
await hass.services.async_call( await hass.services.async_call(
SELECT_DOMAIN, SELECT_DOMAIN,
@ -217,11 +198,11 @@ async def test_select(
# 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