diff --git a/tests/components/devolo_home_network/snapshots/test_sensor.ambr b/tests/components/devolo_home_network/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..241313965c4 --- /dev/null +++ b/tests/components/devolo_home_network/snapshots/test_sensor.ambr @@ -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': , + 'entity_id': 'sensor.mock_title_connected_plc_devices', + 'last_changed': , + 'last_updated': , + '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': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_connected_plc_devices', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_connected_wifi_clients', + 'last_changed': , + 'last_updated': , + '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': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + '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': , + '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': , + 'entity_id': 'sensor.mock_title_neighboring_wifi_networks', + 'last_changed': , + 'last_updated': , + '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': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_neighboring_wifi_networks', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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, + }) +# --- diff --git a/tests/components/devolo_home_network/test_sensor.py b/tests/components/devolo_home_network/test_sensor.py index 0511544224a..dc7842e5fbd 100644 --- a/tests/components/devolo_home_network/test_sensor.py +++ b/tests/components/devolo_home_network/test_sensor.py @@ -1,15 +1,17 @@ """Tests for the devolo Home Network sensors.""" +from datetime import timedelta from unittest.mock import AsyncMock from devolo_plc_api.exceptions.device import DeviceUnavailable import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.devolo_home_network.const import ( LONG_UPDATE_INTERVAL, SHORT_UPDATE_INTERVAL, ) -from homeassistant.components.sensor import DOMAIN, SensorStateClass -from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_UNAVAILABLE, EntityCategory +from homeassistant.components.sensor import DOMAIN +from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er 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) -async def test_update_connected_wifi_clients( - hass: HomeAssistant, mock_device: MockDevice -) -> None: - """Test state change of a connected_wifi_clients sensor device.""" - entry = configure_integration(hass) - device_name = entry.title.replace(" ", "_").lower() - state_key = f"{DOMAIN}.{device_name}_connected_wifi_clients" - 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 Wifi clients" - ) - assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT - - # 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.parametrize( + ("name", "get_method", "interval"), + [ + [ + "connected_wifi_clients", + "async_get_wifi_connected_station", + SHORT_UPDATE_INTERVAL, + ], + [ + "neighboring_wifi_networks", + "async_get_wifi_neighbor_access_points", + LONG_UPDATE_INTERVAL, + ], + [ + "connected_plc_devices", + "async_get_network_overview", + LONG_UPDATE_INTERVAL, + ], + ], +) @pytest.mark.usefixtures("entity_registry_enabled_by_default") -async def test_update_neighboring_wifi_networks( - hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry +async def test_sensor( + hass: HomeAssistant, + mock_device: MockDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, + name: str, + get_method: str, + interval: timedelta, ) -> None: - """Test state change of a neighboring_wifi_networks sensor device.""" + """Test state change of a sensor device.""" entry = configure_integration(hass) 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.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} Neighboring Wifi networks" - ) - assert ( - entity_registry.async_get(state_key).entity_category - is EntityCategory.DIAGNOSTIC - ) + assert hass.states.get(state_key) == snapshot + assert entity_registry.async_get(state_key) == snapshot # Emulate device failure - mock_device.device.async_get_wifi_neighbor_access_points = AsyncMock( - side_effect=DeviceUnavailable - ) - async_fire_time_changed(hass, dt_util.utcnow() + LONG_UPDATE_INTERVAL) + setattr(mock_device.device, get_method, AsyncMock(side_effect=DeviceUnavailable)) + setattr(mock_device.plcnet, get_method, AsyncMock(side_effect=DeviceUnavailable)) + async_fire_time_changed(hass, dt_util.utcnow() + interval) await hass.async_block_till_done() state = hass.states.get(state_key) @@ -112,52 +89,7 @@ async def test_update_neighboring_wifi_networks( # Emulate state change mock_device.reset() - 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 == "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) + async_fire_time_changed(hass, dt_util.utcnow() + interval) await hass.async_block_till_done() state = hass.states.get(state_key)