Use SnapshotAssertion in SFR binary sensor tests (#89624)

pull/89633/head
epenet 2023-03-13 13:37:51 +01:00 committed by GitHub
parent 429e52cf3d
commit f3da95fb1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 18 deletions

View File

@ -1,5 +1,4 @@
"""Constants for SFR Box tests."""
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.button import ButtonDeviceClass
from homeassistant.components.sensor import ATTR_OPTIONS, ATTR_STATE_CLASS
from homeassistant.components.sfr_box.const import DOMAIN
@ -12,7 +11,6 @@ from homeassistant.const import (
ATTR_STATE,
ATTR_SW_VERSION,
ATTR_UNIT_OF_MEASUREMENT,
STATE_ON,
STATE_UNKNOWN,
Platform,
)
@ -33,14 +31,6 @@ EXPECTED_ENTITIES = {
ATTR_NAME: "SFR Box",
ATTR_SW_VERSION: "NB6VAC-MAIN-R4.0.44k",
},
Platform.BINARY_SENSOR: [
{
ATTR_DEVICE_CLASS: BinarySensorDeviceClass.CONNECTIVITY,
ATTR_ENTITY_ID: "binary_sensor.sfr_box_dsl_status",
ATTR_STATE: STATE_ON,
ATTR_UNIQUE_ID: "e4:5d:51:00:11:22_dsl_status",
},
],
Platform.BUTTON: [
{
ATTR_DEVICE_CLASS: ButtonDeviceClass.RESTART,

View File

@ -0,0 +1,75 @@
# serializer version: 1
# name: test_binary_sensors
list([
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': 'http://192.168.0.1',
'connections': set({
}),
'disabled_by': None,
'entry_type': None,
'hw_version': None,
'id': <ANY>,
'identifiers': set({
tuple(
'sfr_box',
'e4:5d:51:00:11:22',
),
}),
'is_new': False,
'manufacturer': None,
'model': 'NB6VAC-FXC-r0',
'name': 'SFR Box',
'name_by_user': None,
'suggested_area': None,
'sw_version': 'NB6VAC-MAIN-R4.0.44k',
'via_device_id': None,
}),
])
# ---
# name: test_binary_sensors.1
list([
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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.sfr_box_dsl_status',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.CONNECTIVITY: 'connectivity'>,
'original_icon': None,
'original_name': 'DSL status',
'platform': 'sfr_box',
'supported_features': 0,
'translation_key': None,
'unique_id': 'e4:5d:51:00:11:22_dsl_status',
'unit_of_measurement': None,
}),
])
# ---
# name: test_binary_sensors[binary_sensor.sfr_box_dsl_status]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'connectivity',
'friendly_name': 'SFR Box DSL status',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.sfr_box_dsl_status',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

@ -1,17 +1,15 @@
"""Test the SFR Box sensors."""
"""Test the SFR Box binary sensors."""
from collections.abc import Generator
from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import check_device_registry, check_entities
from .const import EXPECTED_ENTITIES
pytestmark = pytest.mark.usefixtures("system_get_info", "dsl_get_info")
@ -27,14 +25,21 @@ async def test_binary_sensors(
config_entry: ConfigEntry,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test for SFR Box binary sensors."""
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
check_device_registry(device_registry, EXPECTED_ENTITIES["expected_device"])
device_entries = dr.async_entries_for_config_entry(
device_registry, config_entry.entry_id
)
assert device_entries == snapshot
expected_entities = EXPECTED_ENTITIES[Platform.BINARY_SENSOR]
assert len(entity_registry.entities) == len(expected_entities)
entity_entries = er.async_entries_for_config_entry(
entity_registry, config_entry.entry_id
)
assert entity_entries == snapshot
check_entities(hass, entity_registry, expected_entities)
for entity in entity_entries:
assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id)