Use snapshot in deCONZ binary sensor tests (#122507)

* Use snapshot in deCONZ binary sensor tests

* Fix typing in button test
This commit is contained in:
Robert Svensson 2024-07-24 09:01:34 +02:00 committed by GitHub
parent 4c7828fd50
commit a90d41d9e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 1031 additions and 166 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,11 @@
from collections.abc import Callable
from typing import Any
from unittest.mock import patch
import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.deconz.const import (
CONF_ALLOW_CLIP_SENSOR,
CONF_ALLOW_NEW_DEVICES,
@ -14,17 +15,13 @@ from homeassistant.components.deconz.const import (
)
from homeassistant.components.deconz.services import SERVICE_DEVICE_REFRESH
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_DEVICE_CLASS,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
EntityCategory,
)
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
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
TEST_DATA = [
( # Alarm binary sensor
@ -51,19 +48,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:02:b5:d1:80-01-0500",
},
{
"entity_count": 3,
"device_count": 3,
"entity_id": "binary_sensor.alarm_10",
"unique_id": "00:15:8d:00:02:b5:d1:80-01-0500-alarm",
"state": STATE_OFF,
"entity_category": None,
"device_class": BinarySensorDeviceClass.SAFETY,
"attributes": {
"on": True,
"temperature": 26.0,
"device_class": "safety",
"friendly_name": "Alarm 10",
},
"websocket_event": {"alarm": True},
"next_state": STATE_ON,
},
@ -92,18 +77,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:02:a5:21:24-01-0101",
},
{
"entity_count": 4,
"device_count": 3,
"entity_id": "binary_sensor.cave_co",
"unique_id": "00:15:8d:00:02:a5:21:24-01-0101-carbon_monoxide",
"state": STATE_OFF,
"entity_category": None,
"device_class": BinarySensorDeviceClass.CO,
"attributes": {
"on": True,
"device_class": "carbon_monoxide",
"friendly_name": "Cave CO",
},
"websocket_event": {"carbonmonoxide": True},
"next_state": STATE_ON,
},
@ -127,18 +101,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:01:d9:3e:7c-01-0500",
},
{
"entity_count": 2,
"device_count": 3,
"entity_id": "binary_sensor.sensor_kitchen_smoke",
"unique_id": "00:15:8d:00:01:d9:3e:7c-01-0500-fire",
"state": STATE_OFF,
"entity_category": None,
"device_class": BinarySensorDeviceClass.SMOKE,
"attributes": {
"on": True,
"device_class": "smoke",
"friendly_name": "sensor_kitchen_smoke",
},
"websocket_event": {"fire": True},
"next_state": STATE_ON,
},
@ -163,17 +126,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:01:d9:3e:7c-01-0500",
},
{
"entity_count": 2,
"device_count": 3,
"entity_id": "binary_sensor.sensor_kitchen_smoke_test_mode",
"unique_id": "00:15:8d:00:01:d9:3e:7c-01-0500-in_test_mode",
"state": STATE_OFF,
"entity_category": EntityCategory.DIAGNOSTIC,
"device_class": BinarySensorDeviceClass.SMOKE,
"attributes": {
"device_class": "smoke",
"friendly_name": "sensor_kitchen_smoke Test Mode",
},
"websocket_event": {"test": True},
"next_state": STATE_ON,
},
@ -195,17 +148,7 @@ TEST_DATA = [
"uniqueid": "kitchen-switch",
},
{
"entity_count": 1,
"device_count": 2,
"entity_id": "binary_sensor.kitchen_switch",
"unique_id": "kitchen-switch-flag",
"state": STATE_ON,
"entity_category": None,
"device_class": None,
"attributes": {
"on": True,
"friendly_name": "Kitchen Switch",
},
"websocket_event": {"flag": False},
"next_state": STATE_OFF,
},
@ -232,19 +175,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:02:2b:96:b4-01-0006",
},
{
"entity_count": 3,
"device_count": 3,
"entity_id": "binary_sensor.back_door",
"unique_id": "00:15:8d:00:02:2b:96:b4-01-0006-open",
"state": STATE_OFF,
"entity_category": None,
"device_class": BinarySensorDeviceClass.OPENING,
"attributes": {
"on": True,
"temperature": 33.0,
"device_class": "opening",
"friendly_name": "Back Door",
},
"websocket_event": {"open": True},
"next_state": STATE_ON,
},
@ -278,19 +209,7 @@ TEST_DATA = [
"uniqueid": "00:17:88:01:03:28:8c:9b-02-0406",
},
{
"entity_count": 3,
"device_count": 3,
"entity_id": "binary_sensor.motion_sensor_4",
"unique_id": "00:17:88:01:03:28:8c:9b-02-0406-presence",
"state": STATE_OFF,
"entity_category": None,
"device_class": BinarySensorDeviceClass.MOTION,
"attributes": {
"on": True,
"dark": False,
"device_class": "motion",
"friendly_name": "Motion sensor 4",
},
"websocket_event": {"presence": True},
"next_state": STATE_ON,
},
@ -319,19 +238,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:02:2f:07:db-01-0500",
},
{
"entity_count": 5,
"device_count": 3,
"entity_id": "binary_sensor.water2",
"unique_id": "00:15:8d:00:02:2f:07:db-01-0500-water",
"state": STATE_OFF,
"entity_category": None,
"device_class": BinarySensorDeviceClass.MOISTURE,
"attributes": {
"on": True,
"temperature": 25.0,
"device_class": "moisture",
"friendly_name": "water2",
},
"websocket_event": {"water": True},
"next_state": STATE_ON,
},
@ -364,22 +271,7 @@ TEST_DATA = [
"uniqueid": "00:15:8d:00:02:a5:21:24-01-0101",
},
{
"entity_count": 3,
"device_count": 3,
"entity_id": "binary_sensor.vibration_1",
"unique_id": "00:15:8d:00:02:a5:21:24-01-0101-vibration",
"state": STATE_ON,
"entity_category": None,
"device_class": BinarySensorDeviceClass.VIBRATION,
"attributes": {
"on": True,
"temperature": 32.0,
"orientation": [10, 1059, 0],
"tiltangle": 83,
"vibrationstrength": 114,
"device_class": "vibration",
"friendly_name": "Vibration 1",
},
"websocket_event": {"vibration": False},
"next_state": STATE_OFF,
},
@ -402,17 +294,7 @@ TEST_DATA = [
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
{
"entity_count": 4,
"device_count": 3,
"entity_id": "binary_sensor.presence_sensor_tampered",
"unique_id": "00:00:00:00:00:00:00:00-00-tampered",
"state": STATE_OFF,
"entity_category": EntityCategory.DIAGNOSTIC,
"device_class": BinarySensorDeviceClass.TAMPER,
"attributes": {
"device_class": "tamper",
"friendly_name": "Presence sensor Tampered",
},
"websocket_event": {"tampered": True},
"next_state": STATE_ON,
},
@ -435,17 +317,7 @@ TEST_DATA = [
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
{
"entity_count": 4,
"device_count": 3,
"entity_id": "binary_sensor.presence_sensor_low_battery",
"unique_id": "00:00:00:00:00:00:00:00-00-low_battery",
"state": STATE_OFF,
"entity_category": EntityCategory.DIAGNOSTIC,
"device_class": BinarySensorDeviceClass.BATTERY,
"attributes": {
"device_class": "battery",
"friendly_name": "Presence sensor Low Battery",
},
"websocket_event": {"lowbattery": True},
"next_state": STATE_ON,
},
@ -457,38 +329,16 @@ TEST_DATA = [
@pytest.mark.parametrize(("sensor_payload", "expected"), TEST_DATA)
async def test_binary_sensors(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
config_entry_setup: ConfigEntry,
config_entry_factory: ConfigEntryFactoryType,
sensor_ws_data: WebsocketDataType,
expected: dict[str, Any],
snapshot: SnapshotAssertion,
) -> None:
"""Test successful creation of binary sensor entities."""
assert len(hass.states.async_all()) == expected["entity_count"]
# Verify state data
sensor = hass.states.get(expected["entity_id"])
assert sensor.state == expected["state"]
assert sensor.attributes.get(ATTR_DEVICE_CLASS) == expected["device_class"]
assert sensor.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.BINARY_SENSOR]):
config_entry = await config_entry_factory()
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
# Change state
@ -497,12 +347,12 @@ async def test_binary_sensors(
# 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

View File

@ -8,11 +8,12 @@ import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .conftest import ConfigEntryFactoryType
from tests.common import snapshot_platform
from tests.test_util.aiohttp import AiohttpClientMocker
@ -79,7 +80,7 @@ async def test_button(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
config_entry_factory: ConfigEntry,
config_entry_factory: ConfigEntryFactoryType,
mock_put_request: Callable[[str, str], AiohttpClientMocker],
expected: dict[str, Any],
snapshot: SnapshotAssertion,