Use fixtures in deCONZ scene tests (#120936)

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

View File

@ -1,44 +1,29 @@
"""deCONZ scene platform tests.""" """deCONZ scene platform tests."""
from unittest.mock import patch from collections.abc import Callable
from typing import Any
import pytest import pytest
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
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_scenes(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that scenes can be loaded without scenes being available."""
await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 0
TEST_DATA = [ TEST_DATA = [
( # Scene ( # Scene
{ {
"groups": { "1": {
"1": { "id": "Light group id",
"id": "Light group id", "name": "Light group",
"name": "Light group", "type": "LightGroup",
"type": "LightGroup", "state": {"all_on": False, "any_on": True},
"state": {"all_on": False, "any_on": True}, "action": {},
"action": {}, "scenes": [{"id": "1", "name": "Scene"}],
"scenes": [{"id": "1", "name": "Scene"}], "lights": [],
"lights": [],
}
} }
}, },
{ {
@ -56,19 +41,16 @@ TEST_DATA = [
] ]
@pytest.mark.parametrize(("raw_data", "expected"), TEST_DATA) @pytest.mark.parametrize(("group_payload", "expected"), TEST_DATA)
async def test_scenes( async def test_scenes(
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 scene entities.""" """Test successful creation of scene 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
@ -85,13 +67,17 @@ async def test_scenes(
# 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(
SCENE_DOMAIN, SCENE_DOMAIN,
@ -103,22 +89,20 @@ async def test_scenes(
# 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
async def test_only_new_scenes_are_created( @pytest.mark.parametrize(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket "group_payload",
) -> None: [
"""Test that scenes works.""" {
data = {
"groups": {
"1": { "1": {
"id": "Light group id", "id": "Light group id",
"name": "Light group", "name": "Light group",
@ -129,10 +113,13 @@ async def test_only_new_scenes_are_created(
"lights": [], "lights": [],
} }
} }
} ],
with patch.dict(DECONZ_WEB_REQUEST, data): )
await setup_deconz_integration(hass, aioclient_mock) @pytest.mark.usefixtures("config_entry_setup")
async def test_only_new_scenes_are_created(
hass: HomeAssistant, mock_deconz_websocket
) -> None:
"""Test that scenes works."""
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2
event_changed_group = { event_changed_group = {