Use snapshot validation on Axis binary sensor (#122483)

* Use snapshot validation on binary sensor

* Use snapshot_platform

* Clean up

* Improve typign
This commit is contained in:
Robert Svensson 2024-07-23 21:30:03 +02:00 committed by GitHub
parent e6ef8a34a7
commit ad38b9e9e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 1370 additions and 97 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,22 +1,22 @@
"""Axis binary sensor platform tests.""" """Axis binary sensor platform tests."""
from typing import Any from unittest.mock import patch
import pytest import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
DOMAIN as BINARY_SENSOR_DOMAIN, from homeassistant.const import Platform
BinarySensorDeviceClass,
)
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .conftest import RtspEventMock from .conftest import ConfigEntryFactoryType, RtspEventMock
from .const import NAME
from tests.common import snapshot_platform
@pytest.mark.parametrize( @pytest.mark.parametrize(
("event", "entity"), "event",
[ [
( (
{ {
@ -25,13 +25,7 @@ from .const import NAME
"source_idx": "1", "source_idx": "1",
"data_type": "DayNight", "data_type": "DayNight",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_daynight_1",
"state": STATE_ON,
"name": f"{NAME} DayNight 1",
"device_class": BinarySensorDeviceClass.LIGHT,
},
), ),
( (
{ {
@ -40,13 +34,7 @@ from .const import NAME
"source_idx": "1", "source_idx": "1",
"data_type": "Sound", "data_type": "Sound",
"data_value": "0", "data_value": "0",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_sound_1",
"state": STATE_OFF,
"name": f"{NAME} Sound 1",
"device_class": BinarySensorDeviceClass.SOUND,
},
), ),
( (
{ {
@ -56,13 +44,7 @@ from .const import NAME
"operation": "Initialized", "operation": "Initialized",
"source_name": "port", "source_name": "port",
"source_idx": "0", "source_idx": "0",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_pir_sensor",
"state": STATE_OFF,
"name": f"{NAME} PIR sensor",
"device_class": BinarySensorDeviceClass.CONNECTIVITY,
},
), ),
( (
{ {
@ -71,78 +53,42 @@ from .const import NAME
"data_value": "0", "data_value": "0",
"source_name": "sensor", "source_name": "sensor",
"source_idx": "0", "source_idx": "0",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_pir_0",
"state": STATE_OFF,
"name": f"{NAME} PIR 0",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
( (
{ {
"topic": "tnsaxis:CameraApplicationPlatform/FenceGuard/Camera1Profile1", "topic": "tnsaxis:CameraApplicationPlatform/FenceGuard/Camera1Profile1",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_fence_guard_profile_1",
"state": STATE_ON,
"name": f"{NAME} Fence Guard Profile 1",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
( (
{ {
"topic": "tnsaxis:CameraApplicationPlatform/MotionGuard/Camera1Profile1", "topic": "tnsaxis:CameraApplicationPlatform/MotionGuard/Camera1Profile1",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_motion_guard_profile_1",
"state": STATE_ON,
"name": f"{NAME} Motion Guard Profile 1",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
( (
{ {
"topic": "tnsaxis:CameraApplicationPlatform/LoiteringGuard/Camera1Profile1", "topic": "tnsaxis:CameraApplicationPlatform/LoiteringGuard/Camera1Profile1",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_loitering_guard_profile_1",
"state": STATE_ON,
"name": f"{NAME} Loitering Guard Profile 1",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
( (
{ {
"topic": "tnsaxis:CameraApplicationPlatform/VMD/Camera1Profile1", "topic": "tnsaxis:CameraApplicationPlatform/VMD/Camera1Profile1",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_vmd4_profile_1",
"state": STATE_ON,
"name": f"{NAME} VMD4 Profile 1",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
( (
{ {
"topic": "tnsaxis:CameraApplicationPlatform/ObjectAnalytics/Device1Scenario1", "topic": "tnsaxis:CameraApplicationPlatform/ObjectAnalytics/Device1Scenario1",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_object_analytics_scenario_1",
"state": STATE_ON,
"name": f"{NAME} Object Analytics Scenario 1",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
# Events with names generated from event ID and topic # Events with names generated from event ID and topic
( (
@ -150,50 +96,35 @@ from .const import NAME
"topic": "tnsaxis:CameraApplicationPlatform/VMD/Camera1Profile9", "topic": "tnsaxis:CameraApplicationPlatform/VMD/Camera1Profile9",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_vmd4_camera1profile9",
"state": STATE_ON,
"name": f"{NAME} VMD4 Camera1Profile9",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
( (
{ {
"topic": "tnsaxis:CameraApplicationPlatform/ObjectAnalytics/Device1Scenario8", "topic": "tnsaxis:CameraApplicationPlatform/ObjectAnalytics/Device1Scenario8",
"data_type": "active", "data_type": "active",
"data_value": "1", "data_value": "1",
}, }
{
"id": f"{BINARY_SENSOR_DOMAIN}.{NAME}_object_analytics_device1scenario8",
"state": STATE_ON,
"name": f"{NAME} Object Analytics Device1Scenario8",
"device_class": BinarySensorDeviceClass.MOTION,
},
), ),
], ],
) )
@pytest.mark.usefixtures("config_entry_setup")
async def test_binary_sensors( async def test_binary_sensors(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
config_entry_factory: ConfigEntryFactoryType,
mock_rtsp_event: RtspEventMock, mock_rtsp_event: RtspEventMock,
event: dict[str, str], event: dict[str, str],
entity: dict[str, Any],
) -> None: ) -> None:
"""Test that sensors are loaded properly.""" """Test that sensors are loaded properly."""
with patch("homeassistant.components.axis.PLATFORMS", [Platform.BINARY_SENSOR]):
config_entry = await config_entry_factory()
mock_rtsp_event(**event) mock_rtsp_event(**event)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1 assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
state = hass.states.get(entity["id"])
assert state.state == entity["state"]
assert state.name == entity["name"]
assert state.attributes["device_class"] == entity["device_class"]
@pytest.mark.parametrize( @pytest.mark.parametrize(
("event"), "event",
[ [
# Event with unsupported topic # Event with unsupported topic
{ {
@ -233,5 +164,4 @@ async def test_unsupported_events(
) -> None: ) -> None:
"""Validate nothing breaks with unsupported events.""" """Validate nothing breaks with unsupported events."""
mock_rtsp_event(**event) mock_rtsp_event(**event)
await hass.async_block_till_done()
assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 0 assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 0