From 5bda07214183d7475741ffe9a4faaf0a00ba6353 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 24 Jul 2024 17:32:57 +0200 Subject: [PATCH] Use snapshot in deCONZ scene tests (#122540) --- .../deconz/snapshots/test_scene.ambr | 47 +++++++++++++++++ tests/components/deconz/test_scene.py | 51 +++++-------------- 2 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 tests/components/deconz/snapshots/test_scene.ambr diff --git a/tests/components/deconz/snapshots/test_scene.ambr b/tests/components/deconz/snapshots/test_scene.ambr new file mode 100644 index 00000000000..85a5ab92c5c --- /dev/null +++ b/tests/components/deconz/snapshots/test_scene.ambr @@ -0,0 +1,47 @@ +# serializer version: 1 +# name: test_scenes[group_payload0-expected0][scene.light_group_scene-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'scene', + 'entity_category': None, + 'entity_id': 'scene.light_group_scene', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Scene', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '01234E56789A/groups/1/scenes/1', + 'unit_of_measurement': None, + }) +# --- +# name: test_scenes[group_payload0-expected0][scene.light_group_scene-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Light group Scene', + }), + 'context': , + 'entity_id': 'scene.light_group_scene', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/deconz/test_scene.py b/tests/components/deconz/test_scene.py index 60746311928..d3a5725f7b1 100644 --- a/tests/components/deconz/test_scene.py +++ b/tests/components/deconz/test_scene.py @@ -2,17 +2,19 @@ from collections.abc import Callable from typing import Any +from unittest.mock import patch import pytest +from syrupy import SnapshotAssertion 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, Platform from homeassistant.core import HomeAssistant -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import entity_registry as er -from .conftest import WebsocketDataType +from .conftest import ConfigEntryFactoryType, WebsocketDataType +from tests.common import snapshot_platform from tests.test_util.aiohttp import AiohttpClientMocker TEST_DATA = [ @@ -29,14 +31,7 @@ TEST_DATA = [ } }, { - "entity_count": 2, - "device_count": 3, "entity_id": "scene.light_group_scene", - "unique_id": "01234E56789A/groups/1/scenes/1", - "entity_category": None, - "attributes": { - "friendly_name": "Light group Scene", - }, "request": "/groups/1/scenes/1/recall", }, ), @@ -46,36 +41,16 @@ TEST_DATA = [ @pytest.mark.parametrize(("group_payload", "expected"), TEST_DATA) async def test_scenes( hass: HomeAssistant, - device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, - config_entry_setup: ConfigEntry, + config_entry_factory: ConfigEntryFactoryType, mock_put_request: Callable[[str, str], AiohttpClientMocker], expected: dict[str, Any], + snapshot: SnapshotAssertion, ) -> None: """Test successful creation of scene entities.""" - assert len(hass.states.async_all()) == expected["entity_count"] - - # Verify state data - - scene = hass.states.get(expected["entity_id"]) - assert scene.attributes == expected["attributes"] - - # Verify entity registry data - - ent_reg_entry = entity_registry.async_get(expected["entity_id"]) - assert ent_reg_entry.entity_category is expected["entity_category"] - assert ent_reg_entry.unique_id == expected["unique_id"] - - # Verify device registry data - - assert ( - len( - dr.async_entries_for_config_entry( - device_registry, config_entry_setup.entry_id - ) - ) - == expected["device_count"] - ) + with patch("homeassistant.components.deconz.PLATFORMS", [Platform.SCENE]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) # Verify button press @@ -91,12 +66,12 @@ async def test_scenes( # Unload entry - await hass.config_entries.async_unload(config_entry_setup.entry_id) + await hass.config_entries.async_unload(config_entry.entry_id) assert hass.states.get(expected["entity_id"]).state == STATE_UNAVAILABLE # Remove entry - await hass.config_entries.async_remove(config_entry_setup.entry_id) + await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0