Skip to content

Commit

Permalink
Make Matter snapshot logic a shared function (#126744)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Sep 25, 2024
1 parent 10b9e3b commit 6e4e5ba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
17 changes: 17 additions & 0 deletions tests/components/matter/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
9 changes: 2 additions & 7 deletions tests/components/matter/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .common import (
set_node_attribute,
setup_integration_with_node_fixture,
snapshot_matter_entities,
trigger_subscription_callback,
)

Expand Down Expand Up @@ -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)
9 changes: 2 additions & 7 deletions tests/components/matter/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .common import (
set_node_attribute,
setup_integration_with_node_fixture,
snapshot_matter_entities,
trigger_subscription_callback,
)

Expand Down Expand Up @@ -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)

0 comments on commit 6e4e5ba

Please sign in to comment.