From 08d7beb8033b24305b2568b9cf69bac1f9278c15 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 25 Jul 2024 17:32:31 +0200 Subject: [PATCH] Use snapshots in UniFi update tests (#122599) --- .../unifi/snapshots/test_update.ambr | 229 ++++++++++++++++++ tests/components/unifi/test_update.py | 75 +++--- 2 files changed, 256 insertions(+), 48 deletions(-) create mode 100644 tests/components/unifi/snapshots/test_update.ambr diff --git a/tests/components/unifi/snapshots/test_update.ambr b/tests/components/unifi/snapshots/test_update.ambr new file mode 100644 index 00000000000..99a403a8f21 --- /dev/null +++ b/tests/components/unifi/snapshots/test_update.ambr @@ -0,0 +1,229 @@ +# serializer version: 1 +# name: test_entity_and_device_data[site_payload0-device_payload0][update.device_1-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.device_1', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': None, + 'platform': 'unifi', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'device_update-00:00:00:00:01:01', + 'unit_of_measurement': None, + }) +# --- +# name: test_entity_and_device_data[site_payload0-device_payload0][update.device_1-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'device_class': 'firmware', + 'entity_picture': 'https://brands.home-assistant.io/_/unifi/icon.png', + 'friendly_name': 'Device 1', + 'in_progress': False, + 'installed_version': '4.0.42.10433', + 'latest_version': '4.3.17.11279', + 'release_summary': None, + 'release_url': None, + 'skipped_version': None, + 'supported_features': , + 'title': None, + }), + 'context': , + 'entity_id': 'update.device_1', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_entity_and_device_data[site_payload0-device_payload0][update.device_2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.device_2', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': None, + 'platform': 'unifi', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'device_update-00:00:00:00:01:02', + 'unit_of_measurement': None, + }) +# --- +# name: test_entity_and_device_data[site_payload0-device_payload0][update.device_2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'device_class': 'firmware', + 'entity_picture': 'https://brands.home-assistant.io/_/unifi/icon.png', + 'friendly_name': 'Device 2', + 'in_progress': False, + 'installed_version': '4.0.42.10433', + 'latest_version': '4.0.42.10433', + 'release_summary': None, + 'release_url': None, + 'skipped_version': None, + 'supported_features': , + 'title': None, + }), + 'context': , + 'entity_id': 'update.device_2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entity_and_device_data[site_payload1-device_payload0][update.device_1-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.device_1', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': None, + 'platform': 'unifi', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'device_update-00:00:00:00:01:01', + 'unit_of_measurement': None, + }) +# --- +# name: test_entity_and_device_data[site_payload1-device_payload0][update.device_1-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'device_class': 'firmware', + 'entity_picture': 'https://brands.home-assistant.io/_/unifi/icon.png', + 'friendly_name': 'Device 1', + 'in_progress': False, + 'installed_version': '4.0.42.10433', + 'latest_version': '4.3.17.11279', + 'release_summary': None, + 'release_url': None, + 'skipped_version': None, + 'supported_features': , + 'title': None, + }), + 'context': , + 'entity_id': 'update.device_1', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_entity_and_device_data[site_payload1-device_payload0][update.device_2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.device_2', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': None, + 'platform': 'unifi', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'device_update-00:00:00:00:01:02', + 'unit_of_measurement': None, + }) +# --- +# name: test_entity_and_device_data[site_payload1-device_payload0][update.device_2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'device_class': 'firmware', + 'entity_picture': 'https://brands.home-assistant.io/_/unifi/icon.png', + 'friendly_name': 'Device 2', + 'in_progress': False, + 'installed_version': '4.0.42.10433', + 'latest_version': '4.0.42.10433', + 'release_summary': None, + 'release_url': None, + 'skipped_version': None, + 'supported_features': , + 'title': None, + }), + 'context': , + 'entity_id': 'update.device_2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/unifi/test_update.py b/tests/components/unifi/test_update.py index 3b1de6c4456..a8fe9231159 100644 --- a/tests/components/unifi/test_update.py +++ b/tests/components/unifi/test_update.py @@ -1,9 +1,11 @@ """The tests for the UniFi Network update platform.""" from copy import deepcopy +from unittest.mock import patch from aiounifi.models.message import MessageKey import pytest +from syrupy import SnapshotAssertion from yarl import URL from homeassistant.components.unifi.const import CONF_SITE_ID @@ -13,23 +15,23 @@ from homeassistant.components.update import ( ATTR_LATEST_VERSION, DOMAIN as UPDATE_DOMAIN, SERVICE_INSTALL, - UpdateDeviceClass, - UpdateEntityFeature, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( - ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, - ATTR_SUPPORTED_FEATURES, CONF_HOST, STATE_OFF, STATE_ON, STATE_UNAVAILABLE, + Platform, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er +from tests.common import snapshot_platform from tests.test_util.aiohttp import AiohttpClientMocker +# Device with new firmware available DEVICE_1 = { "board_rev": 3, "device_id": "mock-id", @@ -46,6 +48,7 @@ DEVICE_1 = { "upgrade_to_firmware": "4.3.17.11279", } +# Device without new firmware available DEVICE_2 = { "board_rev": 3, "device_id": "mock-id", @@ -61,43 +64,38 @@ DEVICE_2 = { @pytest.mark.parametrize("device_payload", [[DEVICE_1, DEVICE_2]]) +@pytest.mark.parametrize( + "site_payload", + [ + [{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}], + [{"desc": "Site name", "name": "site_id", "role": "not admin", "_id": "1"}], + ], +) +async def test_entity_and_device_data( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry_factory, + snapshot: SnapshotAssertion, +) -> None: + """Validate entity and device data with and without admin rights.""" + with patch("homeassistant.components.unifi.PLATFORMS", [Platform.UPDATE]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) + + +@pytest.mark.parametrize("device_payload", [[DEVICE_1]]) @pytest.mark.usefixtures("config_entry_setup") async def test_device_updates(hass: HomeAssistant, mock_websocket_message) -> None: """Test the update_items function with some devices.""" - assert len(hass.states.async_entity_ids(UPDATE_DOMAIN)) == 2 - - # Device with new firmware available - device_1_state = hass.states.get("update.device_1") assert device_1_state.state == STATE_ON - assert device_1_state.attributes[ATTR_INSTALLED_VERSION] == "4.0.42.10433" - assert device_1_state.attributes[ATTR_LATEST_VERSION] == "4.3.17.11279" assert device_1_state.attributes[ATTR_IN_PROGRESS] is False - assert device_1_state.attributes[ATTR_DEVICE_CLASS] == UpdateDeviceClass.FIRMWARE - assert ( - device_1_state.attributes[ATTR_SUPPORTED_FEATURES] - == UpdateEntityFeature.PROGRESS | UpdateEntityFeature.INSTALL - ) - - # Device without new firmware available - - device_2_state = hass.states.get("update.device_2") - assert device_2_state.state == STATE_OFF - assert device_2_state.attributes[ATTR_INSTALLED_VERSION] == "4.0.42.10433" - assert device_2_state.attributes[ATTR_LATEST_VERSION] == "4.0.42.10433" - assert device_2_state.attributes[ATTR_IN_PROGRESS] is False - assert device_2_state.attributes[ATTR_DEVICE_CLASS] == UpdateDeviceClass.FIRMWARE - assert ( - device_2_state.attributes[ATTR_SUPPORTED_FEATURES] - == UpdateEntityFeature.PROGRESS | UpdateEntityFeature.INSTALL - ) # Simulate start of update device_1 = deepcopy(DEVICE_1) device_1["state"] = 4 mock_websocket_message(message=MessageKey.DEVICE, data=device_1) - await hass.async_block_till_done() device_1_state = hass.states.get("update.device_1") assert device_1_state.state == STATE_ON @@ -112,7 +110,6 @@ async def test_device_updates(hass: HomeAssistant, mock_websocket_message) -> No device_1["upgradable"] = False del device_1["upgrade_to_firmware"] mock_websocket_message(message=MessageKey.DEVICE, data=device_1) - await hass.async_block_till_done() device_1_state = hass.states.get("update.device_1") assert device_1_state.state == STATE_OFF @@ -121,22 +118,6 @@ async def test_device_updates(hass: HomeAssistant, mock_websocket_message) -> No assert device_1_state.attributes[ATTR_IN_PROGRESS] is False -@pytest.mark.parametrize("device_payload", [[DEVICE_1]]) -@pytest.mark.parametrize( - "site_payload", - [[{"desc": "Site name", "name": "site_id", "role": "not admin", "_id": "1"}]], -) -@pytest.mark.usefixtures("config_entry_setup") -async def test_not_admin(hass: HomeAssistant) -> None: - """Test that the INSTALL feature is not available on a non-admin account.""" - assert len(hass.states.async_entity_ids(UPDATE_DOMAIN)) == 1 - device_state = hass.states.get("update.device_1") - assert device_state.state == STATE_ON - assert ( - device_state.attributes[ATTR_SUPPORTED_FEATURES] == UpdateEntityFeature.PROGRESS - ) - - @pytest.mark.parametrize("device_payload", [[DEVICE_1]]) async def test_install( hass: HomeAssistant, @@ -144,7 +125,6 @@ async def test_install( config_entry_setup: ConfigEntry, ) -> None: """Test the device update install call.""" - assert len(hass.states.async_entity_ids(UPDATE_DOMAIN)) == 1 device_state = hass.states.get("update.device_1") assert device_state.state == STATE_ON @@ -176,7 +156,6 @@ async def test_install( @pytest.mark.usefixtures("config_entry_setup") async def test_hub_state_change(hass: HomeAssistant, mock_websocket_state) -> None: """Verify entities state reflect on hub becoming unavailable.""" - assert len(hass.states.async_entity_ids(UPDATE_DOMAIN)) == 1 assert hass.states.get("update.device_1").state == STATE_ON # Controller unavailable