mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 12:21:04 -04:00
Refactor: Use shared fixtures in Vallox config flow tests (#188932)
This commit is contained in:
@@ -15,8 +15,8 @@ rules:
|
||||
config-flow-test-coverage:
|
||||
status: todo
|
||||
comment: >-
|
||||
config_flow.py branch coverage is missing for async_step_reconfigure with
|
||||
the unchanged host, where the duplicate check is skipped.
|
||||
Tests that create an entry must also check its unique ID. Blocked on the
|
||||
config entry unique ID from unique-config-entry.
|
||||
config-flow:
|
||||
status: todo
|
||||
comment: >-
|
||||
@@ -110,9 +110,6 @@ rules:
|
||||
(2) The fan's power-setting failure and unsupported-profile fan speed
|
||||
error paths are not tested.
|
||||
(3) No test explicitly unloads the entry.
|
||||
(4) The config flow tests patch the library inline instead of using
|
||||
shared mock_client and mock_setup_entry fixtures, and the user-step
|
||||
error tests could be one parametrized test.
|
||||
|
||||
# Gold
|
||||
devices:
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
"""Tests for the Vallox integration."""
|
||||
|
||||
TEST_HOST = "192.168.100.50"
|
||||
TEST_NAME = "Vallox"
|
||||
|
||||
@@ -1,132 +1,70 @@
|
||||
"""Common utilities for Vallox tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from vallox_websocket_api import MetricData
|
||||
from pytest_homeassistant.common import MockConfigEntry, load_json_object_fixture
|
||||
from vallox_websocket_api import MetricData, MetricValue
|
||||
|
||||
from homeassistant.components.vallox.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigFlowResult
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
DEFAULT_HOST = "192.168.100.50"
|
||||
DEFAULT_NAME = "Vallox"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def default_host() -> str:
|
||||
"""Return the default host used in the default mock entry."""
|
||||
return DEFAULT_HOST
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def default_name() -> str:
|
||||
"""Return the default name used in the default mock entry."""
|
||||
return DEFAULT_NAME
|
||||
from . import TEST_HOST, TEST_NAME
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_entry(
|
||||
hass: HomeAssistant, default_host: str, default_name: str
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
fetch_metric_data_mock: AsyncMock,
|
||||
) -> MockConfigEntry:
|
||||
"""Create mocked Vallox config entry fixture."""
|
||||
return create_mock_entry(hass, default_host, default_name)
|
||||
"""Add the mocked Vallox config entry to Home Assistant."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
return mock_config_entry
|
||||
|
||||
|
||||
def create_mock_entry(hass: HomeAssistant, host: str, name: str) -> MockConfigEntry:
|
||||
"""Create mocked Vallox config entry."""
|
||||
vallox_mock_entry = MockConfigEntry(
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Return the default mocked config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_HOST: host,
|
||||
CONF_NAME: name,
|
||||
},
|
||||
title=TEST_NAME,
|
||||
data={CONF_HOST: TEST_HOST, CONF_NAME: TEST_NAME},
|
||||
)
|
||||
vallox_mock_entry.add_to_hass(hass)
|
||||
|
||||
return vallox_mock_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def setup_vallox_entry(
|
||||
hass: HomeAssistant, default_host: str, default_name: str
|
||||
) -> None:
|
||||
"""Define a fixture to set up Vallox."""
|
||||
await do_setup_vallox_entry(hass, default_host, default_name)
|
||||
|
||||
|
||||
async def do_setup_vallox_entry(hass: HomeAssistant, host: str, name: str) -> None:
|
||||
"""Set up the Vallox component."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
CONF_HOST: host,
|
||||
CONF_NAME: name,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def init_reconfigure_flow(
|
||||
hass: HomeAssistant, mock_entry, setup_vallox_entry
|
||||
) -> tuple[MockConfigEntry, ConfigFlowResult]:
|
||||
"""Initialize a config entry and a reconfigure flow for it."""
|
||||
result = await mock_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
# original entry
|
||||
assert mock_entry.data["host"] == "192.168.100.50"
|
||||
|
||||
return (mock_entry, result)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def default_metrics():
|
||||
def default_metrics() -> dict[str, MetricValue]:
|
||||
"""Return default Vallox metrics."""
|
||||
return {
|
||||
"A_CYC_MACHINE_MODEL": 3,
|
||||
"A_CYC_APPL_SW_VERSION_1": 2,
|
||||
"A_CYC_APPL_SW_VERSION_2": 0,
|
||||
"A_CYC_APPL_SW_VERSION_3": 16,
|
||||
"A_CYC_UUID0": 5,
|
||||
"A_CYC_UUID1": 6,
|
||||
"A_CYC_UUID2": 7,
|
||||
"A_CYC_UUID3": 8,
|
||||
"A_CYC_UUID4": 9,
|
||||
"A_CYC_UUID5": 10,
|
||||
"A_CYC_UUID6": 11,
|
||||
"A_CYC_UUID7": 12,
|
||||
"A_CYC_BOOST_TIMER": 0,
|
||||
"A_CYC_FIREPLACE_TIMER": 0,
|
||||
"A_CYC_EXTRA_TIMER": 0,
|
||||
"A_CYC_MODE": 0,
|
||||
"A_CYC_STATE": 0,
|
||||
"A_CYC_FILTER_CHANGED_YEAR": 24,
|
||||
"A_CYC_FILTER_CHANGED_MONTH": 2,
|
||||
"A_CYC_FILTER_CHANGED_DAY": 16,
|
||||
"A_CYC_FILTER_CHANGE_INTERVAL": 120,
|
||||
"A_CYC_TOTAL_FAULT_COUNT": 0,
|
||||
"A_CYC_FAULT_CODE": 0,
|
||||
"A_CYC_FAULT_ACTIVITY": 0,
|
||||
"A_CYC_FAULT_FIRST_DATE": 0,
|
||||
"A_CYC_FAULT_LAST_DATE": 0,
|
||||
"A_CYC_FAULT_SEVERITY": 0,
|
||||
"A_CYC_FAULT_COUNT": 0,
|
||||
"A_CYC_HOME_SPEED_SETTING": 30,
|
||||
"A_CYC_AWAY_SPEED_SETTING": 10,
|
||||
"A_CYC_BOOST_SPEED_SETTING": 80,
|
||||
}
|
||||
return load_json_object_fixture("metrics.json", DOMAIN)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@pytest.fixture
|
||||
def mock_vallox_client(
|
||||
default_metrics: dict[str, MetricValue],
|
||||
) -> Generator[AsyncMock]:
|
||||
"""Return a mocked Vallox client used by both the integration and config flow."""
|
||||
with (
|
||||
patch("homeassistant.components.vallox.Vallox", autospec=True) as mock_class,
|
||||
patch("homeassistant.components.vallox.config_flow.Vallox", new=mock_class),
|
||||
):
|
||||
client = mock_class.return_value
|
||||
client.fetch_metric_data.return_value = MetricData(default_metrics)
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.vallox.async_setup_entry", return_value=None
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fetch_metric_data_mock(default_metrics):
|
||||
"""Stub the Vallox fetch_metric_data method."""
|
||||
with patch(
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"A_CYC_MACHINE_MODEL": 3,
|
||||
"A_CYC_APPL_SW_VERSION_1": 2,
|
||||
"A_CYC_APPL_SW_VERSION_2": 0,
|
||||
"A_CYC_APPL_SW_VERSION_3": 16,
|
||||
"A_CYC_UUID0": 5,
|
||||
"A_CYC_UUID1": 6,
|
||||
"A_CYC_UUID2": 7,
|
||||
"A_CYC_UUID3": 8,
|
||||
"A_CYC_UUID4": 9,
|
||||
"A_CYC_UUID5": 10,
|
||||
"A_CYC_UUID6": 11,
|
||||
"A_CYC_UUID7": 12,
|
||||
"A_CYC_BOOST_TIMER": 0,
|
||||
"A_CYC_FIREPLACE_TIMER": 0,
|
||||
"A_CYC_EXTRA_TIMER": 0,
|
||||
"A_CYC_MODE": 0,
|
||||
"A_CYC_STATE": 0,
|
||||
"A_CYC_FILTER_CHANGED_YEAR": 24,
|
||||
"A_CYC_FILTER_CHANGED_MONTH": 2,
|
||||
"A_CYC_FILTER_CHANGED_DAY": 16,
|
||||
"A_CYC_FILTER_CHANGE_INTERVAL": 120,
|
||||
"A_CYC_TOTAL_FAULT_COUNT": 0,
|
||||
"A_CYC_FAULT_CODE": 0,
|
||||
"A_CYC_FAULT_ACTIVITY": 0,
|
||||
"A_CYC_FAULT_FIRST_DATE": 0,
|
||||
"A_CYC_FAULT_LAST_DATE": 0,
|
||||
"A_CYC_FAULT_SEVERITY": 0,
|
||||
"A_CYC_FAULT_COUNT": 0,
|
||||
"A_CYC_HOME_SPEED_SETTING": 30,
|
||||
"A_CYC_AWAY_SPEED_SETTING": 10,
|
||||
"A_CYC_BOOST_SPEED_SETTING": 80
|
||||
}
|
||||
@@ -1,382 +1,234 @@
|
||||
"""Test the Vallox integration config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from vallox_websocket_api import ValloxApiException, ValloxWebsocketException
|
||||
|
||||
from homeassistant.components.vallox.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .conftest import create_mock_entry, do_setup_vallox_entry
|
||||
from . import TEST_HOST, TEST_NAME
|
||||
|
||||
OTHER_HOST = "192.168.100.60"
|
||||
INVALID_HOST = "test.host.com"
|
||||
|
||||
|
||||
async def test_form_no_input(hass: HomeAssistant) -> None:
|
||||
"""Test that the form is returned with no input."""
|
||||
@pytest.mark.usefixtures("mock_vallox_client")
|
||||
async def test_form_create_entry(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test that an entry is created with valid input."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
|
||||
async def test_form_create_entry(hass: HomeAssistant) -> None:
|
||||
"""Test that an entry is created with valid input."""
|
||||
init = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: TEST_HOST}
|
||||
)
|
||||
|
||||
assert init["type"] is FlowResultType.FORM
|
||||
assert init["errors"] is None
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
return_value=None,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.vallox.async_setup_entry",
|
||||
return_value=None,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "1.2.3.4"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Vallox"
|
||||
assert result["data"] == {"host": "1.2.3.4", "name": "Vallox"}
|
||||
assert result["title"] == TEST_NAME
|
||||
assert result["data"] == {CONF_HOST: TEST_HOST, CONF_NAME: TEST_NAME}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_ip(hass: HomeAssistant) -> None:
|
||||
"""Test that invalid IP error is handled."""
|
||||
init = await hass.config_entries.flow.async_init(
|
||||
@pytest.mark.usefixtures("mock_vallox_client", "mock_setup_entry")
|
||||
async def test_form_invalid_host(hass: HomeAssistant) -> None:
|
||||
"""Test that an invalid host is rejected and the flow recovers."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "test.host.com"},
|
||||
result["flow_id"], {CONF_HOST: INVALID_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"host": "invalid_host"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
return_value=None,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.vallox.async_setup_entry",
|
||||
return_value=None,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "1.2.3.4"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Vallox"
|
||||
assert result["data"] == {"host": "1.2.3.4", "name": "Vallox"}
|
||||
|
||||
|
||||
async def test_form_vallox_api_exception_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test that cannot connect error is handled."""
|
||||
init = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
side_effect=ValloxApiException,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "4.3.2.1"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"host": "cannot_connect"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
return_value=None,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.vallox.async_setup_entry",
|
||||
return_value=None,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "1.2.3.4"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Vallox"
|
||||
assert result["data"] == {"host": "1.2.3.4", "name": "Vallox"}
|
||||
|
||||
|
||||
async def test_form_os_error_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test that cannot connect error is handled."""
|
||||
init = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
side_effect=ValloxWebsocketException,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "5.6.7.8"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"host": "cannot_connect"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
return_value=None,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.vallox.async_setup_entry",
|
||||
return_value=None,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "1.2.3.4"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Vallox"
|
||||
assert result["data"] == {"host": "1.2.3.4", "name": "Vallox"}
|
||||
|
||||
|
||||
async def test_form_unknown_exception(hass: HomeAssistant) -> None:
|
||||
"""Test that unknown exceptions are handled."""
|
||||
init = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
side_effect=Exception,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "54.12.31.41"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"host": "unknown"}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
return_value=None,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.vallox.async_setup_entry",
|
||||
return_value=None,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "1.2.3.4"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "Vallox"
|
||||
assert result["data"] == {"host": "1.2.3.4", "name": "Vallox"}
|
||||
|
||||
|
||||
async def test_form_already_configured(hass: HomeAssistant) -> None:
|
||||
"""Test that already configured error is handled."""
|
||||
init = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
create_mock_entry(hass, "20.40.10.30", "Vallox 110 MV")
|
||||
assert result["errors"] == {CONF_HOST: "invalid_host"}
|
||||
|
||||
# Verify recovery without starting a new flow (rule config-flow-test-coverage).
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
init["flow_id"],
|
||||
{"host": "20.40.10.30"},
|
||||
result["flow_id"], {CONF_HOST: TEST_HOST}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {CONF_HOST: TEST_HOST, CONF_NAME: TEST_NAME}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
pytest.param(ValloxApiException, "cannot_connect", id="api_error"),
|
||||
pytest.param(ValloxWebsocketException, "cannot_connect", id="websocket_error"),
|
||||
pytest.param(Exception, "unknown", id="unknown_error"),
|
||||
],
|
||||
)
|
||||
async def test_form_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_vallox_client: AsyncMock,
|
||||
exception: type[Exception],
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test that connection errors are handled and the flow recovers."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_vallox_client.fetch_metric_data.side_effect = exception
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: TEST_HOST}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {CONF_HOST: error}
|
||||
|
||||
# Verify recovery without starting a new flow (rule config-flow-test-coverage).
|
||||
mock_vallox_client.fetch_metric_data.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: TEST_HOST}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {CONF_HOST: TEST_HOST, CONF_NAME: TEST_NAME}
|
||||
|
||||
|
||||
async def test_form_already_configured(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that a host that is already configured is rejected."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: TEST_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_reconfigure_host(hass: HomeAssistant, init_reconfigure_flow) -> None:
|
||||
@pytest.mark.usefixtures("mock_vallox_client", "mock_setup_entry")
|
||||
@pytest.mark.parametrize(
|
||||
"host",
|
||||
[
|
||||
pytest.param(OTHER_HOST, id="new_host"),
|
||||
# Resubmitting the current host is expected to succeed as well.
|
||||
pytest.param(TEST_HOST, id="unchanged_host"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_host(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry, host: str
|
||||
) -> None:
|
||||
"""Test that the host can be reconfigured."""
|
||||
entry, init_flow_result = init_reconfigure_flow
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.60",
|
||||
},
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reconfigure"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: host}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||
assert reconfigure_result["reason"] == "reconfigure_successful"
|
||||
|
||||
# changed entry
|
||||
assert entry.data["host"] == "192.168.100.60"
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data[CONF_HOST] == host
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_vallox_client", "mock_setup_entry")
|
||||
async def test_reconfigure_host_to_same_host_as_another_fails(
|
||||
hass: HomeAssistant, init_reconfigure_flow
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that changing host to a host that already exists fails."""
|
||||
entry, init_flow_result = init_reconfigure_flow
|
||||
"""Test that changing the host to the host of another entry fails."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_HOST: OTHER_HOST, CONF_NAME: "Vallox 2"},
|
||||
).add_to_hass(hass)
|
||||
|
||||
# Create second device
|
||||
create_mock_entry(hass=hass, host="192.168.100.70", name="Vallox 2")
|
||||
await do_setup_vallox_entry(hass=hass, host="192.168.100.70", name="Vallox 2")
|
||||
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.70",
|
||||
},
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: OTHER_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||
assert reconfigure_result["reason"] == "already_configured"
|
||||
|
||||
# entry not changed
|
||||
assert entry.data["host"] == "192.168.100.50"
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
assert mock_config_entry.data[CONF_HOST] == TEST_HOST
|
||||
|
||||
|
||||
async def test_reconfigure_host_to_invalid_ip_fails(
|
||||
hass: HomeAssistant, init_reconfigure_flow
|
||||
@pytest.mark.usefixtures("mock_vallox_client", "mock_setup_entry")
|
||||
async def test_reconfigure_invalid_host(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that an invalid IP error is handled by the reconfigure step."""
|
||||
entry, init_flow_result = init_reconfigure_flow
|
||||
"""Test that an invalid host is rejected and the reconfigure flow recovers."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "test.host.com",
|
||||
},
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: INVALID_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert reconfigure_result["type"] is FlowResultType.FORM
|
||||
assert reconfigure_result["errors"] == {"host": "invalid_host"}
|
||||
|
||||
# entry not changed
|
||||
assert entry.data["host"] == "192.168.100.50"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {CONF_HOST: "invalid_host"}
|
||||
assert mock_config_entry.data[CONF_HOST] == TEST_HOST
|
||||
|
||||
# makes sure we can recover and continue
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.60",
|
||||
},
|
||||
# Verify recovery without starting a new flow (rule config-flow-test-coverage).
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: OTHER_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||
assert reconfigure_result["reason"] == "reconfigure_successful"
|
||||
|
||||
# changed entry
|
||||
assert entry.data["host"] == "192.168.100.60"
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data[CONF_HOST] == OTHER_HOST
|
||||
|
||||
|
||||
async def test_reconfigure_host_vallox_api_exception_cannot_connect(
|
||||
hass: HomeAssistant, init_reconfigure_flow
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "error"),
|
||||
[
|
||||
pytest.param(ValloxApiException, "cannot_connect", id="api_error"),
|
||||
pytest.param(Exception, "unknown", id="unknown_error"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_errors(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_vallox_client: AsyncMock,
|
||||
exception: type[Exception],
|
||||
error: str,
|
||||
) -> None:
|
||||
"""Test that cannot connect error is handled by the reconfigure step."""
|
||||
entry, init_flow_result = init_reconfigure_flow
|
||||
"""Test that connection errors are handled and the reconfigure flow recovers."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
side_effect=ValloxApiException,
|
||||
):
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.80",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
result = await mock_config_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert reconfigure_result["type"] is FlowResultType.FORM
|
||||
assert reconfigure_result["errors"] == {"host": "cannot_connect"}
|
||||
|
||||
# entry not changed
|
||||
assert entry.data["host"] == "192.168.100.50"
|
||||
|
||||
# makes sure we can recover and continue
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.60",
|
||||
},
|
||||
mock_vallox_client.fetch_metric_data.side_effect = exception
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: OTHER_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||
assert reconfigure_result["reason"] == "reconfigure_successful"
|
||||
|
||||
# changed entry
|
||||
assert entry.data["host"] == "192.168.100.60"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {CONF_HOST: error}
|
||||
assert mock_config_entry.data[CONF_HOST] == TEST_HOST
|
||||
|
||||
|
||||
async def test_reconfigure_host_unknown_exception(
|
||||
hass: HomeAssistant, init_reconfigure_flow
|
||||
) -> None:
|
||||
"""Test that cannot connect error is handled by the reconfigure step."""
|
||||
entry, init_flow_result = init_reconfigure_flow
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.vallox.config_flow.Vallox.fetch_metric_data",
|
||||
side_effect=Exception,
|
||||
):
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.90",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert reconfigure_result["type"] is FlowResultType.FORM
|
||||
assert reconfigure_result["errors"] == {"host": "unknown"}
|
||||
|
||||
# entry not changed
|
||||
assert entry.data["host"] == "192.168.100.50"
|
||||
|
||||
# makes sure we can recover and continue
|
||||
reconfigure_result = await hass.config_entries.flow.async_configure(
|
||||
init_flow_result["flow_id"],
|
||||
{
|
||||
"host": "192.168.100.60",
|
||||
},
|
||||
# Verify recovery without starting a new flow (rule config-flow-test-coverage).
|
||||
mock_vallox_client.fetch_metric_data.side_effect = None
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: OTHER_HOST}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert reconfigure_result["type"] is FlowResultType.ABORT
|
||||
assert reconfigure_result["reason"] == "reconfigure_successful"
|
||||
|
||||
# changed entry
|
||||
assert entry.data["host"] == "192.168.100.60"
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
assert mock_config_entry.data[CONF_HOST] == OTHER_HOST
|
||||
|
||||
Reference in New Issue
Block a user