From 933ae5198e08f8863cce54176d8f213d1ba8be4c Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Wed, 21 Jun 2023 10:21:36 +0200 Subject: [PATCH] Refactor devolo_home_network tests (#88706) * Refactor devolo_home_network tests * Reduce to snapshot introduction * Simplify * Update snapshots * Simplify further --- .../snapshots/test_binary_sensor.ambr | 45 +++++ .../snapshots/test_device_tracker.ambr | 17 ++ .../snapshots/test_switch.ambr | 173 ++++++++++++++++++ .../devolo_home_network/test_binary_sensor.py | 25 +-- .../devolo_home_network/test_config_flow.py | 2 +- .../test_device_tracker.py | 28 +-- .../devolo_home_network/test_switch.py | 24 +-- 7 files changed, 267 insertions(+), 47 deletions(-) create mode 100644 tests/components/devolo_home_network/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/devolo_home_network/snapshots/test_device_tracker.ambr create mode 100644 tests/components/devolo_home_network/snapshots/test_switch.ambr diff --git a/tests/components/devolo_home_network/snapshots/test_binary_sensor.ambr b/tests/components/devolo_home_network/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..f247f2dc1f0 --- /dev/null +++ b/tests/components/devolo_home_network/snapshots/test_binary_sensor.ambr @@ -0,0 +1,45 @@ +# serializer version: 1 +# name: test_update_attached_to_router + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'plug', + 'friendly_name': 'Mock Title Connected to router', + 'icon': 'mdi:router-network', + }), + 'context': , + 'entity_id': 'binary_sensor.mock_title_connected_to_router', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_update_attached_to_router.1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mock_title_connected_to_router', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': 'mdi:router-network', + 'original_name': 'Connected to router', + 'platform': 'devolo_home_network', + 'supported_features': 0, + 'translation_key': 'connected_to_router', + 'unique_id': '1234567890_connected_to_router', + 'unit_of_measurement': None, + }) +# --- diff --git a/tests/components/devolo_home_network/snapshots/test_device_tracker.ambr b/tests/components/devolo_home_network/snapshots/test_device_tracker.ambr new file mode 100644 index 00000000000..d438aca6a4a --- /dev/null +++ b/tests/components/devolo_home_network/snapshots/test_device_tracker.ambr @@ -0,0 +1,17 @@ +# serializer version: 1 +# name: test_device_tracker + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'band': '5 GHz', + 'icon': 'mdi:lan-connect', + 'mac': 'AA:BB:CC:DD:EE:FF', + 'source_type': , + 'wifi': 'Main', + }), + 'context': , + 'entity_id': 'device_tracker.devolo_home_network_1234567890_aa_bb_cc_dd_ee_ff', + 'last_changed': , + 'last_updated': , + 'state': 'home', + }) +# --- diff --git a/tests/components/devolo_home_network/snapshots/test_switch.ambr b/tests/components/devolo_home_network/snapshots/test_switch.ambr new file mode 100644 index 00000000000..600c9478035 --- /dev/null +++ b/tests/components/devolo_home_network/snapshots/test_switch.ambr @@ -0,0 +1,173 @@ +# serializer version: 1 +# name: test_switches[enable_guest_wifi-async_get_wifi_guest_access-async_set_wifi_guest_access-interval0] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Enable guest Wifi', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_enable_guest_wifi', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switches[enable_guest_wifi-async_get_wifi_guest_access-async_set_wifi_guest_access-interval0].1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.mock_title_enable_guest_wifi', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Enable guest Wifi', + 'platform': 'devolo_home_network', + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1234567890_switch_guest_wifi', + 'unit_of_measurement': None, + }) +# --- +# name: test_switches[enable_leds-async_get_led_setting-async_set_led_setting-interval1] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Enable LEDs', + 'icon': 'mdi:led-off', + }), + 'context': , + 'entity_id': 'switch.mock_title_enable_leds', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switches[enable_leds-async_get_led_setting-async_set_led_setting-interval1].1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_enable_leds', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:led-off', + 'original_name': 'Enable LEDs', + 'platform': 'devolo_home_network', + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1234567890_switch_leds', + 'unit_of_measurement': None, + }) +# --- +# name: test_update_enable_guest_wifi + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Enable guest Wifi', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_enable_guest_wifi', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_update_enable_guest_wifi.1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.mock_title_enable_guest_wifi', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Enable guest Wifi', + 'platform': 'devolo_home_network', + 'supported_features': 0, + 'translation_key': 'switch_guest_wifi', + 'unique_id': '1234567890_switch_guest_wifi', + 'unit_of_measurement': None, + }) +# --- +# name: test_update_enable_leds + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Enable LEDs', + 'icon': 'mdi:led-off', + }), + 'context': , + 'entity_id': 'switch.mock_title_enable_leds', + 'last_changed': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_update_enable_leds.1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_enable_leds', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:led-off', + 'original_name': 'Enable LEDs', + 'platform': 'devolo_home_network', + 'supported_features': 0, + 'translation_key': 'switch_leds', + 'unique_id': '1234567890_switch_leds', + 'unit_of_measurement': None, + }) +# --- diff --git a/tests/components/devolo_home_network/test_binary_sensor.py b/tests/components/devolo_home_network/test_binary_sensor.py index fc9cb232989..7a6395c20f1 100644 --- a/tests/components/devolo_home_network/test_binary_sensor.py +++ b/tests/components/devolo_home_network/test_binary_sensor.py @@ -3,19 +3,14 @@ from unittest.mock import AsyncMock from devolo_plc_api.exceptions.device import DeviceUnavailable import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.binary_sensor import DOMAIN from homeassistant.components.devolo_home_network.const import ( CONNECTED_TO_ROUTER, LONG_UPDATE_INTERVAL, ) -from homeassistant.const import ( - ATTR_FRIENDLY_NAME, - STATE_OFF, - STATE_ON, - STATE_UNAVAILABLE, - EntityCategory, -) +from homeassistant.const import STATE_ON, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.util import dt as dt_util @@ -42,7 +37,10 @@ async def test_binary_sensor_setup(hass: HomeAssistant) -> None: @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_update_attached_to_router( - hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry + hass: HomeAssistant, + mock_device: MockDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, ) -> None: """Test state change of a attached_to_router binary sensor device.""" entry = configure_integration(hass) @@ -52,15 +50,8 @@ async def test_update_attached_to_router( 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 == STATE_OFF - assert state.attributes[ATTR_FRIENDLY_NAME] == f"{entry.title} Connected to router" - - assert ( - entity_registry.async_get(state_key).entity_category - == EntityCategory.DIAGNOSTIC - ) + assert hass.states.get(state_key) == snapshot + assert entity_registry.async_get(state_key) == snapshot # Emulate device failure mock_device.plcnet.async_get_network_overview = AsyncMock( diff --git a/tests/components/devolo_home_network/test_config_flow.py b/tests/components/devolo_home_network/test_config_flow.py index 82aa983522e..91d7d6f39cf 100644 --- a/tests/components/devolo_home_network/test_config_flow.py +++ b/tests/components/devolo_home_network/test_config_flow.py @@ -31,7 +31,7 @@ from .mock import MockDevice from tests.common import MockConfigEntry -async def test_form(hass: HomeAssistant, info: dict[str, Any]): +async def test_form(hass: HomeAssistant, info: dict[str, Any]) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} diff --git a/tests/components/devolo_home_network/test_device_tracker.py b/tests/components/devolo_home_network/test_device_tracker.py index 4ccb16644ce..324f8b44041 100644 --- a/tests/components/devolo_home_network/test_device_tracker.py +++ b/tests/components/devolo_home_network/test_device_tracker.py @@ -2,20 +2,14 @@ from unittest.mock import AsyncMock from devolo_plc_api.exceptions.device import DeviceUnavailable +from syrupy.assertion import SnapshotAssertion from homeassistant.components.device_tracker import DOMAIN as PLATFORM from homeassistant.components.devolo_home_network.const import ( DOMAIN, LONG_UPDATE_INTERVAL, - WIFI_APTYPE, - WIFI_BANDS, -) -from homeassistant.const import ( - STATE_HOME, - STATE_NOT_HOME, - STATE_UNAVAILABLE, - UnitOfFrequency, ) +from homeassistant.const import STATE_NOT_HOME, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.util import dt as dt_util @@ -31,7 +25,10 @@ SERIAL = DISCOVERY_INFO.properties["SN"] async def test_device_tracker( - hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry + hass: HomeAssistant, + mock_device: MockDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, ) -> None: """Test device tracker states.""" state_key = ( @@ -49,14 +46,7 @@ async def test_device_tracker( 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_HOME - assert state.attributes["wifi"] == WIFI_APTYPE[STATION.vap_type] - assert ( - state.attributes["band"] - == f"{WIFI_BANDS[STATION.band]} {UnitOfFrequency.GIGAHERTZ}" - ) + assert hass.states.get(state_key) == snapshot # Emulate state change mock_device.device.async_get_wifi_connected_station = AsyncMock( @@ -84,7 +74,9 @@ async def test_device_tracker( async def test_restoring_clients( - hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry + hass: HomeAssistant, + mock_device: MockDevice, + entity_registry: er.EntityRegistry, ) -> None: """Test restoring existing device_tracker entities.""" state_key = ( diff --git a/tests/components/devolo_home_network/test_switch.py b/tests/components/devolo_home_network/test_switch.py index b2f0e54f971..8b84a0a9344 100644 --- a/tests/components/devolo_home_network/test_switch.py +++ b/tests/components/devolo_home_network/test_switch.py @@ -5,6 +5,7 @@ from unittest.mock import AsyncMock, patch from devolo_plc_api.device_api import WifiGuestAccessGet from devolo_plc_api.exceptions.device import DevicePasswordProtected, DeviceUnavailable import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.devolo_home_network.const import ( DOMAIN, @@ -18,7 +19,6 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNAVAILABLE, - EntityCategory, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -72,7 +72,10 @@ async def test_update_guest_wifi_status_auth_failed( async def test_update_enable_guest_wifi( - hass: HomeAssistant, mock_device: MockDevice + hass: HomeAssistant, + mock_device: MockDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, ) -> None: """Test state change of a enable_guest_wifi switch device.""" entry = configure_integration(hass) @@ -82,9 +85,8 @@ async def test_update_enable_guest_wifi( 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 == STATE_OFF + assert hass.states.get(state_key) == snapshot + assert entity_registry.async_get(state_key) == snapshot # Emulate state change mock_device.device.async_get_wifi_guest_access.return_value = WifiGuestAccessGet( @@ -159,7 +161,10 @@ async def test_update_enable_guest_wifi( async def test_update_enable_leds( - hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry + hass: HomeAssistant, + mock_device: MockDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, ) -> None: """Test state change of a enable_leds switch device.""" entry = configure_integration(hass) @@ -169,11 +174,8 @@ async def test_update_enable_leds( 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 == STATE_OFF - - assert entity_registry.async_get(state_key).entity_category == EntityCategory.CONFIG + assert hass.states.get(state_key) == snapshot + assert entity_registry.async_get(state_key) == snapshot # Emulate state change mock_device.device.async_get_led_setting.return_value = True