2020-03-22 01:12:32 +00:00
|
|
|
"""Test the Monoprice 6-Zone Amplifier config flow."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2020-03-22 01:12:32 +00:00
|
|
|
from serial import SerialException
|
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2020-03-22 01:12:32 +00:00
|
|
|
from homeassistant.components.monoprice.const import (
|
|
|
|
CONF_SOURCE_1,
|
|
|
|
CONF_SOURCE_4,
|
|
|
|
CONF_SOURCE_5,
|
|
|
|
CONF_SOURCES,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_PORT
|
2023-02-08 18:06:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-03-22 01:12:32 +00:00
|
|
|
|
2020-03-24 00:32:21 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2020-03-22 01:12:32 +00:00
|
|
|
CONFIG = {
|
|
|
|
CONF_PORT: "/test/port",
|
|
|
|
CONF_SOURCE_1: "one",
|
|
|
|
CONF_SOURCE_4: "four",
|
|
|
|
CONF_SOURCE_5: " ",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_form(hass: HomeAssistant) -> None:
|
2020-03-22 01:12:32 +00:00
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-03-22 01:12:32 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
with patch(
|
2022-07-26 04:24:39 +00:00
|
|
|
"homeassistant.components.monoprice.config_flow.get_monoprice",
|
2020-03-22 01:12:32 +00:00
|
|
|
return_value=True,
|
|
|
|
), patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.monoprice.async_setup_entry",
|
|
|
|
return_value=True,
|
2020-03-22 01:12:32 +00:00
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], CONFIG
|
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-03-22 01:12:32 +00:00
|
|
|
|
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == CONFIG[CONF_PORT]
|
|
|
|
assert result2["data"] == {
|
|
|
|
CONF_PORT: CONFIG[CONF_PORT],
|
2020-03-24 00:32:21 +00:00
|
|
|
CONF_SOURCES: {"1": CONFIG[CONF_SOURCE_1], "4": CONFIG[CONF_SOURCE_4]},
|
2020-03-22 01:12:32 +00:00
|
|
|
}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
2020-03-22 01:12:32 +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(
|
2022-07-26 04:24:39 +00:00
|
|
|
"homeassistant.components.monoprice.config_flow.get_monoprice",
|
2020-03-22 01:12:32 +00:00
|
|
|
side_effect=SerialException,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_generic_exception(hass: HomeAssistant) -> None:
|
2020-03-22 01:12:32 +00:00
|
|
|
"""Test we handle cannot generic exception."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2022-07-26 04:24:39 +00:00
|
|
|
"homeassistant.components.monoprice.config_flow.get_monoprice",
|
2020-03-22 01:12:32 +00:00
|
|
|
side_effect=Exception,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "unknown"}
|
2020-03-24 00:32:21 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:06:59 +00:00
|
|
|
async def test_options_flow(hass: HomeAssistant) -> None:
|
2020-03-24 00:32:21 +00:00
|
|
|
"""Test config flow options."""
|
|
|
|
conf = {CONF_PORT: "/test/port", CONF_SOURCES: {"4": "four"}}
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=conf,
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.monoprice.async_setup_entry", return_value=True
|
|
|
|
):
|
2020-09-05 13:25:22 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-03-24 00:32:21 +00:00
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-03-24 00:32:21 +00:00
|
|
|
assert result["step_id"] == "init"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_SOURCE_1: "one", CONF_SOURCE_4: "", CONF_SOURCE_5: "five"},
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2020-03-24 00:32:21 +00:00
|
|
|
assert config_entry.options[CONF_SOURCES] == {"1": "one", "5": "five"}
|