Implement snapshot testing for Plugwise button platform (#149984)

This commit is contained in:
Bouwe Westerdijk
2025-08-05 15:31:02 +02:00
committed by GitHub
parent f714388130
commit 70c9b1f095
3 changed files with 94 additions and 20 deletions

View File

@@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Generator from collections.abc import AsyncGenerator, Generator
import json import json
from typing import Any from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
@@ -130,6 +130,28 @@ def mock_smile_config_flow() -> Generator[MagicMock]:
yield api yield api
@pytest.fixture
def platforms() -> list[str]:
"""Fixture for platforms."""
return []
@pytest.fixture
async def setup_platform(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
platforms,
) -> AsyncGenerator[None]:
"""Set up one or all platforms."""
mock_config_entry.add_to_hass(hass)
with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
yield mock_config_entry
@pytest.fixture @pytest.fixture
def mock_smile_adam() -> Generator[MagicMock]: def mock_smile_adam() -> Generator[MagicMock]:
"""Create a Mock Adam environment for testing exceptions.""" """Create a Mock Adam environment for testing exceptions."""

View File

@@ -0,0 +1,50 @@
# serializer version: 1
# name: test_adam_button_snapshot[platforms0][button.adam_reboot-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.adam_reboot',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.RESTART: 'restart'>,
'original_icon': None,
'original_name': 'Reboot',
'platform': 'plugwise',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'reboot',
'unique_id': 'fe799307f1624099878210aa0b9f1475-reboot',
'unit_of_measurement': None,
})
# ---
# name: test_adam_button_snapshot[platforms0][button.adam_reboot-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'restart',
'friendly_name': 'Adam Reboot',
}),
'context': <ANY>,
'entity_id': 'button.adam_reboot',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---

View File

@@ -2,32 +2,34 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
from homeassistant.components.button import ( import pytest
DOMAIN as BUTTON_DOMAIN, from syrupy.assertion import SnapshotAssertion
SERVICE_PRESS,
ButtonDeviceClass, from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
) from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry from tests.common import MockConfigEntry, snapshot_platform
async def test_adam_reboot_button( @pytest.mark.parametrize("platforms", [(BUTTON_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_adam_button_snapshot(
hass: HomeAssistant,
mock_smile_adam: MagicMock,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Adam button snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
async def test_adam_press_reboot_button(
hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry
) -> None: ) -> None:
"""Test creation of button entities.""" """Test pressing of button entity."""
state = hass.states.get("button.adam_reboot")
assert state
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_DEVICE_CLASS) == ButtonDeviceClass.RESTART
registry = er.async_get(hass)
entry = registry.async_get("button.adam_reboot")
assert entry
assert entry.unique_id == "fe799307f1624099878210aa0b9f1475-reboot"
await hass.services.async_call( await hass.services.async_call(
BUTTON_DOMAIN, BUTTON_DOMAIN,
SERVICE_PRESS, SERVICE_PRESS,