Make Matter snapshot logic a shared function (#126744)

pull/126802/head
Joost Lekkerkerker 2024-09-25 15:12:48 +02:00 committed by GitHub
parent 10b9e3b29c
commit 6e4e5ba8c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 14 deletions

View File

@ -10,8 +10,11 @@ from unittest.mock import MagicMock
from matter_server.client.models.node import MatterNode
from matter_server.common.helpers.util import dataclass_from_dict
from matter_server.common.models import EventType, MatterNodeData
from syrupy import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, load_fixture
@ -89,3 +92,17 @@ async def trigger_subscription_callback(
if event_filter in (None, event):
callback(event, data)
await hass.async_block_till_done()
def snapshot_matter_entities(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
platform: Platform,
) -> None:
"""Snapshot Matter entities."""
entities = hass.states.async_all(platform)
for entity_state in entities:
entity_entry = entity_registry.async_get(entity_state.entity_id)
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
assert entity_state == snapshot(name=f"{entity_entry.entity_id}-state")

View File

@ -17,6 +17,7 @@ from homeassistant.helpers import entity_registry as er
from .common import (
set_node_attribute,
setup_integration_with_node_fixture,
snapshot_matter_entities,
trigger_subscription_callback,
)
@ -136,10 +137,4 @@ async def test_binary_sensors(
snapshot: SnapshotAssertion,
) -> None:
"""Test binary sensors."""
entities = hass.states.async_all(Platform.BINARY_SENSOR)
for entity_state in entities:
entity_entry = entity_registry.async_get(entity_state.entity_id)
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
state = hass.states.get(entity_entry.entity_id)
assert state, f"State not found for {entity_entry.entity_id}"
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
snapshot_matter_entities(hass, entity_registry, snapshot, Platform.BINARY_SENSOR)

View File

@ -13,6 +13,7 @@ from homeassistant.helpers import entity_registry as er
from .common import (
set_node_attribute,
setup_integration_with_node_fixture,
snapshot_matter_entities,
trigger_subscription_callback,
)
@ -439,10 +440,4 @@ async def test_sensors(
snapshot: SnapshotAssertion,
) -> None:
"""Test sensors."""
entities = hass.states.async_all(Platform.SENSOR)
for entity_state in entities:
entity_entry = entity_registry.async_get(entity_state.entity_id)
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
state = hass.states.get(entity_entry.entity_id)
assert state, f"State not found for {entity_entry.entity_id}"
assert state == snapshot(name=f"{entity_entry.entity_id}-state")
snapshot_matter_entities(hass, entity_registry, snapshot, Platform.SENSOR)