Use fixtures in deCONZ config flow tests PT1 (#121121)

This commit is contained in:
Robert Svensson 2024-07-04 11:49:46 +02:00 committed by GitHub
parent ee2df9c4b3
commit 02e7707f91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 29 deletions

View file

@ -11,7 +11,7 @@ from pydeconz.websocket import Signal
import pytest
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
@ -32,6 +32,7 @@ def fixture_config_entry(
hass: HomeAssistant,
config_entry_data: MappingProxyType[str, Any],
config_entry_options: MappingProxyType[str, Any],
config_entry_source: str,
) -> ConfigEntry:
"""Define a config entry fixture."""
config_entry = MockConfigEntry(
@ -40,6 +41,7 @@ def fixture_config_entry(
unique_id=BRIDGEID,
data=config_entry_data,
options=config_entry_options,
source=config_entry_source,
)
config_entry.add_to_hass(hass)
return config_entry
@ -61,6 +63,12 @@ def fixture_config_entry_options() -> MappingProxyType[str, Any]:
return {}
@pytest.fixture(name="config_entry_source")
def fixture_config_entry_source() -> str:
"""Define a config entry source fixture."""
return SOURCE_USER
# Request mocks

View file

@ -27,6 +27,7 @@ from homeassistant.config_entries import (
SOURCE_REAUTH,
SOURCE_SSDP,
SOURCE_USER,
ConfigEntry,
)
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON
from homeassistant.core import HomeAssistant
@ -481,11 +482,9 @@ async def test_flow_ssdp_discovery(
async def test_ssdp_discovery_update_configuration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, config_entry_setup: ConfigEntry
) -> None:
"""Test if a discovered bridge is configured but updates with new attributes."""
config_entry = await setup_deconz_integration(hass, aioclient_mock)
with patch(
"homeassistant.components.deconz.async_setup_entry",
return_value=True,
@ -507,15 +506,14 @@ async def test_ssdp_discovery_update_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "2.3.4.5"
assert config_entry_setup.data[CONF_HOST] == "2.3.4.5"
assert len(mock_setup_entry.mock_calls) == 1
async def test_ssdp_discovery_dont_update_configuration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, config_entry_setup: ConfigEntry
) -> None:
"""Test if a discovered bridge has already been configured."""
config_entry = await setup_deconz_integration(hass, aioclient_mock)
result = await hass.config_entries.flow.async_init(
DECONZ_DOMAIN,
@ -533,17 +531,14 @@ async def test_ssdp_discovery_dont_update_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "1.2.3.4"
assert config_entry_setup.data[CONF_HOST] == "1.2.3.4"
@pytest.mark.parametrize("config_entry_source", [SOURCE_HASSIO])
async def test_ssdp_discovery_dont_update_existing_hassio_configuration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, config_entry_setup: ConfigEntry
) -> None:
"""Test to ensure the SSDP discovery does not update an Hass.io entry."""
config_entry = await setup_deconz_integration(
hass, aioclient_mock, source=SOURCE_HASSIO
)
result = await hass.config_entries.flow.async_init(
DECONZ_DOMAIN,
data=ssdp.SsdpServiceInfo(
@ -560,7 +555,7 @@ async def test_ssdp_discovery_dont_update_existing_hassio_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "1.2.3.4"
assert config_entry_setup.data[CONF_HOST] == "1.2.3.4"
async def test_flow_hassio_discovery(hass: HomeAssistant) -> None:
@ -610,11 +605,10 @@ async def test_flow_hassio_discovery(hass: HomeAssistant) -> None:
async def test_hassio_discovery_update_configuration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant,
config_entry_setup: ConfigEntry,
) -> None:
"""Test we can update an existing config entry."""
config_entry = await setup_deconz_integration(hass, aioclient_mock)
with patch(
"homeassistant.components.deconz.async_setup_entry",
return_value=True,
@ -638,18 +632,15 @@ async def test_hassio_discovery_update_configuration(
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "2.3.4.5"
assert config_entry.data[CONF_PORT] == 8080
assert config_entry.data[CONF_API_KEY] == "updated"
assert config_entry_setup.data[CONF_HOST] == "2.3.4.5"
assert config_entry_setup.data[CONF_PORT] == 8080
assert config_entry_setup.data[CONF_API_KEY] == "updated"
assert len(mock_setup_entry.mock_calls) == 1
async def test_hassio_discovery_dont_update_configuration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
@pytest.mark.usefixtures("config_entry_setup")
async def test_hassio_discovery_dont_update_configuration(hass: HomeAssistant) -> None:
"""Test we can update an existing config entry."""
await setup_deconz_integration(hass, aioclient_mock)
result = await hass.config_entries.flow.async_init(
DECONZ_DOMAIN,
data=HassioServiceInfo(
@ -671,12 +662,10 @@ async def test_hassio_discovery_dont_update_configuration(
async def test_option_flow(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, config_entry_setup: ConfigEntry
) -> None:
"""Test config flow options."""
config_entry = await setup_deconz_integration(hass, aioclient_mock)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
result = await hass.config_entries.options.async_init(config_entry_setup.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "deconz_devices"