Use fixtures in deCONZ number tests (#120938)

This commit is contained in:
Robert Svensson 2024-07-01 17:46:46 +02:00 committed by GitHub
parent 361e81821c
commit dcf4e91234
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
"""deCONZ number platform tests.""" """deCONZ number platform tests."""
from unittest.mock import patch from collections.abc import Callable
from typing import Any
import pytest import pytest
@ -9,41 +10,29 @@ from homeassistant.components.number import (
DOMAIN as NUMBER_DOMAIN, DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE, SERVICE_SET_VALUE,
) )
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.exceptions import ServiceValidationError from homeassistant.exceptions import ServiceValidationError
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_number_entities(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that no sensors in deconz results in no number entities."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
TEST_DATA = [ TEST_DATA = [
( # Presence sensor - delay configuration ( # Presence sensor - delay configuration
{ {
"name": "Presence sensor", "0": {
"type": "ZHAPresence", "name": "Presence sensor",
"state": {"dark": False, "presence": False}, "type": "ZHAPresence",
"config": { "state": {"dark": False, "presence": False},
"delay": 0, "config": {
"on": True, "delay": 0,
"reachable": True, "on": True,
"temperature": 10, "reachable": True,
}, "temperature": 10,
"uniqueid": "00:00:00:00:00:00:00:00-00", },
"uniqueid": "00:00:00:00:00:00:00:00-00",
}
}, },
{ {
"entity_count": 3, "entity_count": 3,
@ -70,16 +59,18 @@ TEST_DATA = [
), ),
( # Presence sensor - duration configuration ( # Presence sensor - duration configuration
{ {
"name": "Presence sensor", "0": {
"type": "ZHAPresence", "name": "Presence sensor",
"state": {"dark": False, "presence": False}, "type": "ZHAPresence",
"config": { "state": {"dark": False, "presence": False},
"duration": 0, "config": {
"on": True, "duration": 0,
"reachable": True, "on": True,
"temperature": 10, "reachable": True,
}, "temperature": 10,
"uniqueid": "00:00:00:00:00:00:00:00-00", },
"uniqueid": "00:00:00:00:00:00:00:00-00",
}
}, },
{ {
"entity_count": 3, "entity_count": 3,
@ -107,21 +98,17 @@ TEST_DATA = [
] ]
@pytest.mark.parametrize(("sensor_data", "expected"), TEST_DATA) @pytest.mark.parametrize(("sensor_payload", "expected"), TEST_DATA)
async def test_number_entities( async def test_number_entities(
hass: HomeAssistant, hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
config_entry_setup: ConfigEntry,
mock_deconz_websocket, mock_deconz_websocket,
sensor_data, mock_put_request: Callable[[str, str], AiohttpClientMocker],
expected, expected: dict[str, Any],
) -> None: ) -> None:
"""Test successful creation of number entities.""" """Test successful creation of number entities."""
with patch.dict(DECONZ_WEB_REQUEST, {"sensors": {"0": sensor_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
@ -139,7 +126,11 @@ async def test_number_entities(
# 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"]
) )
@ -157,7 +148,7 @@ async def test_number_entities(
# Verify service calls # Verify service calls
mock_deconz_put_request(aioclient_mock, config_entry.data, "/sensors/0/config") aioclient_mock = mock_put_request("/sensors/0/config")
# Service set supported value # Service set supported value
@ -200,11 +191,11 @@ async def test_number_entities(
# 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