Add full device snapshot tests for Shelly (#145620)

This commit is contained in:
Maciej Bieniek 2025-06-24 10:16:46 +02:00 committed by GitHub
parent f2944f4d8e
commit 438aa3486d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 4779 additions and 4 deletions

View File

@ -9,6 +9,7 @@ from unittest.mock import Mock
from aioshelly.const import MODEL_25
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.shelly.const import (
CONF_GEN,
@ -151,3 +152,30 @@ def register_device(
config_entry_id=config_entry.entry_id,
connections={(CONNECTION_NETWORK_MAC, format_mac(MOCK_MAC))},
)
async def snapshot_device_entities(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
config_entry_id: str,
) -> None:
"""Snapshot all device entities."""
entity_entries = er.async_entries_for_config_entry(entity_registry, config_entry_id)
assert entity_entries
for entity_entry in entity_entries:
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
assert entity_entry.disabled_by is None, "Please enable all entities."
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")
async def force_uptime_value(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Force time to a specific point."""
await hass.config.async_set_time_zone("UTC")
freezer.move_to("2025-05-26 16:04:00+00:00")

File diff suppressed because it is too large Load Diff

View File

@ -3,24 +3,29 @@
from unittest.mock import Mock
from aioshelly.const import MODEL_2PM_G3, MODEL_PRO_EM3
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.shelly.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceRegistry
from homeassistant.helpers.entity_registry import EntityRegistry
from . import init_integration
from . import force_uptime_value, init_integration, snapshot_device_entities
from tests.common import async_load_json_object_fixture
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_shelly_2pm_gen3_no_relay_names(
hass: HomeAssistant,
mock_rpc_device: Mock,
entity_registry: EntityRegistry,
device_registry: DeviceRegistry,
monkeypatch: pytest.MonkeyPatch,
snapshot: SnapshotAssertion,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test Shelly 2PM Gen3 without relay names.
@ -32,7 +37,9 @@ async def test_shelly_2pm_gen3_no_relay_names(
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
await init_integration(hass, gen=3, model=MODEL_2PM_G3)
await force_uptime_value(hass, freezer)
config_entry = await init_integration(hass, gen=3, model=MODEL_2PM_G3)
# Relay 0 sub-device
entity_id = "switch.test_name_switch_0"
@ -97,6 +104,10 @@ async def test_shelly_2pm_gen3_no_relay_names(
assert device_entry
assert device_entry.name == "Test name"
await snapshot_device_entities(
hass, entity_registry, snapshot, config_entry.entry_id
)
async def test_shelly_2pm_gen3_relay_names(
hass: HomeAssistant,
@ -183,12 +194,15 @@ async def test_shelly_2pm_gen3_relay_names(
assert device_entry.name == "Test name"
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_shelly_2pm_gen3_cover(
hass: HomeAssistant,
mock_rpc_device: Mock,
entity_registry: EntityRegistry,
device_registry: DeviceRegistry,
monkeypatch: pytest.MonkeyPatch,
snapshot: SnapshotAssertion,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test Shelly 2PM Gen3 with cover profile.
@ -201,7 +215,9 @@ async def test_shelly_2pm_gen3_cover(
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
await init_integration(hass, gen=3, model=MODEL_2PM_G3)
await force_uptime_value(hass, freezer)
config_entry = await init_integration(hass, gen=3, model=MODEL_2PM_G3)
entity_id = "cover.test_name"
@ -239,6 +255,10 @@ async def test_shelly_2pm_gen3_cover(
assert device_entry
assert device_entry.name == "Test name"
await snapshot_device_entities(
hass, entity_registry, snapshot, config_entry.entry_id
)
async def test_shelly_2pm_gen3_cover_with_name(
hass: HomeAssistant,
@ -298,12 +318,15 @@ async def test_shelly_2pm_gen3_cover_with_name(
assert device_entry.name == "Test name"
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_shelly_pro_3em(
hass: HomeAssistant,
mock_rpc_device: Mock,
entity_registry: EntityRegistry,
device_registry: DeviceRegistry,
monkeypatch: pytest.MonkeyPatch,
snapshot: SnapshotAssertion,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test Shelly Pro 3EM.
@ -314,7 +337,9 @@ async def test_shelly_pro_3em(
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
await init_integration(hass, gen=2, model=MODEL_PRO_EM3)
await force_uptime_value(hass, freezer)
config_entry = await init_integration(hass, gen=2, model=MODEL_PRO_EM3)
# Main device
entity_id = "sensor.test_name_total_active_power"
@ -368,6 +393,10 @@ async def test_shelly_pro_3em(
assert device_entry
assert device_entry.name == "Test name Phase C"
await snapshot_device_entities(
hass, entity_registry, snapshot, config_entry.entry_id
)
async def test_shelly_pro_3em_with_emeter_name(
hass: HomeAssistant,