mirror of
https://github.com/home-assistant/core.git
synced 2026-05-19 06:50:15 +01:00
97 lines
2.8 KiB
Python
97 lines
2.8 KiB
Python
"""Tests for the Duco config flow."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from duco.exceptions import DucoConnectionError, DucoError
|
|
import pytest
|
|
|
|
from homeassistant.components.duco.const import DOMAIN
|
|
from homeassistant.config_entries import SOURCE_USER
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from .conftest import TEST_MAC, USER_INPUT
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_user_flow_success(
|
|
hass: HomeAssistant,
|
|
mock_duco_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
) -> None:
|
|
"""Test a successful user flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {}
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], USER_INPUT
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == "SILENT_CONNECT"
|
|
assert result["data"] == USER_INPUT
|
|
assert result["result"].unique_id == TEST_MAC
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "expected_error"),
|
|
[
|
|
(DucoConnectionError("Connection refused"), "cannot_connect"),
|
|
(DucoError("Unexpected error"), "unknown"),
|
|
],
|
|
)
|
|
async def test_user_flow_error(
|
|
hass: HomeAssistant,
|
|
mock_duco_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
exception: Exception,
|
|
expected_error: str,
|
|
) -> None:
|
|
"""Test handling of connection and unknown errors in the user flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
mock_duco_client.async_get_board_info.side_effect = exception
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], USER_INPUT
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["step_id"] == "user"
|
|
assert result["errors"] == {"base": expected_error}
|
|
|
|
mock_duco_client.async_get_board_info.side_effect = None
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], USER_INPUT
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
async def test_user_flow_duplicate(
|
|
hass: HomeAssistant,
|
|
mock_duco_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test that a duplicate config entry is aborted."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
# Second attempt for the same device
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"], USER_INPUT
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|