Refactor devolo_home_network tests (#88706)

* Refactor devolo_home_network tests

* Reduce to snapshot introduction

* Simplify

* Update snapshots

* Simplify further
This commit is contained in:
Guido Schmitz 2023-06-21 10:21:36 +02:00 committed by GitHub
parent f8bef95eb6
commit 933ae5198e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 267 additions and 47 deletions

View File

@ -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': <ANY>,
'entity_id': 'binary_sensor.mock_title_connected_to_router',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_update_attached_to_router.1
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.mock_title_connected_to_router',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.PLUG: 'plug'>,
'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,
})
# ---

View File

@ -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': <SourceType.ROUTER: 'router'>,
'wifi': 'Main',
}),
'context': <ANY>,
'entity_id': 'device_tracker.devolo_home_network_1234567890_aa_bb_cc_dd_ee_ff',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'home',
})
# ---

View File

@ -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': <ANY>,
'entity_id': 'switch.mock_title_enable_guest_wifi',
'last_changed': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'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': <ANY>,
'entity_id': 'switch.mock_title_enable_leds',
'last_changed': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.mock_title_enable_leds',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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': <ANY>,
'entity_id': 'switch.mock_title_enable_guest_wifi',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_update_enable_guest_wifi.1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'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': <ANY>,
'entity_id': 'switch.mock_title_enable_leds',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_update_enable_leds.1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.mock_title_enable_leds',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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,
})
# ---

View File

@ -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(

View File

@ -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}

View File

@ -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 = (

View File

@ -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