core/tests/components/goalzero/test_config_flow.py

116 lines
3.6 KiB
Python

"""Test Goal Zero Yeti config flow."""
from goalzero import exceptions
from homeassistant.components.goalzero.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from . import (
CONF_CONFIG_FLOW,
CONF_DATA,
CONF_HOST,
CONF_NAME,
NAME,
_create_mocked_yeti,
_patch_config_flow_yeti,
)
from tests.async_mock import patch
from tests.common import MockConfigEntry
def _flow_next(hass, flow_id):
return next(
flow
for flow in hass.config_entries.flow.async_progress()
if flow["flow_id"] == flow_id
)
def _patch_setup():
return patch(
"homeassistant.components.goalzero.async_setup_entry",
return_value=True,
)
async def test_flow_user(hass):
"""Test user initialized flow."""
mocked_yeti = await _create_mocked_yeti()
with _patch_config_flow_yeti(mocked_yeti), _patch_setup():
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_CONFIG_FLOW,
)
assert result["type"] == 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_HOST: "1.2.3.4", CONF_NAME: "Yeti"},
)
entry.add_to_hass(hass)
service_info = {
"host": "1.2.3.4",
"name": "Yeti",
}
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=service_info
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_flow_user_cannot_connect(hass):
"""Test user initialized flow with unreachable server."""
mocked_yeti = await _create_mocked_yeti(True)
with _patch_config_flow_yeti(mocked_yeti) as yetimock:
yetimock.side_effect = exceptions.ConnectError
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
async def test_flow_user_invalid_host(hass):
"""Test user initialized flow with invalid server."""
mocked_yeti = await _create_mocked_yeti(True)
with _patch_config_flow_yeti(mocked_yeti) as yetimock:
yetimock.side_effect = exceptions.InvalidHost
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_host"}
async def test_flow_user_unknown_error(hass):
"""Test user initialized flow with unreachable server."""
mocked_yeti = await _create_mocked_yeti(True)
with _patch_config_flow_yeti(mocked_yeti) as yetimock:
yetimock.side_effect = Exception
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONF_CONFIG_FLOW
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "unknown"}