Use snapshot in deCONZ number tests (#122538)

This commit is contained in:
Robert Svensson 2024-07-24 17:28:47 +02:00 committed by GitHub
parent 50da3c5c5b
commit a8e60a6c53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 224 additions and 56 deletions

View File

@ -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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.MOTION: 'motion'>,
'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': <ANY>,
'entity_id': 'binary_sensor.presence_sensor',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <NumberMode.AUTO: 'auto'>,
'step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.presence_sensor_delay',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <NumberMode.AUTO: 'auto'>,
'step': 1,
}),
'context': <ANY>,
'entity_id': 'number.presence_sensor_delay',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.MOTION: 'motion'>,
'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': <ANY>,
'entity_id': 'binary_sensor.presence_sensor',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <NumberMode.AUTO: 'auto'>,
'step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.presence_sensor_duration',
'has_entity_name': False,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <NumberMode.AUTO: 'auto'>,
'step': 1,
}),
'context': <ANY>,
'entity_id': 'number.presence_sensor_duration',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '0',
})
# ---

View File

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