diff --git a/tests/components/matter/common.py b/tests/components/matter/common.py index a1cdcf699a6d46..519b4c4027d1c6 100644 --- a/tests/components/matter/common.py +++ b/tests/components/matter/common.py @@ -10,8 +10,11 @@ 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") diff --git a/tests/components/matter/test_binary_sensor.py b/tests/components/matter/test_binary_sensor.py index 61518053897387..e0dd445cd72dd0 100644 --- a/tests/components/matter/test_binary_sensor.py +++ b/tests/components/matter/test_binary_sensor.py @@ -17,6 +17,7 @@ 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) diff --git a/tests/components/matter/test_sensor.py b/tests/components/matter/test_sensor.py index 0d67c33bbfb44c..b914e2160b55d9 100644 --- a/tests/components/matter/test_sensor.py +++ b/tests/components/matter/test_sensor.py @@ -13,6 +13,7 @@ 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)