From 07b25939a287e7ed20d3648b8a1f4674cc9f25d6 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 13 Mar 2023 15:23:00 +0100 Subject: [PATCH] Use SnapshotAssertion in SFR button tests (#89633) --- tests/components/sfr_box/__init__.py | 54 ------------- tests/components/sfr_box/const.py | 42 ----------- .../sfr_box/snapshots/test_button.ambr | 75 +++++++++++++++++++ tests/components/sfr_box/test_button.py | 25 +++++-- 4 files changed, 93 insertions(+), 103 deletions(-) delete mode 100644 tests/components/sfr_box/const.py create mode 100644 tests/components/sfr_box/snapshots/test_button.ambr diff --git a/tests/components/sfr_box/__init__.py b/tests/components/sfr_box/__init__.py index 651c419f4e9..52d911ef832 100644 --- a/tests/components/sfr_box/__init__.py +++ b/tests/components/sfr_box/__init__.py @@ -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}" - ) diff --git a/tests/components/sfr_box/const.py b/tests/components/sfr_box/const.py deleted file mode 100644 index 44a2ce4a575..00000000000 --- a/tests/components/sfr_box/const.py +++ /dev/null @@ -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", - }, - ], -} diff --git a/tests/components/sfr_box/snapshots/test_button.ambr b/tests/components/sfr_box/snapshots/test_button.ambr new file mode 100644 index 00000000000..a7ce9334e1b --- /dev/null +++ b/tests/components/sfr_box/snapshots/test_button.ambr @@ -0,0 +1,75 @@ +# serializer version: 1 +# name: test_buttons + list([ + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': 'http://192.168.0.1', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + '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': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.sfr_box_reboot', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + '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': , + 'entity_id': 'button.sfr_box_reboot', + 'last_changed': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/sfr_box/test_button.py b/tests/components/sfr_box/test_button.py index 22666505cfa..d1bb06fc79c 100644 --- a/tests/components/sfr_box/test_button.py +++ b/tests/components/sfr_box/test_button.py @@ -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"}