core/tests/components/balboa/test_config_flow.py

137 lines
4.5 KiB
Python
Raw Normal View History

2021-11-25 18:04:06 +00:00
"""Test the Balboa Spa Client config flow."""
2021-12-13 16:11:21 +00:00
from unittest.mock import MagicMock, patch
2021-11-25 18:04:06 +00:00
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.balboa.const import CONF_SYNC_TIME, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from tests.common import MockConfigEntry
TEST_DATA = {
CONF_HOST: "1.1.1.1",
}
TEST_ID = "FakeBalboa"
2021-12-13 16:11:21 +00:00
async def test_form(hass: HomeAssistant, client: MagicMock) -> None:
2021-11-25 18:04:06 +00:00
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["errors"] == {}
with patch(
2021-12-13 16:11:21 +00:00
"homeassistant.components.balboa.config_flow.BalboaSpaWifi",
return_value=client,
2021-11-25 18:04:06 +00:00
), patch(
"homeassistant.components.balboa.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_CREATE_ENTRY
assert result2["data"] == TEST_DATA
assert len(mock_setup_entry.mock_calls) == 1
2021-12-13 16:11:21 +00:00
async def test_form_cannot_connect(hass: HomeAssistant, client: MagicMock) -> None:
2021-11-25 18:04:06 +00:00
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
2021-12-13 16:11:21 +00:00
"homeassistant.components.balboa.config_flow.BalboaSpaWifi",
return_value=client,
2021-11-25 18:04:06 +00:00
):
2021-12-13 16:11:21 +00:00
client.connect.return_value = False
2021-11-25 18:04:06 +00:00
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "cannot_connect"}
2021-12-13 16:11:21 +00:00
async def test_unknown_error(hass: HomeAssistant, client: MagicMock) -> None:
2021-11-25 18:04:06 +00:00
"""Test we handle unknown error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
2021-12-13 16:11:21 +00:00
"homeassistant.components.balboa.config_flow.BalboaSpaWifi",
return_value=client,
2021-11-25 18:04:06 +00:00
):
2021-12-13 16:11:21 +00:00
client.connect.side_effect = Exception("Boom")
2021-11-25 18:04:06 +00:00
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["errors"] == {"base": "unknown"}
2021-12-13 16:11:21 +00:00
async def test_already_configured(hass: HomeAssistant, client: MagicMock) -> None:
2021-11-25 18:04:06 +00:00
"""Test when provided credentials are already configured."""
MockConfigEntry(domain=DOMAIN, data=TEST_DATA, unique_id=TEST_ID).add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == SOURCE_USER
with patch(
2021-12-13 16:11:21 +00:00
"homeassistant.components.balboa.config_flow.BalboaSpaWifi",
return_value=client,
2021-11-25 18:04:06 +00:00
), patch(
"homeassistant.components.balboa.async_setup_entry",
return_value=True,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
TEST_DATA,
)
await hass.async_block_till_done()
assert result2["type"] == RESULT_TYPE_ABORT
assert result2["reason"] == "already_configured"
2021-12-13 16:11:21 +00:00
async def test_options_flow(hass: HomeAssistant, client: MagicMock) -> None:
2021-11-25 18:04:06 +00:00
"""Test specifying non default settings using options flow."""
config_entry = MockConfigEntry(domain=DOMAIN, data=TEST_DATA, unique_id=TEST_ID)
config_entry.add_to_hass(hass)
2021-12-13 16:11:21 +00:00
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
2021-11-25 18:04:06 +00:00
2021-12-13 16:11:21 +00:00
result = await hass.config_entries.options.async_init(config_entry.entry_id)
2021-11-25 18:04:06 +00:00
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_SYNC_TIME: True},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
2021-12-13 16:11:21 +00:00
assert dict(config_entry.options) == {CONF_SYNC_TIME: True}