mirror of
https://github.com/home-assistant/core.git
synced 2026-05-18 22:40:15 +01:00
261ca2dd9a
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
154 lines
4.7 KiB
Python
154 lines
4.7 KiB
Python
"""Test the CentriConnect/MyPropane API config flow."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from aiocentriconnect.exceptions import (
|
|
CentriConnectConnectionError,
|
|
CentriConnectConnectionTimeoutError,
|
|
CentriConnectDecodeError,
|
|
CentriConnectEmptyResponseError,
|
|
CentriConnectNotFoundError,
|
|
CentriConnectTooManyRequestsError,
|
|
)
|
|
import pytest
|
|
|
|
from homeassistant.components.centriconnect.const import DOMAIN
|
|
from homeassistant.config_entries import SOURCE_USER
|
|
from homeassistant.const import CONF_DEVICE_ID, CONF_PASSWORD, CONF_USERNAME
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from .const import TEST_PASSWORD, TEST_TANK_ID, TEST_TANK_NAME, TEST_USERNAME
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_full_flow(
|
|
hass: HomeAssistant,
|
|
mock_centriconnect_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
) -> None:
|
|
"""Test full 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"],
|
|
{
|
|
CONF_DEVICE_ID: TEST_TANK_ID,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == TEST_TANK_NAME
|
|
assert result["data"] == {
|
|
CONF_DEVICE_ID: TEST_TANK_ID,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
}
|
|
assert result["result"].unique_id == TEST_TANK_ID
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "error"),
|
|
[
|
|
(CentriConnectNotFoundError, "invalid_auth"),
|
|
(CentriConnectDecodeError("Oh no!", "Bad response"), "unknown"),
|
|
(CentriConnectConnectionTimeoutError, "cannot_connect"),
|
|
(CentriConnectConnectionError, "cannot_connect"),
|
|
(CentriConnectTooManyRequestsError, "cannot_connect"),
|
|
(CentriConnectEmptyResponseError, "unknown"),
|
|
(Exception, "unknown"),
|
|
],
|
|
)
|
|
async def test_form_errors(
|
|
hass: HomeAssistant,
|
|
mock_centriconnect_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
exception: Exception,
|
|
error: str,
|
|
) -> None:
|
|
"""Test flow errors."""
|
|
mock_centriconnect_client.async_get_tank_data.side_effect = exception
|
|
|
|
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"],
|
|
{
|
|
CONF_DEVICE_ID: TEST_TANK_ID,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
assert result["errors"] == {"base": error}
|
|
|
|
# Make sure the config flow tests finish with either an
|
|
# FlowResultType.CREATE_ENTRY or FlowResultType.ABORT so
|
|
# we can show the config flow is able to recover from an error.
|
|
mock_centriconnect_client.async_get_tank_data.side_effect = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{
|
|
CONF_DEVICE_ID: TEST_TANK_ID,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == TEST_TANK_NAME
|
|
assert result["data"] == {
|
|
CONF_DEVICE_ID: TEST_TANK_ID,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
}
|
|
assert result["result"].unique_id == TEST_TANK_ID
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_user_flow_already_configured(
|
|
hass: HomeAssistant,
|
|
mock_centriconnect_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test that duplicate devices are rejected."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
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"],
|
|
{
|
|
CONF_DEVICE_ID: TEST_TANK_ID,
|
|
CONF_USERNAME: TEST_USERNAME,
|
|
CONF_PASSWORD: TEST_PASSWORD,
|
|
},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
assert result["reason"] == "already_configured"
|