Use snapshots in UniFi update tests (#122599)

This commit is contained in:
Robert Svensson 2024-07-25 17:32:31 +02:00 committed by GitHub
parent 131ce09490
commit 08d7beb803
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 256 additions and 48 deletions

View File

@ -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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.device_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <UpdateDeviceClass.FIRMWARE: 'firmware'>,
'original_icon': None,
'original_name': None,
'platform': 'unifi',
'previous_unique_id': None,
'supported_features': <UpdateEntityFeature: 5>,
'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': <UpdateEntityFeature: 5>,
'title': None,
}),
'context': <ANY>,
'entity_id': 'update.device_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.device_2',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <UpdateDeviceClass.FIRMWARE: 'firmware'>,
'original_icon': None,
'original_name': None,
'platform': 'unifi',
'previous_unique_id': None,
'supported_features': <UpdateEntityFeature: 5>,
'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': <UpdateEntityFeature: 5>,
'title': None,
}),
'context': <ANY>,
'entity_id': 'update.device_2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.device_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <UpdateDeviceClass.FIRMWARE: 'firmware'>,
'original_icon': None,
'original_name': None,
'platform': 'unifi',
'previous_unique_id': None,
'supported_features': <UpdateEntityFeature: 4>,
'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': <UpdateEntityFeature: 4>,
'title': None,
}),
'context': <ANY>,
'entity_id': 'update.device_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.device_2',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <UpdateDeviceClass.FIRMWARE: 'firmware'>,
'original_icon': None,
'original_name': None,
'platform': 'unifi',
'previous_unique_id': None,
'supported_features': <UpdateEntityFeature: 4>,
'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': <UpdateEntityFeature: 4>,
'title': None,
}),
'context': <ANY>,
'entity_id': 'update.device_2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -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