Add config flow to nfandroidtv (#51280)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
02a7a2464a
commit
462db1b4b2
14 changed files with 512 additions and 185 deletions
135
tests/components/nfandroidtv/test_config_flow.py
Normal file
135
tests/components/nfandroidtv/test_config_flow.py
Normal file
|
@ -0,0 +1,135 @@
|
|||
"""Test NFAndroidTV config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from notifications_android_tv.notifications import ConnectError
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow
|
||||
from homeassistant.components.nfandroidtv.const import DEFAULT_NAME, DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||
|
||||
from . import (
|
||||
CONF_CONFIG_FLOW,
|
||||
CONF_DATA,
|
||||
HOST,
|
||||
NAME,
|
||||
_create_mocked_tv,
|
||||
_patch_config_flow_tv,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
def _patch_setup():
|
||||
return patch(
|
||||
"homeassistant.components.nfandroidtv.async_setup_entry",
|
||||
return_value=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_flow_user(hass):
|
||||
"""Test user initialized flow."""
|
||||
mocked_tv = await _create_mocked_tv()
|
||||
with _patch_config_flow_tv(mocked_tv), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input=CONF_CONFIG_FLOW,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == NAME
|
||||
assert result["data"] == CONF_DATA
|
||||
|
||||
|
||||
async def test_flow_user_already_configured(hass):
|
||||
"""Test user initialized flow with duplicate server."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONF_CONFIG_FLOW,
|
||||
unique_id=HOST,
|
||||
)
|
||||
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mocked_tv = await _create_mocked_tv()
|
||||
with _patch_config_flow_tv(mocked_tv), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
)
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input=CONF_CONFIG_FLOW,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_flow_user_cannot_connect(hass):
|
||||
"""Test user initialized flow with unreachable server."""
|
||||
mocked_tv = await _create_mocked_tv(True)
|
||||
with _patch_config_flow_tv(mocked_tv) as tvmock:
|
||||
tvmock.side_effect = ConnectError
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data=CONF_CONFIG_FLOW,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_flow_user_unknown_error(hass):
|
||||
"""Test user initialized flow with unreachable server."""
|
||||
mocked_tv = await _create_mocked_tv(True)
|
||||
with _patch_config_flow_tv(mocked_tv) as tvmock:
|
||||
tvmock.side_effect = Exception
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data=CONF_CONFIG_FLOW,
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_flow_import(hass):
|
||||
"""Test an import flow."""
|
||||
mocked_tv = await _create_mocked_tv(True)
|
||||
with _patch_config_flow_tv(mocked_tv), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data=CONF_CONFIG_FLOW,
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == CONF_DATA
|
||||
|
||||
with _patch_config_flow_tv(mocked_tv), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data=CONF_CONFIG_FLOW,
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_flow_import_missing_optional(hass):
|
||||
"""Test an import flow with missing options."""
|
||||
mocked_tv = await _create_mocked_tv(True)
|
||||
with _patch_config_flow_tv(mocked_tv), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data={CONF_HOST: HOST},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {CONF_HOST: HOST, CONF_NAME: f"{DEFAULT_NAME} {HOST}"}
|
Loading…
Add table
Add a link
Reference in a new issue