Use fixtures in deCONZ scene tests (#120936)

pull/120944/head
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
1 changed files with 34 additions and 47 deletions

View File

@ -1,44 +1,29 @@
"""deCONZ scene platform tests."""
from unittest.mock import patch
from collections.abc import Callable
from typing import Any
import pytest
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.core import HomeAssistant
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
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 = [
( # Scene
{
"groups": {
"1": {
"id": "Light group id",
"name": "Light group",
"type": "LightGroup",
"state": {"all_on": False, "any_on": True},
"action": {},
"scenes": [{"id": "1", "name": "Scene"}],
"lights": [],
}
"1": {
"id": "Light group id",
"name": "Light group",
"type": "LightGroup",
"state": {"all_on": False, "any_on": True},
"action": {},
"scenes": [{"id": "1", "name": "Scene"}],
"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(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
raw_data,
expected,
config_entry_setup: ConfigEntry,
mock_put_request: Callable[[str, str], AiohttpClientMocker],
expected: dict[str, Any],
) -> None:
"""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"]
# Verify state data
@ -85,13 +67,17 @@ async def test_scenes(
# 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"]
)
# 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(
SCENE_DOMAIN,
@ -103,22 +89,20 @@ async def test_scenes(
# 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_only_new_scenes_are_created(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_deconz_websocket
) -> None:
"""Test that scenes works."""
data = {
"groups": {
@pytest.mark.parametrize(
"group_payload",
[
{
"1": {
"id": "Light group id",
"name": "Light group",
@ -129,10 +113,13 @@ async def test_only_new_scenes_are_created(
"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
event_changed_group = {