From c5f9ff6ac5c299d13b1f6f85f4e8a2eec487dc07 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Wed, 24 Jul 2024 17:14:40 +0200 Subject: [PATCH] Use snapshot in deCONZ cover tests (#122537) --- .../deconz/snapshots/test_cover.ambr | 150 ++++++++++++++++++ tests/components/deconz/test_cover.py | 61 +++---- 2 files changed, 181 insertions(+), 30 deletions(-) create mode 100644 tests/components/deconz/snapshots/test_cover.ambr diff --git a/tests/components/deconz/snapshots/test_cover.ambr b/tests/components/deconz/snapshots/test_cover.ambr new file mode 100644 index 00000000000..5c50923453c --- /dev/null +++ b/tests/components/deconz/snapshots/test_cover.ambr @@ -0,0 +1,150 @@ +# serializer version: 1 +# name: test_cover[light_payload0][cover.window_covering_device-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.window_covering_device', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Window covering device', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00:00:01-00', + 'unit_of_measurement': None, + }) +# --- +# name: test_cover[light_payload0][cover.window_covering_device-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_position': 0, + 'device_class': 'shade', + 'friendly_name': 'Window covering device', + 'supported_features': , + }), + 'context': , + 'entity_id': 'cover.window_covering_device', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'closed', + }) +# --- +# name: test_level_controllable_output_cover[light_payload0][cover.vent-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.vent', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Vent', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:22:a3:00:00:00:00:00-01', + 'unit_of_measurement': None, + }) +# --- +# name: test_level_controllable_output_cover[light_payload0][cover.vent-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_position': 5, + 'current_tilt_position': 97, + 'device_class': 'damper', + 'friendly_name': 'Vent', + 'supported_features': , + }), + 'context': , + 'entity_id': 'cover.vent', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'open', + }) +# --- +# name: test_tilt_cover[light_payload0][cover.covering_device-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.covering_device', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Covering device', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:24:46:00:00:12:34:56-01', + 'unit_of_measurement': None, + }) +# --- +# name: test_tilt_cover[light_payload0][cover.covering_device-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_position': 100, + 'current_tilt_position': 100, + 'device_class': 'shade', + 'friendly_name': 'Covering device', + 'supported_features': , + }), + 'context': , + 'entity_id': 'cover.covering_device', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'open', + }) +# --- diff --git a/tests/components/deconz/test_cover.py b/tests/components/deconz/test_cover.py index d04fb43a0d7..a93d40b4d1e 100644 --- a/tests/components/deconz/test_cover.py +++ b/tests/components/deconz/test_cover.py @@ -1,12 +1,13 @@ """deCONZ cover platform tests.""" from collections.abc import Callable +from unittest.mock import patch import pytest +from syrupy import SnapshotAssertion from homeassistant.components.cover import ( ATTR_CURRENT_POSITION, - ATTR_CURRENT_TILT_POSITION, ATTR_POSITION, ATTR_TILT_POSITION, DOMAIN as COVER_DOMAIN, @@ -19,17 +20,13 @@ from homeassistant.components.cover import ( SERVICE_STOP_COVER, SERVICE_STOP_COVER_TILT, ) -from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - ATTR_ENTITY_ID, - STATE_CLOSED, - STATE_OPEN, - STATE_UNAVAILABLE, -) +from homeassistant.const import ATTR_ENTITY_ID, STATE_OPEN, STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant +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 @@ -55,16 +52,16 @@ from tests.test_util.aiohttp import AiohttpClientMocker ) async def test_cover( hass: HomeAssistant, - config_entry_setup: ConfigEntry, + entity_registry: er.EntityRegistry, + config_entry_factory: ConfigEntryFactoryType, mock_put_request: Callable[[str, str], AiohttpClientMocker], light_ws_data: WebsocketDataType, + snapshot: SnapshotAssertion, ) -> None: """Test that all supported cover entities are created.""" - assert len(hass.states.async_all()) == 2 - cover = hass.states.get("cover.window_covering_device") - assert cover.state == STATE_CLOSED - assert cover.attributes[ATTR_CURRENT_POSITION] == 0 - assert not hass.states.get("cover.unsupported_cover") + with patch("homeassistant.components.deconz.PLATFORMS", [Platform.COVER]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) # Event signals cover is open @@ -117,14 +114,14 @@ async def test_cover( ) assert aioclient_mock.mock_calls[4][2] == {"stop": True} - await hass.config_entries.async_unload(config_entry_setup.entry_id) + await hass.config_entries.async_unload(config_entry.entry_id) states = hass.states.async_all() - assert len(states) == 2 + assert len(states) == 1 for state in states: assert state.state == STATE_UNAVAILABLE - 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 @@ -153,15 +150,17 @@ async def test_cover( } ], ) -@pytest.mark.usefixtures("config_entry_setup") async def test_tilt_cover( - hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker] + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry_factory: ConfigEntryFactoryType, + mock_put_request: Callable[[str, str], AiohttpClientMocker], + snapshot: SnapshotAssertion, ) -> None: """Test that tilting a cover works.""" - assert len(hass.states.async_all()) == 1 - covering_device = hass.states.get("cover.covering_device") - assert covering_device.state == STATE_OPEN - assert covering_device.attributes[ATTR_CURRENT_TILT_POSITION] == 100 + with patch("homeassistant.components.deconz.PLATFORMS", [Platform.COVER]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) # Verify service calls for tilting cover @@ -232,15 +231,17 @@ async def test_tilt_cover( } ], ) -@pytest.mark.usefixtures("config_entry_setup") async def test_level_controllable_output_cover( - hass: HomeAssistant, mock_put_request: Callable[[str, str], AiohttpClientMocker] + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry_factory: ConfigEntryFactoryType, + mock_put_request: Callable[[str, str], AiohttpClientMocker], + snapshot: SnapshotAssertion, ) -> None: """Test that tilting a cover works.""" - assert len(hass.states.async_all()) == 1 - covering_device = hass.states.get("cover.vent") - assert covering_device.state == STATE_OPEN - assert covering_device.attributes[ATTR_CURRENT_TILT_POSITION] == 97 + with patch("homeassistant.components.deconz.PLATFORMS", [Platform.COVER]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) # Verify service calls for tilting cover