From a8e60a6c53269e48dea20d6948c7b6327dfc4137 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 24 Jul 2024 17:28:47 +0200 Subject: [PATCH] Use snapshot in deCONZ number tests (#122538) --- .../deconz/snapshots/test_number.ambr | 211 ++++++++++++++++++ tests/components/deconz/test_number.py | 69 ++---- 2 files changed, 224 insertions(+), 56 deletions(-) create mode 100644 tests/components/deconz/snapshots/test_number.ambr diff --git a/tests/components/deconz/snapshots/test_number.ambr b/tests/components/deconz/snapshots/test_number.ambr new file mode 100644 index 00000000000..5311addc7a1 --- /dev/null +++ b/tests/components/deconz/snapshots/test_number.ambr @@ -0,0 +1,211 @@ +# serializer version: 1 +# name: test_number_entities[sensor_payload0-expected0][binary_sensor.presence_sensor-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.presence_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Presence sensor', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00:00:00-00-presence', + 'unit_of_measurement': None, + }) +# --- +# name: test_number_entities[sensor_payload0-expected0][binary_sensor.presence_sensor-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'dark': False, + 'device_class': 'motion', + 'friendly_name': 'Presence sensor', + 'on': True, + 'temperature': 0.1, + }), + 'context': , + 'entity_id': 'binary_sensor.presence_sensor', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_number_entities[sensor_payload0-expected0][number.presence_sensor_delay-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'max': 65535, + 'min': 0, + 'mode': , + 'step': 1, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'number', + 'entity_category': , + 'entity_id': 'number.presence_sensor_delay', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Presence sensor Delay', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00:00:00-00-delay', + 'unit_of_measurement': None, + }) +# --- +# name: test_number_entities[sensor_payload0-expected0][number.presence_sensor_delay-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Presence sensor Delay', + 'max': 65535, + 'min': 0, + 'mode': , + 'step': 1, + }), + 'context': , + 'entity_id': 'number.presence_sensor_delay', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_number_entities[sensor_payload1-expected1][binary_sensor.presence_sensor-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.presence_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Presence sensor', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00:00:00-00-presence', + 'unit_of_measurement': None, + }) +# --- +# name: test_number_entities[sensor_payload1-expected1][binary_sensor.presence_sensor-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'dark': False, + 'device_class': 'motion', + 'friendly_name': 'Presence sensor', + 'on': True, + 'temperature': 0.1, + }), + 'context': , + 'entity_id': 'binary_sensor.presence_sensor', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_number_entities[sensor_payload1-expected1][number.presence_sensor_duration-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'max': 65535, + 'min': 0, + 'mode': , + 'step': 1, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'number', + 'entity_category': , + 'entity_id': 'number.presence_sensor_duration', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Presence sensor Duration', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00:00:00-00-duration', + 'unit_of_measurement': None, + }) +# --- +# name: test_number_entities[sensor_payload1-expected1][number.presence_sensor_duration-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Presence sensor Duration', + 'max': 65535, + 'min': 0, + 'mode': , + 'step': 1, + }), + 'context': , + 'entity_id': 'number.presence_sensor_duration', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- diff --git a/tests/components/deconz/test_number.py b/tests/components/deconz/test_number.py index 7b34402600d..66eccdc6b4c 100644 --- a/tests/components/deconz/test_number.py +++ b/tests/components/deconz/test_number.py @@ -2,22 +2,24 @@ from collections.abc import Callable from typing import Any +from unittest.mock import patch import pytest +from syrupy import SnapshotAssertion from homeassistant.components.number import ( ATTR_VALUE, DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE, ) -from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, EntityCategory +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import entity_registry as er -from .conftest import WebsocketDataType +from .conftest import ConfigEntryFactoryType, WebsocketDataType +from tests.common import snapshot_platform from tests.test_util.aiohttp import AiohttpClientMocker TEST_DATA = [ @@ -35,19 +37,7 @@ TEST_DATA = [ "uniqueid": "00:00:00:00:00:00:00:00-00", }, { - "entity_count": 3, - "device_count": 3, "entity_id": "number.presence_sensor_delay", - "unique_id": "00:00:00:00:00:00:00:00-00-delay", - "state": "0", - "entity_category": EntityCategory.CONFIG, - "attributes": { - "min": 0, - "max": 65535, - "step": 1, - "mode": "auto", - "friendly_name": "Presence sensor Delay", - }, "websocket_event": {"config": {"delay": 10}}, "next_state": "10", "supported_service_value": 111, @@ -71,19 +61,7 @@ TEST_DATA = [ "uniqueid": "00:00:00:00:00:00:00:00-00", }, { - "entity_count": 3, - "device_count": 3, "entity_id": "number.presence_sensor_duration", - "unique_id": "00:00:00:00:00:00:00:00-00-duration", - "state": "0", - "entity_category": EntityCategory.CONFIG, - "attributes": { - "min": 0, - "max": 65535, - "step": 1, - "mode": "auto", - "friendly_name": "Presence sensor Duration", - }, "websocket_event": {"config": {"duration": 10}}, "next_state": "10", "supported_service_value": 111, @@ -99,38 +77,17 @@ TEST_DATA = [ @pytest.mark.parametrize(("sensor_payload", "expected"), TEST_DATA) async def test_number_entities( hass: HomeAssistant, - device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, - config_entry_setup: ConfigEntry, + config_entry_factory: ConfigEntryFactoryType, sensor_ws_data: WebsocketDataType, mock_put_request: Callable[[str, str], AiohttpClientMocker], expected: dict[str, Any], + snapshot: SnapshotAssertion, ) -> None: """Test successful creation of number entities.""" - assert len(hass.states.async_all()) == expected["entity_count"] - - # Verify state data - - entity = hass.states.get(expected["entity_id"]) - assert entity.state == expected["state"] - assert entity.attributes == expected["attributes"] - - # Verify entity registry data - - ent_reg_entry = entity_registry.async_get(expected["entity_id"]) - assert ent_reg_entry.entity_category is expected["entity_category"] - assert ent_reg_entry.unique_id == expected["unique_id"] - - # Verify device registry data - - assert ( - len( - dr.async_entries_for_config_entry( - device_registry, config_entry_setup.entry_id - ) - ) - == expected["device_count"] - ) + with patch("homeassistant.components.deconz.PLATFORMS", [Platform.NUMBER]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) # Change state @@ -182,11 +139,11 @@ async def test_number_entities( # Unload entry - await hass.config_entries.async_unload(config_entry_setup.entry_id) + await hass.config_entries.async_unload(config_entry.entry_id) assert hass.states.get(expected["entity_id"]).state == STATE_UNAVAILABLE # Remove entry - await hass.config_entries.async_remove(config_entry_setup.entry_id) + await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0