Rework platform setup tests for devolo Home Network (#143114)

* Rework platform setup tests for devolo Home Network

* Fix sensor test

* Remove unload
This commit is contained in:
Guido Schmitz 2025-05-12 23:21:14 +02:00 committed by GitHub
parent 026687299d
commit e58750555e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 86 additions and 112 deletions

View File

@ -7,11 +7,9 @@ from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.devolo_home_network.const import (
CONNECTED_TO_ROUTER,
LONG_UPDATE_INTERVAL,
)
from homeassistant.components.binary_sensor import DOMAIN as PLATFORM
from homeassistant.components.devolo_home_network.const import LONG_UPDATE_INTERVAL
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -24,19 +22,20 @@ from tests.common import async_fire_time_changed
@pytest.mark.usefixtures("mock_device")
async def test_binary_sensor_setup(hass: HomeAssistant) -> None:
async def test_binary_sensor_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test default setup of the binary sensor component."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
assert (
hass.states.get(f"{BINARY_SENSOR_DOMAIN}.{device_name}_{CONNECTED_TO_ROUTER}")
is None
)
await hass.config_entries.async_unload(entry.entry_id)
assert entity_registry.async_get(
f"{PLATFORM}.{device_name}_connected_to_router"
).disabled
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@ -50,7 +49,7 @@ async def test_update_attached_to_router(
"""Test state change of a attached_to_router binary sensor device."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{BINARY_SENSOR_DOMAIN}.{device_name}_{CONNECTED_TO_ROUTER}"
state_key = f"{PLATFORM}.{device_name}_connected_to_router"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
@ -81,5 +80,3 @@ async def test_update_attached_to_router(
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_ON
await hass.config_entries.async_unload(entry.entry_id)

View File

@ -8,7 +8,7 @@ from syrupy.assertion import SnapshotAssertion
from homeassistant.components.button import DOMAIN as PLATFORM, SERVICE_PRESS
from homeassistant.components.devolo_home_network.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -19,22 +19,27 @@ from .mock import MockDevice
@pytest.mark.usefixtures("mock_device")
async def test_button_setup(hass: HomeAssistant) -> None:
async def test_button_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test default setup of the button component."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
assert (
hass.states.get(f"{PLATFORM}.{device_name}_identify_device_with_a_blinking_led")
is not None
)
assert hass.states.get(f"{PLATFORM}.{device_name}_start_plc_pairing") is not None
assert hass.states.get(f"{PLATFORM}.{device_name}_restart_device") is not None
assert hass.states.get(f"{PLATFORM}.{device_name}_start_wps") is not None
await hass.config_entries.async_unload(entry.entry_id)
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_identify_device_with_a_blinking_led"
).disabled
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_start_plc_pairing"
).disabled
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_restart_device"
).disabled
assert not entity_registry.async_get(f"{PLATFORM}.{device_name}_start_wps").disabled
@pytest.mark.parametrize(
@ -107,8 +112,6 @@ async def test_button(
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)
async def test_auth_failed(hass: HomeAssistant, mock_device: MockDevice) -> None:
"""Test setting unautherized triggers the reauth flow."""
@ -139,5 +142,3 @@ async def test_auth_failed(hass: HomeAssistant, mock_device: MockDevice) -> None
assert "context" in flow
assert flow["context"]["source"] == SOURCE_REAUTH
assert flow["context"]["entry_id"] == entry.entry_id
await hass.config_entries.async_unload(entry.entry_id)

View File

@ -70,8 +70,6 @@ async def test_device_tracker(
assert state is not None
assert state.state == STATE_UNAVAILABLE
await hass.config_entries.async_unload(entry.entry_id)
async def test_restoring_clients(
hass: HomeAssistant,

View File

@ -9,7 +9,8 @@ import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.devolo_home_network.const import SHORT_UPDATE_INTERVAL
from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN
from homeassistant.components.image import DOMAIN as PLATFORM
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -24,21 +25,20 @@ from tests.typing import ClientSessionGenerator
@pytest.mark.usefixtures("mock_device")
async def test_image_setup(hass: HomeAssistant) -> None:
async def test_image_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test default setup of the image component."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
assert (
hass.states.get(
f"{IMAGE_DOMAIN}.{device_name}_guest_wi_fi_credentials_as_qr_code"
)
is not None
)
await hass.config_entries.async_unload(entry.entry_id)
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_guest_wi_fi_credentials_as_qr_code"
).disabled
@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00")
@ -53,7 +53,7 @@ async def test_guest_wifi_qr(
"""Test showing a QR code of the guest wifi credentials."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{IMAGE_DOMAIN}.{device_name}_guest_wi_fi_credentials_as_qr_code"
state_key = f"{PLATFORM}.{device_name}_guest_wi_fi_credentials_as_qr_code"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
@ -95,5 +95,3 @@ async def test_guest_wifi_qr(
resp = await client.get(f"/api/image_proxy/{state_key}")
assert resp.status == HTTPStatus.OK
assert await resp.read() != body
await hass.config_entries.async_unload(entry.entry_id)

View File

@ -27,49 +27,41 @@ from tests.common import async_fire_time_changed
@pytest.mark.usefixtures("mock_device")
async def test_sensor_setup(hass: HomeAssistant) -> None:
async def test_sensor_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test default setup of the sensor component."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
assert (
hass.states.get(f"{PLATFORM}.{device_name}_connected_wi_fi_clients") is not None
)
assert hass.states.get(f"{PLATFORM}.{device_name}_connected_plc_devices") is None
assert (
hass.states.get(f"{PLATFORM}.{device_name}_neighboring_wi_fi_networks") is None
)
assert (
hass.states.get(
f"{PLATFORM}.{device_name}_plc_downlink_phy_rate_{PLCNET.devices[1].user_device_name}"
)
is not None
)
assert (
hass.states.get(
f"{PLATFORM}.{device_name}_plc_uplink_phy_rate_{PLCNET.devices[1].user_device_name}"
)
is not None
)
assert (
hass.states.get(
f"{PLATFORM}.{device_name}_plc_downlink_phyrate_{PLCNET.devices[2].user_device_name}"
)
is None
)
assert (
hass.states.get(
f"{PLATFORM}.{device_name}_plc_uplink_phyrate_{PLCNET.devices[2].user_device_name}"
)
is None
)
assert (
hass.states.get(f"{PLATFORM}.{device_name}_last_restart_of_the_device") is None
)
await hass.config_entries.async_unload(entry.entry_id)
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_connected_wi_fi_clients"
).disabled
assert entity_registry.async_get(
f"{PLATFORM}.{device_name}_connected_plc_devices"
).disabled
assert entity_registry.async_get(
f"{PLATFORM}.{device_name}_neighboring_wi_fi_networks"
).disabled
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_plc_downlink_phy_rate_{PLCNET.devices[1].user_device_name}"
).disabled
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_plc_uplink_phy_rate_{PLCNET.devices[1].user_device_name}"
).disabled
assert entity_registry.async_get(
f"{PLATFORM}.{device_name}_plc_downlink_phy_rate_{PLCNET.devices[2].user_device_name}"
).disabled
assert entity_registry.async_get(
f"{PLATFORM}.{device_name}_plc_uplink_phy_rate_{PLCNET.devices[2].user_device_name}"
).disabled
assert entity_registry.async_get(
f"{PLATFORM}.{device_name}_last_restart_of_the_device"
).disabled
@pytest.mark.parametrize(
@ -145,8 +137,6 @@ async def test_sensor(
assert state is not None
assert state.state == expected_state
await hass.config_entries.async_unload(entry.entry_id)
async def test_update_plc_phyrates(
hass: HomeAssistant,
@ -198,8 +188,6 @@ async def test_update_plc_phyrates(
assert state is not None
assert state.state == str(PLCNET.data_rates[0].tx_rate)
await hass.config_entries.async_unload(entry.entry_id)
async def test_update_last_update_auth_failed(
hass: HomeAssistant, mock_device: MockDevice
@ -222,5 +210,3 @@ async def test_update_last_update_auth_failed(
assert "context" in flow
assert flow["context"]["source"] == SOURCE_REAUTH
assert flow["context"]["entry_id"] == entry.entry_id
await hass.config_entries.async_unload(entry.entry_id)

View File

@ -35,17 +35,23 @@ from tests.common import async_fire_time_changed
@pytest.mark.usefixtures("mock_device")
async def test_switch_setup(hass: HomeAssistant) -> None:
async def test_switch_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test default setup of the switch component."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
assert hass.states.get(f"{PLATFORM}.{device_name}_enable_guest_wi_fi") is not None
assert hass.states.get(f"{PLATFORM}.{device_name}_enable_leds") is not None
await hass.config_entries.async_unload(entry.entry_id)
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_enable_guest_wi_fi"
).disabled
assert not entity_registry.async_get(
f"{PLATFORM}.{device_name}_enable_leds"
).disabled
async def test_update_guest_wifi_status_auth_failed(
@ -70,8 +76,6 @@ async def test_update_guest_wifi_status_auth_failed(
assert flow["context"]["source"] == SOURCE_REAUTH
assert flow["context"]["entry_id"] == entry.entry_id
await hass.config_entries.async_unload(entry.entry_id)
async def test_update_enable_guest_wifi(
hass: HomeAssistant,
@ -153,8 +157,6 @@ async def test_update_enable_guest_wifi(
assert state is not None
assert state.state == STATE_UNAVAILABLE
await hass.config_entries.async_unload(entry.entry_id)
async def test_update_enable_leds(
hass: HomeAssistant,
@ -230,8 +232,6 @@ async def test_update_enable_leds(
assert state is not None
assert state.state == STATE_UNAVAILABLE
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.parametrize(
("name", "get_method", "update_interval"),
@ -325,5 +325,3 @@ async def test_auth_failed(
assert "context" in flow
assert flow["context"]["source"] == SOURCE_REAUTH
assert flow["context"]["entry_id"] == entry.entry_id
await hass.config_entries.async_unload(entry.entry_id)

View File

@ -11,7 +11,7 @@ from homeassistant.components.devolo_home_network.const import (
FIRMWARE_UPDATE_INTERVAL,
)
from homeassistant.components.update import DOMAIN as PLATFORM, SERVICE_INSTALL
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -25,16 +25,18 @@ from tests.common import async_fire_time_changed
@pytest.mark.usefixtures("mock_device")
async def test_update_setup(hass: HomeAssistant) -> None:
async def test_update_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test default setup of the update component."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
assert hass.states.get(f"{PLATFORM}.{device_name}_firmware") is not None
await hass.config_entries.async_unload(entry.entry_id)
assert not entity_registry.async_get(f"{PLATFORM}.{device_name}_firmware").disabled
async def test_update_firmware(
@ -85,8 +87,6 @@ async def test_update_firmware(
assert device_info is not None
assert device_info.sw_version == mock_device.firmware_version
await hass.config_entries.async_unload(entry.entry_id)
async def test_device_failure_check(
hass: HomeAssistant,
@ -137,8 +137,6 @@ async def test_device_failure_update(
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)
async def test_auth_failed(hass: HomeAssistant, mock_device: MockDevice) -> None:
"""Test updating unauthorized triggers the reauth flow."""
@ -168,5 +166,3 @@ async def test_auth_failed(hass: HomeAssistant, mock_device: MockDevice) -> None
assert "context" in flow
assert flow["context"]["source"] == SOURCE_REAUTH
assert flow["context"]["entry_id"] == entry.entry_id
await hass.config_entries.async_unload(entry.entry_id)