mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Use SnapshotAssertion in SFR button tests (#89633)
This commit is contained in:
parent
15506da332
commit
07b25939a2
@ -1,55 +1 @@
|
||||
"""Tests for the SFR Box integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from types import MappingProxyType
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_IDENTIFIERS,
|
||||
ATTR_MODEL,
|
||||
ATTR_NAME,
|
||||
ATTR_STATE,
|
||||
ATTR_SW_VERSION,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceRegistry
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
|
||||
from .const import ATTR_UNIQUE_ID, FIXED_ATTRIBUTES
|
||||
|
||||
|
||||
def check_device_registry(
|
||||
device_registry: DeviceRegistry, expected_device: MappingProxyType
|
||||
) -> None:
|
||||
"""Ensure that the expected_device is correctly registered."""
|
||||
assert len(device_registry.devices) == 1
|
||||
registry_entry = device_registry.async_get_device(expected_device[ATTR_IDENTIFIERS])
|
||||
assert registry_entry is not None
|
||||
assert registry_entry.identifiers == expected_device[ATTR_IDENTIFIERS]
|
||||
assert registry_entry.name == expected_device[ATTR_NAME]
|
||||
assert registry_entry.model == expected_device[ATTR_MODEL]
|
||||
assert registry_entry.sw_version == expected_device[ATTR_SW_VERSION]
|
||||
|
||||
|
||||
def check_entities(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: EntityRegistry,
|
||||
expected_entities: MappingProxyType,
|
||||
) -> None:
|
||||
"""Ensure that the expected_entities are correct."""
|
||||
for expected_entity in expected_entities:
|
||||
entity_id = expected_entity[ATTR_ENTITY_ID]
|
||||
registry_entry = entity_registry.entities.get(entity_id)
|
||||
assert registry_entry is not None
|
||||
assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID]
|
||||
state = hass.states.get(entity_id)
|
||||
assert state, f"Expected valid state for {entity_id}, got {state}"
|
||||
assert state.state == expected_entity[ATTR_STATE], (
|
||||
f"Expected state {expected_entity[ATTR_STATE]}, got {state.state} for"
|
||||
f" {entity_id}"
|
||||
)
|
||||
for attr in FIXED_ATTRIBUTES:
|
||||
assert state.attributes.get(attr) == expected_entity.get(attr), (
|
||||
f"Expected attribute {attr} == {expected_entity.get(attr)}, got"
|
||||
f" {state.attributes.get(attr)} for {entity_id}"
|
||||
)
|
||||
|
@ -1,42 +0,0 @@
|
||||
"""Constants for SFR Box tests."""
|
||||
from homeassistant.components.button import ButtonDeviceClass
|
||||
from homeassistant.components.sensor import ATTR_OPTIONS, ATTR_STATE_CLASS
|
||||
from homeassistant.components.sfr_box.const import DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_IDENTIFIERS,
|
||||
ATTR_MODEL,
|
||||
ATTR_NAME,
|
||||
ATTR_STATE,
|
||||
ATTR_SW_VERSION,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
STATE_UNKNOWN,
|
||||
Platform,
|
||||
)
|
||||
|
||||
ATTR_DEFAULT_DISABLED = "default_disabled"
|
||||
ATTR_UNIQUE_ID = "unique_id"
|
||||
FIXED_ATTRIBUTES = (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_OPTIONS,
|
||||
ATTR_STATE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
|
||||
EXPECTED_ENTITIES = {
|
||||
"expected_device": {
|
||||
ATTR_IDENTIFIERS: {(DOMAIN, "e4:5d:51:00:11:22")},
|
||||
ATTR_MODEL: "NB6VAC-FXC-r0",
|
||||
ATTR_NAME: "SFR Box",
|
||||
ATTR_SW_VERSION: "NB6VAC-MAIN-R4.0.44k",
|
||||
},
|
||||
Platform.BUTTON: [
|
||||
{
|
||||
ATTR_DEVICE_CLASS: ButtonDeviceClass.RESTART,
|
||||
ATTR_ENTITY_ID: "button.sfr_box_reboot",
|
||||
ATTR_STATE: STATE_UNKNOWN,
|
||||
ATTR_UNIQUE_ID: "e4:5d:51:00:11:22_system_reboot",
|
||||
},
|
||||
],
|
||||
}
|
75
tests/components/sfr_box/snapshots/test_button.ambr
Normal file
75
tests/components/sfr_box/snapshots/test_button.ambr
Normal file
@ -0,0 +1,75 @@
|
||||
# serializer version: 1
|
||||
# name: test_buttons
|
||||
list([
|
||||
DeviceRegistryEntrySnapshot({
|
||||
'area_id': None,
|
||||
'config_entries': <ANY>,
|
||||
'configuration_url': 'http://192.168.0.1',
|
||||
'connections': set({
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'entry_type': None,
|
||||
'hw_version': None,
|
||||
'id': <ANY>,
|
||||
'identifiers': set({
|
||||
tuple(
|
||||
'sfr_box',
|
||||
'e4:5d:51:00:11:22',
|
||||
),
|
||||
}),
|
||||
'is_new': False,
|
||||
'manufacturer': None,
|
||||
'model': 'NB6VAC-FXC-r0',
|
||||
'name': 'SFR Box',
|
||||
'name_by_user': None,
|
||||
'suggested_area': None,
|
||||
'sw_version': 'NB6VAC-MAIN-R4.0.44k',
|
||||
'via_device_id': None,
|
||||
}),
|
||||
])
|
||||
# ---
|
||||
# name: test_buttons.1
|
||||
list([
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'button.sfr_box_reboot',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <ButtonDeviceClass.RESTART: 'restart'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Reboot',
|
||||
'platform': 'sfr_box',
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'unique_id': 'e4:5d:51:00:11:22_system_reboot',
|
||||
'unit_of_measurement': None,
|
||||
}),
|
||||
])
|
||||
# ---
|
||||
# name: test_buttons[button.sfr_box_reboot]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'restart',
|
||||
'friendly_name': 'SFR Box Reboot',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.sfr_box_reboot',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
@ -4,6 +4,7 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from sfrbox_api.exceptions import SFRBoxError
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
@ -12,9 +13,6 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from . import check_device_registry, check_entities
|
||||
from .const import EXPECTED_ENTITIES
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("system_get_info", "dsl_get_info")
|
||||
|
||||
|
||||
@ -32,17 +30,30 @@ async def test_buttons(
|
||||
config_entry_with_auth: ConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test for SFR Box buttons."""
|
||||
await hass.config_entries.async_setup(config_entry_with_auth.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
check_device_registry(device_registry, EXPECTED_ENTITIES["expected_device"])
|
||||
device_entries = dr.async_entries_for_config_entry(
|
||||
device_registry, config_entry_with_auth.entry_id
|
||||
)
|
||||
assert device_entries == snapshot
|
||||
|
||||
expected_entities = EXPECTED_ENTITIES[Platform.BUTTON]
|
||||
assert len(entity_registry.entities) == len(expected_entities)
|
||||
entity_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry_with_auth.entry_id
|
||||
)
|
||||
assert entity_entries == snapshot
|
||||
|
||||
check_entities(hass, entity_registry, expected_entities)
|
||||
for entity in entity_entries:
|
||||
assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id)
|
||||
|
||||
|
||||
async def test_reboot(hass: HomeAssistant, config_entry_with_auth: ConfigEntry) -> None:
|
||||
"""Test for SFR Box reboot button."""
|
||||
await hass.config_entries.async_setup(config_entry_with_auth.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Reboot success
|
||||
service_data = {ATTR_ENTITY_ID: "button.sfr_box_reboot"}
|
||||
|
Loading…
x
Reference in New Issue
Block a user