mirror of
https://github.com/home-assistant/core.git
synced 2025-04-28 03:07:50 +00:00

* add config flow to enigma2 * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * fix suggested change * use parametrize for config flow tests * Restore PLATFORM_SCHEMA and add create_issue to async_setup_platform * fix docstring * remove name, refactor config flow * bump dependency * remove name, add verify_ssl, use async_create_clientsession * use translation key, change integration type to device * Bump openwebifpy to 4.2.1 * cleanup, remove CONF_NAME from entity, add async_set_unique_id * clear unneeded constants, fix tests * fix tests * move _attr_translation_key out of init * update test requirement * Address review comments * address review comments * clear strings.json * Review coments --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
150 lines
4.4 KiB
Python
150 lines
4.4 KiB
Python
"""Test the Enigma2 config flow."""
|
|
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
from aiohttp.client_exceptions import ClientError
|
|
from openwebif.error import InvalidAuthError
|
|
import pytest
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.enigma2.const import DOMAIN
|
|
from homeassistant.const import CONF_HOST
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from .conftest import (
|
|
EXPECTED_OPTIONS,
|
|
TEST_FULL,
|
|
TEST_IMPORT_FULL,
|
|
TEST_IMPORT_REQUIRED,
|
|
TEST_REQUIRED,
|
|
MockDevice,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
async def user_flow(hass: HomeAssistant) -> str:
|
|
"""Return a user-initiated flow after filling in host info."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
)
|
|
assert result["type"] == FlowResultType.FORM
|
|
assert result["errors"] is None
|
|
return result["flow_id"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("test_config"),
|
|
[(TEST_FULL), (TEST_REQUIRED)],
|
|
)
|
|
async def test_form_user(
|
|
hass: HomeAssistant, user_flow: str, test_config: dict[str, Any]
|
|
):
|
|
"""Test a successful user initiated flow."""
|
|
with (
|
|
patch(
|
|
"openwebif.api.OpenWebIfDevice.__new__",
|
|
return_value=MockDevice(),
|
|
),
|
|
patch(
|
|
"homeassistant.components.enigma2.async_setup_entry",
|
|
return_value=True,
|
|
) as mock_setup_entry,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(user_flow, test_config)
|
|
await hass.async_block_till_done()
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == test_config[CONF_HOST]
|
|
assert result["data"] == test_config
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "error_type"),
|
|
[
|
|
(InvalidAuthError, "invalid_auth"),
|
|
(ClientError, "cannot_connect"),
|
|
(Exception, "unknown"),
|
|
],
|
|
)
|
|
async def test_form_user_errors(
|
|
hass: HomeAssistant, user_flow, exception: Exception, error_type: str
|
|
) -> None:
|
|
"""Test we handle errors."""
|
|
with patch(
|
|
"homeassistant.components.enigma2.config_flow.OpenWebIfDevice.__new__",
|
|
side_effect=exception,
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(user_flow, TEST_FULL)
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
assert result["step_id"] == config_entries.SOURCE_USER
|
|
assert result["errors"] == {"base": error_type}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("test_config", "expected_data", "expected_options"),
|
|
[
|
|
(TEST_IMPORT_FULL, TEST_FULL, EXPECTED_OPTIONS),
|
|
(TEST_IMPORT_REQUIRED, TEST_REQUIRED, {}),
|
|
],
|
|
)
|
|
async def test_form_import(
|
|
hass: HomeAssistant,
|
|
test_config: dict[str, Any],
|
|
expected_data: dict[str, Any],
|
|
expected_options: dict[str, Any],
|
|
) -> None:
|
|
"""Test we get the form with import source."""
|
|
with (
|
|
patch(
|
|
"homeassistant.components.enigma2.config_flow.OpenWebIfDevice.__new__",
|
|
return_value=MockDevice(),
|
|
),
|
|
patch(
|
|
"homeassistant.components.enigma2.async_setup_entry",
|
|
return_value=True,
|
|
) as mock_setup_entry,
|
|
):
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
data=test_config,
|
|
)
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
|
assert result["title"] == test_config[CONF_HOST]
|
|
assert result["data"] == expected_data
|
|
assert result["options"] == expected_options
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exception", "error_type"),
|
|
[
|
|
(InvalidAuthError, "invalid_auth"),
|
|
(ClientError, "cannot_connect"),
|
|
(Exception, "unknown"),
|
|
],
|
|
)
|
|
async def test_form_import_errors(
|
|
hass: HomeAssistant, exception: Exception, error_type: str
|
|
) -> None:
|
|
"""Test we handle errors on import."""
|
|
with patch(
|
|
"homeassistant.components.enigma2.config_flow.OpenWebIfDevice.__new__",
|
|
side_effect=exception,
|
|
):
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
data=TEST_IMPORT_FULL,
|
|
)
|
|
|
|
assert result["type"] == FlowResultType.FORM
|
|
assert result["errors"] == {"base": error_type}
|