mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00

* Init Autarco integration * Add integration code with tests * Update every 5 minutes * Process all feedback from Joost * Bump lib to v2.0.0 * Add more then one site if present * Fix issue with entity translation * Update the test for sensor entities * Fix round two based on feedback from Joost * Add autarco to strict typing * Update tests/components/autarco/test_config_flow.py * Update tests/components/autarco/test_config_flow.py --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
102 lines
3.2 KiB
Python
102 lines
3.2 KiB
Python
"""Test the Autarco config flow."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from autarco import AutarcoAuthenticationError, AutarcoConnectionError
|
|
import pytest
|
|
|
|
from homeassistant.components.autarco.const import DOMAIN
|
|
from homeassistant.config_entries import SOURCE_USER
|
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_full_user_flow(
|
|
hass: HomeAssistant,
|
|
mock_autarco_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
) -> None:
|
|
"""Test the full user configuration flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result.get("type") is FlowResultType.FORM
|
|
assert result.get("step_id") == "user"
|
|
assert not result.get("errors")
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={CONF_EMAIL: "test@autarco.com", CONF_PASSWORD: "test-password"},
|
|
)
|
|
|
|
assert result.get("type") is FlowResultType.CREATE_ENTRY
|
|
assert result.get("title") == "test@autarco.com"
|
|
assert result.get("data") == {
|
|
CONF_EMAIL: "test@autarco.com",
|
|
CONF_PASSWORD: "test-password",
|
|
}
|
|
assert len(mock_autarco_client.get_account.mock_calls) == 1
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
async def test_duplicate_entry(
|
|
hass: HomeAssistant,
|
|
mock_config_entry: MockConfigEntry,
|
|
mock_autarco_client: AsyncMock,
|
|
) -> None:
|
|
"""Test abort when setting up duplicate entry."""
|
|
mock_config_entry.add_to_hass(hass)
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
)
|
|
|
|
assert result.get("type") is FlowResultType.FORM
|
|
assert not result.get("errors")
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={CONF_EMAIL: "test@autarco.com", CONF_PASSWORD: "test-password"},
|
|
)
|
|
|
|
assert result.get("type") is FlowResultType.ABORT
|
|
assert result.get("reason") == "already_configured"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "error"),
|
|
[
|
|
(AutarcoConnectionError, "cannot_connect"),
|
|
(AutarcoAuthenticationError, "invalid_auth"),
|
|
],
|
|
)
|
|
async def test_exceptions(
|
|
hass: HomeAssistant,
|
|
mock_autarco_client: AsyncMock,
|
|
mock_setup_entry: AsyncMock,
|
|
exception: Exception,
|
|
error: str,
|
|
) -> None:
|
|
"""Test exceptions."""
|
|
mock_autarco_client.get_account.side_effect = exception
|
|
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={CONF_EMAIL: "test@autarco.com", CONF_PASSWORD: "test-password"},
|
|
)
|
|
assert result.get("type") is FlowResultType.FORM
|
|
assert result.get("errors") == {"base": error}
|
|
|
|
mock_autarco_client.get_account.side_effect = None
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
user_input={CONF_EMAIL: "test@autarco.com", CONF_PASSWORD: "test-password"},
|
|
)
|
|
assert result.get("type") is FlowResultType.CREATE_ENTRY
|