Use snapshot in deCONZ button tests (#122505)

Use snapshot in button tests
This commit is contained in:
Robert Svensson 2024-07-24 08:44:10 +02:00 committed by GitHub
parent b7b3094a49
commit 902bf4ae86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 109 additions and 46 deletions

View File

@ -0,0 +1,95 @@
# serializer version: 1
# name: test_button[deconz_payload0-expected0][button.light_group_scene_store_current_scene-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.light_group_scene_store_current_scene',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:inbox-arrow-down',
'original_name': 'Scene Store Current Scene',
'platform': 'deconz',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '01234E56789A/groups/1/scenes/1-store',
'unit_of_measurement': None,
})
# ---
# name: test_button[deconz_payload0-expected0][button.light_group_scene_store_current_scene-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Light group Scene Store Current Scene',
'icon': 'mdi:inbox-arrow-down',
}),
'context': <ANY>,
'entity_id': 'button.light_group_scene_store_current_scene',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[deconz_payload1-expected1][button.aqara_fp1_reset_presence-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.aqara_fp1_reset_presence',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.RESTART: 'restart'>,
'original_icon': None,
'original_name': 'Aqara FP1 Reset Presence',
'platform': 'deconz',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'xx:xx:xx:xx:xx:xx:xx:xx-01-0406-reset_presence',
'unit_of_measurement': None,
})
# ---
# name: test_button[deconz_payload1-expected1][button.aqara_fp1_reset_presence-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'restart',
'friendly_name': 'Aqara FP1 Reset Presence',
}),
'context': <ANY>,
'entity_id': 'button.aqara_fp1_reset_presence',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---

View File

@ -1,15 +1,19 @@
"""deCONZ button platform tests.""" """deCONZ button platform tests."""
from collections.abc import Callable from collections.abc import Callable
from typing import Any
from unittest.mock import patch
import pytest import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.config_entries import ConfigEntry 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, Platform
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 entity_registry as er
from tests.common import snapshot_platform
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
TEST_DATA = [ TEST_DATA = [
@ -28,15 +32,7 @@ TEST_DATA = [
} }
}, },
{ {
"entity_count": 2,
"device_count": 3,
"entity_id": "button.light_group_scene_store_current_scene", "entity_id": "button.light_group_scene_store_current_scene",
"unique_id": "01234E56789A/groups/1/scenes/1-store",
"entity_category": EntityCategory.CONFIG,
"attributes": {
"icon": "mdi:inbox-arrow-down",
"friendly_name": "Light group Scene Store Current Scene",
},
"request": "/groups/1/scenes/1/store", "request": "/groups/1/scenes/1/store",
"request_data": {}, "request_data": {},
}, },
@ -70,15 +66,7 @@ TEST_DATA = [
} }
}, },
{ {
"entity_count": 5,
"device_count": 3,
"entity_id": "button.aqara_fp1_reset_presence", "entity_id": "button.aqara_fp1_reset_presence",
"unique_id": "xx:xx:xx:xx:xx:xx:xx:xx-01-0406-reset_presence",
"entity_category": EntityCategory.CONFIG,
"attributes": {
"device_class": "restart",
"friendly_name": "Aqara FP1 Reset Presence",
},
"request": "/sensors/1/config", "request": "/sensors/1/config",
"request_data": {"resetpresence": True}, "request_data": {"resetpresence": True},
}, },
@ -90,36 +78,16 @@ TEST_DATA = [
async def test_button( async def test_button(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
config_entry_setup: ConfigEntry, config_entry_factory: ConfigEntry,
mock_put_request: Callable[[str, str], AiohttpClientMocker], mock_put_request: Callable[[str, str], AiohttpClientMocker],
expected, expected: dict[str, Any],
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test successful creation of button entities.""" """Test successful creation of button entities."""
assert len(hass.states.async_all()) == expected["entity_count"] with patch("homeassistant.components.deconz.PLATFORMS", [Platform.BUTTON]):
config_entry = await config_entry_factory()
# Verify state data await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
button = hass.states.get(expected["entity_id"])
assert button.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"]
)
# Verify button press # Verify button press
@ -135,11 +103,11 @@ async def test_button(
# Unload entry # 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 assert hass.states.get(expected["entity_id"]).state == STATE_UNAVAILABLE
# Remove entry # 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() await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0 assert len(hass.states.async_all()) == 0