Add snapshot tests to Totalconnect (#115952)

* Add snapshot tests to Totalconnect

* Add snapshot tests to Totalconnect
This commit is contained in:
Joost Lekkerkerker 2024-04-22 13:01:31 +02:00 committed by GitHub
parent 6985d36f18
commit 693bd08a0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1229 additions and 40 deletions

View File

@ -0,0 +1,117 @@
# serializer version: 1
# name: test_attributes[alarm_control_panel.test-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'alarm_control_panel',
'entity_category': None,
'entity_id': 'alarm_control_panel.test',
'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': 'test',
'platform': 'totalconnect',
'previous_unique_id': None,
'supported_features': <AlarmControlPanelEntityFeature: 7>,
'translation_key': None,
'unique_id': '123456',
'unit_of_measurement': None,
})
# ---
# name: test_attributes[alarm_control_panel.test-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'ac_loss': False,
'changed_by': None,
'code_arm_required': True,
'code_format': None,
'cover_tampered': False,
'friendly_name': 'test',
'location_id': '123456',
'location_name': 'test',
'low_battery': False,
'partition': 1,
'supported_features': <AlarmControlPanelEntityFeature: 7>,
'triggered_source': None,
'triggered_zone': None,
}),
'context': <ANY>,
'entity_id': 'alarm_control_panel.test',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'disarmed',
})
# ---
# name: test_attributes[alarm_control_panel.test_partition_2-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'alarm_control_panel',
'entity_category': None,
'entity_id': 'alarm_control_panel.test_partition_2',
'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': 'test partition 2',
'platform': 'totalconnect',
'previous_unique_id': None,
'supported_features': <AlarmControlPanelEntityFeature: 7>,
'translation_key': None,
'unique_id': '123456_2',
'unit_of_measurement': None,
})
# ---
# name: test_attributes[alarm_control_panel.test_partition_2-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'ac_loss': False,
'changed_by': None,
'code_arm_required': True,
'code_format': None,
'cover_tampered': False,
'friendly_name': 'test partition 2',
'location_id': '123456',
'location_name': 'test partition 2',
'low_battery': False,
'partition': 2,
'supported_features': <AlarmControlPanelEntityFeature: 7>,
'triggered_source': None,
'triggered_zone': None,
}),
'context': <ANY>,
'entity_id': 'alarm_control_panel.test_partition_2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'disarmed',
})
# ---

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ from datetime import timedelta
from unittest.mock import patch
import pytest
from syrupy import SnapshotAssertion
from total_connect_client.exceptions import ServiceUnavailable, TotalConnectError
from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN
@ -14,7 +15,6 @@ from homeassistant.components.totalconnect.alarm_control_panel import (
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
SERVICE_ALARM_ARM_AWAY,
SERVICE_ALARM_ARM_HOME,
SERVICE_ALARM_ARM_NIGHT,
@ -36,7 +36,6 @@ from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.util import dt as dt_util
from .common import (
LOCATION_ID,
RESPONSE_ARM_FAILURE,
RESPONSE_ARM_SUCCESS,
RESPONSE_ARMED_AWAY,
@ -58,7 +57,7 @@ from .common import (
setup_platform,
)
from tests.common import async_fire_time_changed
from tests.common import async_fire_time_changed, snapshot_platform
ENTITY_ID = "alarm_control_panel.test"
ENTITY_ID_2 = "alarm_control_panel.test_partition_2"
@ -67,28 +66,20 @@ DATA = {ATTR_ENTITY_ID: ENTITY_ID}
DELAY = timedelta(seconds=10)
async def test_attributes(hass: HomeAssistant) -> None:
async def test_attributes(
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
) -> None:
"""Test the alarm control panel attributes are correct."""
await setup_platform(hass, ALARM_DOMAIN)
entry = await setup_platform(hass, ALARM_DOMAIN)
with patch(
"homeassistant.components.totalconnect.TotalConnectClient.request",
return_value=RESPONSE_DISARMED,
) as mock_request:
await async_update_entity(hass, ENTITY_ID)
await hass.async_block_till_done()
state = hass.states.get(ENTITY_ID)
assert state.state == STATE_ALARM_DISARMED
mock_request.assert_called_once()
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "test"
entity_registry = er.async_get(hass)
entry = entity_registry.async_get(ENTITY_ID)
# TotalConnect partition #1 alarm device unique_id is the location_id
assert entry.unique_id == LOCATION_ID
entry2 = entity_registry.async_get(ENTITY_ID_2)
# TotalConnect partition #2 unique_id is the location_id + "_{partition_number}"
assert entry2.unique_id == LOCATION_ID + "_2"
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
assert mock_request.call_count == 1

View File

@ -2,6 +2,8 @@
from unittest.mock import patch
from syrupy import SnapshotAssertion
from homeassistant.components.binary_sensor import (
DOMAIN as BINARY_SENSOR,
BinarySensorDeviceClass,
@ -10,7 +12,9 @@ from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .common import LOCATION_ID, RESPONSE_DISARMED, ZONE_NORMAL, setup_platform
from .common import RESPONSE_DISARMED, ZONE_NORMAL, setup_platform
from tests.common import snapshot_platform
ZONE_ENTITY_ID = "binary_sensor.security"
ZONE_LOW_BATTERY_ID = "binary_sensor.security_low_battery"
@ -20,31 +24,13 @@ PANEL_TAMPER_ID = "binary_sensor.test_tamper"
PANEL_POWER_ID = "binary_sensor.test_power"
async def test_entity_registry(hass: HomeAssistant) -> None:
async def test_entity_registry(
hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion
) -> None:
"""Test the binary sensor is registered in entity registry."""
await setup_platform(hass, BINARY_SENSOR)
entity_registry = er.async_get(hass)
entry = await setup_platform(hass, BINARY_SENSOR)
# ensure zone 1 plus two diagnostic zones are created
entry = entity_registry.async_get(ZONE_ENTITY_ID)
entry_low_battery = entity_registry.async_get(ZONE_LOW_BATTERY_ID)
entry_tamper = entity_registry.async_get(ZONE_TAMPER_ID)
assert entry.unique_id == f"{LOCATION_ID}_{ZONE_NORMAL['ZoneID']}_zone"
assert (
entry_low_battery.unique_id
== f"{LOCATION_ID}_{ZONE_NORMAL['ZoneID']}_low_battery"
)
assert entry_tamper.unique_id == f"{LOCATION_ID}_{ZONE_NORMAL['ZoneID']}_tamper"
# ensure panel diagnostic zones are created
panel_battery = entity_registry.async_get(PANEL_BATTERY_ID)
panel_tamper = entity_registry.async_get(PANEL_TAMPER_ID)
panel_power = entity_registry.async_get(PANEL_POWER_ID)
assert panel_battery.unique_id == f"{LOCATION_ID}_low_battery"
assert panel_tamper.unique_id == f"{LOCATION_ID}_tamper"
assert panel_power.unique_id == f"{LOCATION_ID}_power"
await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)
async def test_state_and_attributes(hass: HomeAssistant) -> None: