Use snapshots in devolo Home Network sensor tests (#95104)

Use snapshots

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Guido Schmitz 2023-07-10 15:49:08 +02:00 committed by GitHub
parent 5f6ddedd67
commit a3681774d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 173 additions and 108 deletions

View file

@ -0,0 +1,133 @@
# serializer version: 1
# name: test_sensor[connected_plc_devices-async_get_network_overview-interval2]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Connected PLC devices',
'icon': 'mdi:lan',
}),
'context': <ANY>,
'entity_id': 'sensor.mock_title_connected_plc_devices',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '1',
})
# ---
# name: test_sensor[connected_plc_devices-async_get_network_overview-interval2].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.mock_title_connected_plc_devices',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:lan',
'original_name': 'Connected PLC devices',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'connected_plc_devices',
'unique_id': '1234567890_connected_plc_devices',
'unit_of_measurement': None,
})
# ---
# name: test_sensor[connected_wifi_clients-async_get_wifi_connected_station-interval0]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Connected Wifi clients',
'icon': 'mdi:wifi',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'context': <ANY>,
'entity_id': 'sensor.mock_title_connected_wifi_clients',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '1',
})
# ---
# name: test_sensor[connected_wifi_clients-async_get_wifi_connected_station-interval0].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.mock_title_connected_wifi_clients',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:wifi',
'original_name': 'Connected Wifi clients',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'connected_wifi_clients',
'unique_id': '1234567890_connected_wifi_clients',
'unit_of_measurement': None,
})
# ---
# name: test_sensor[neighboring_wifi_networks-async_get_wifi_neighbor_access_points-interval1]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Neighboring Wifi networks',
'icon': 'mdi:wifi-marker',
}),
'context': <ANY>,
'entity_id': 'sensor.mock_title_neighboring_wifi_networks',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '1',
})
# ---
# name: test_sensor[neighboring_wifi_networks-async_get_wifi_neighbor_access_points-interval1].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'sensor.mock_title_neighboring_wifi_networks',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:wifi-marker',
'original_name': 'Neighboring Wifi networks',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'neighboring_wifi_networks',
'unique_id': '1234567890_neighboring_wifi_networks',
'unit_of_measurement': None,
})
# ---

View file

@ -1,15 +1,17 @@
"""Tests for the devolo Home Network sensors.""" """Tests for the devolo Home Network sensors."""
from datetime import timedelta
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
from devolo_plc_api.exceptions.device import DeviceUnavailable from devolo_plc_api.exceptions.device import DeviceUnavailable
import pytest import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.devolo_home_network.const import ( from homeassistant.components.devolo_home_network.const import (
LONG_UPDATE_INTERVAL, LONG_UPDATE_INTERVAL,
SHORT_UPDATE_INTERVAL, SHORT_UPDATE_INTERVAL,
) )
from homeassistant.components.sensor import DOMAIN, SensorStateClass from homeassistant.components.sensor import DOMAIN
from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_UNAVAILABLE, EntityCategory from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
@ -35,75 +37,50 @@ async def test_sensor_setup(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
async def test_update_connected_wifi_clients( @pytest.mark.parametrize(
hass: HomeAssistant, mock_device: MockDevice ("name", "get_method", "interval"),
) -> None: [
"""Test state change of a connected_wifi_clients sensor device.""" [
entry = configure_integration(hass) "connected_wifi_clients",
device_name = entry.title.replace(" ", "_").lower() "async_get_wifi_connected_station",
state_key = f"{DOMAIN}.{device_name}_connected_wifi_clients" SHORT_UPDATE_INTERVAL,
await hass.config_entries.async_setup(entry.entry_id) ],
await hass.async_block_till_done() [
"neighboring_wifi_networks",
state = hass.states.get(state_key) "async_get_wifi_neighbor_access_points",
assert state is not None LONG_UPDATE_INTERVAL,
assert state.state == "1" ],
assert ( [
state.attributes[ATTR_FRIENDLY_NAME] == f"{entry.title} Connected Wifi clients" "connected_plc_devices",
) "async_get_network_overview",
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT LONG_UPDATE_INTERVAL,
],
# Emulate device failure ],
mock_device.device.async_get_wifi_connected_station = AsyncMock( )
side_effect=DeviceUnavailable
)
async_fire_time_changed(hass, dt_util.utcnow() + SHORT_UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_UNAVAILABLE
# Emulate state change
mock_device.reset()
async_fire_time_changed(hass, dt_util.utcnow() + SHORT_UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == "1"
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_update_neighboring_wifi_networks( async def test_sensor(
hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry hass: HomeAssistant,
mock_device: MockDevice,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
name: str,
get_method: str,
interval: timedelta,
) -> None: ) -> None:
"""Test state change of a neighboring_wifi_networks sensor device.""" """Test state change of a sensor device."""
entry = configure_integration(hass) entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower() device_name = entry.title.replace(" ", "_").lower()
state_key = f"{DOMAIN}.{device_name}_neighboring_wifi_networks" state_key = f"{DOMAIN}.{device_name}_{name}"
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(state_key) assert hass.states.get(state_key) == snapshot
assert state is not None assert entity_registry.async_get(state_key) == snapshot
assert state.state == "1"
assert (
state.attributes[ATTR_FRIENDLY_NAME]
== f"{entry.title} Neighboring Wifi networks"
)
assert (
entity_registry.async_get(state_key).entity_category
is EntityCategory.DIAGNOSTIC
)
# Emulate device failure # Emulate device failure
mock_device.device.async_get_wifi_neighbor_access_points = AsyncMock( setattr(mock_device.device, get_method, AsyncMock(side_effect=DeviceUnavailable))
side_effect=DeviceUnavailable setattr(mock_device.plcnet, get_method, AsyncMock(side_effect=DeviceUnavailable))
) async_fire_time_changed(hass, dt_util.utcnow() + interval)
async_fire_time_changed(hass, dt_util.utcnow() + LONG_UPDATE_INTERVAL)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(state_key) state = hass.states.get(state_key)
@ -112,52 +89,7 @@ async def test_update_neighboring_wifi_networks(
# Emulate state change # Emulate state change
mock_device.reset() mock_device.reset()
async_fire_time_changed(hass, dt_util.utcnow() + LONG_UPDATE_INTERVAL) async_fire_time_changed(hass, dt_util.utcnow() + interval)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == "1"
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_update_connected_plc_devices(
hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry
) -> None:
"""Test state change of a connected_plc_devices sensor device."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{DOMAIN}.{device_name}_connected_plc_devices"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == "1"
assert (
state.attributes[ATTR_FRIENDLY_NAME] == f"{entry.title} Connected PLC devices"
)
assert (
entity_registry.async_get(state_key).entity_category
is EntityCategory.DIAGNOSTIC
)
# Emulate device failure
mock_device.plcnet.async_get_network_overview = AsyncMock(
side_effect=DeviceUnavailable
)
async_fire_time_changed(hass, dt_util.utcnow() + LONG_UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_UNAVAILABLE
# Emulate state change
mock_device.reset()
async_fire_time_changed(hass, dt_util.utcnow() + LONG_UPDATE_INTERVAL)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(state_key) state = hass.states.get(state_key)