2019-03-30 13:52:17 +00:00
|
|
|
"""Tests for the Heos config flow module."""
|
2024-03-08 13:50:04 +00:00
|
|
|
|
2019-08-25 18:57:43 +00:00
|
|
|
from pyheos import HeosError
|
2019-03-30 13:52:17 +00:00
|
|
|
|
2020-02-27 11:19:54 +00:00
|
|
|
from homeassistant.components import heos, ssdp
|
2024-12-13 08:46:52 +00:00
|
|
|
from homeassistant.components.heos.const import DOMAIN
|
|
|
|
from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER
|
2019-12-19 17:28:03 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2023-02-08 17:12:56 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-02 21:01:37 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
2023-02-13 11:06:51 +00:00
|
|
|
async def test_flow_aborts_already_setup(hass: HomeAssistant, config_entry) -> None:
|
2019-03-30 13:52:17 +00:00
|
|
|
"""Test flow aborts when entry already setup."""
|
|
|
|
config_entry.add_to_hass(hass)
|
2024-12-13 08:46:52 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2020-10-05 17:22:02 +00:00
|
|
|
assert result["reason"] == "single_instance_allowed"
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
2023-02-08 17:12:56 +00:00
|
|
|
async def test_no_host_shows_form(hass: HomeAssistant) -> None:
|
2019-03-30 13:52:17 +00:00
|
|
|
"""Test form is shown when host not provided."""
|
2024-12-13 08:46:52 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
2023-02-13 11:06:51 +00:00
|
|
|
async def test_cannot_connect_shows_error_form(hass: HomeAssistant, controller) -> None:
|
2019-03-30 13:52:17 +00:00
|
|
|
"""Test form is shown with error when cannot connect."""
|
2019-08-25 18:57:43 +00:00
|
|
|
controller.connect.side_effect = HeosError()
|
2020-02-27 11:19:54 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
heos.DOMAIN, context={"source": SOURCE_USER}, data={CONF_HOST: "127.0.0.1"}
|
2020-02-27 11:19:54 +00:00
|
|
|
)
|
2024-04-02 21:01:37 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-08-25 18:57:43 +00:00
|
|
|
assert result["step_id"] == "user"
|
2020-10-05 17:22:02 +00:00
|
|
|
assert result["errors"][CONF_HOST] == "cannot_connect"
|
2019-08-25 18:57:43 +00:00
|
|
|
assert controller.connect.call_count == 1
|
|
|
|
assert controller.disconnect.call_count == 1
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
2023-02-13 11:06:51 +00:00
|
|
|
async def test_create_entry_when_host_valid(hass: HomeAssistant, controller) -> None:
|
2019-03-30 13:52:17 +00:00
|
|
|
"""Test result type is create entry when host is valid."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {CONF_HOST: "127.0.0.1"}
|
2024-12-13 08:46:52 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
heos.DOMAIN, context={"source": SOURCE_USER}, data=data
|
|
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["result"].unique_id == DOMAIN
|
2024-12-16 16:08:14 +00:00
|
|
|
assert result["title"] == "HEOS System (via 127.0.0.1)"
|
2024-12-13 08:46:52 +00:00
|
|
|
assert result["data"] == data
|
|
|
|
assert controller.connect.call_count == 2 # Also called in async_setup_entry
|
|
|
|
assert controller.disconnect.call_count == 1
|
2019-04-02 20:22:49 +00:00
|
|
|
|
|
|
|
|
2023-02-13 11:06:51 +00:00
|
|
|
async def test_create_entry_when_friendly_name_valid(
|
|
|
|
hass: HomeAssistant, controller
|
|
|
|
) -> None:
|
2019-04-13 21:44:45 +00:00
|
|
|
"""Test result type is create entry when friendly name is valid."""
|
2024-12-13 08:46:52 +00:00
|
|
|
hass.data[DOMAIN] = {"Office (127.0.0.1)": "127.0.0.1"}
|
2019-04-13 21:44:45 +00:00
|
|
|
data = {CONF_HOST: "Office (127.0.0.1)"}
|
2024-12-13 08:46:52 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
heos.DOMAIN, context={"source": SOURCE_USER}, data=data
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["result"].unique_id == DOMAIN
|
2024-12-16 16:08:14 +00:00
|
|
|
assert result["title"] == "HEOS System (via 127.0.0.1)"
|
2024-12-13 08:46:52 +00:00
|
|
|
assert result["data"] == {CONF_HOST: "127.0.0.1"}
|
|
|
|
assert controller.connect.call_count == 2 # Also called in async_setup_entry
|
|
|
|
assert controller.disconnect.call_count == 1
|
|
|
|
assert DOMAIN not in hass.data
|
2019-04-13 21:44:45 +00:00
|
|
|
|
|
|
|
|
2021-11-25 13:35:19 +00:00
|
|
|
async def test_discovery_shows_create_form(
|
2024-12-13 08:46:52 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
controller,
|
|
|
|
discovery_data: ssdp.SsdpServiceInfo,
|
|
|
|
discovery_data_bedroom: ssdp.SsdpServiceInfo,
|
2023-02-13 11:06:51 +00:00
|
|
|
) -> None:
|
2024-12-13 08:46:52 +00:00
|
|
|
"""Test discovery shows form to confirm setup."""
|
2020-05-26 15:51:50 +00:00
|
|
|
|
2024-12-13 08:46:52 +00:00
|
|
|
# Single discovered host shows form for user to finish setup.
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
heos.DOMAIN, context={"source": SOURCE_SSDP}, data=discovery_data
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2024-12-13 08:46:52 +00:00
|
|
|
assert hass.data[DOMAIN] == {"Office (127.0.0.1)": "127.0.0.1"}
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
2020-05-26 15:51:50 +00:00
|
|
|
|
2024-12-13 08:46:52 +00:00
|
|
|
# Subsequent discovered hosts append to discovered hosts and abort.
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
heos.DOMAIN, context={"source": SOURCE_SSDP}, data=discovery_data_bedroom
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2024-12-13 08:46:52 +00:00
|
|
|
assert hass.data[DOMAIN] == {
|
2019-04-13 21:44:45 +00:00
|
|
|
"Office (127.0.0.1)": "127.0.0.1",
|
2019-07-31 19:25:30 +00:00
|
|
|
"Bedroom (127.0.0.2)": "127.0.0.2",
|
2019-04-13 21:44:45 +00:00
|
|
|
}
|
2024-12-13 08:46:52 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "already_in_progress"
|
2019-04-13 21:44:45 +00:00
|
|
|
|
|
|
|
|
2020-05-26 15:51:50 +00:00
|
|
|
async def test_discovery_flow_aborts_already_setup(
|
2023-02-13 11:06:51 +00:00
|
|
|
hass: HomeAssistant, controller, discovery_data: ssdp.SsdpServiceInfo, config_entry
|
|
|
|
) -> None:
|
2019-04-13 21:44:45 +00:00
|
|
|
"""Test discovery flow aborts when entry already setup."""
|
|
|
|
config_entry.add_to_hass(hass)
|
2020-05-26 15:51:50 +00:00
|
|
|
|
2024-12-13 08:46:52 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_SSDP}, data=discovery_data
|
2020-05-26 15:51:50 +00:00
|
|
|
)
|
2024-12-13 08:46:52 +00:00
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "single_instance_allowed"
|
2024-12-16 16:08:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_reconfigure_validates_and_updates_config(
|
|
|
|
hass: HomeAssistant, config_entry, controller
|
|
|
|
) -> None:
|
|
|
|
"""Test reconfigure validates host and successfully updates."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await config_entry.start_reconfigure_flow(hass)
|
|
|
|
assert config_entry.data[CONF_HOST] == "127.0.0.1"
|
|
|
|
|
|
|
|
# Test reconfigure initially shows form with current host value.
|
|
|
|
host = next(
|
|
|
|
key.default() for key in result["data_schema"].schema if key == CONF_HOST
|
|
|
|
)
|
|
|
|
assert host == "127.0.0.1"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
assert result["step_id"] == "reconfigure"
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
|
|
|
|
# Test reconfigure successfully updates.
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_HOST: "127.0.0.2"},
|
|
|
|
)
|
|
|
|
assert controller.connect.call_count == 2 # Also called when entry reloaded
|
|
|
|
assert controller.disconnect.call_count == 1
|
|
|
|
assert config_entry.data == {CONF_HOST: "127.0.0.2"}
|
|
|
|
assert config_entry.unique_id == DOMAIN
|
|
|
|
assert result["reason"] == "reconfigure_successful"
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reconfigure_cannot_connect_recovers(
|
|
|
|
hass: HomeAssistant, config_entry, controller
|
|
|
|
) -> None:
|
|
|
|
"""Test reconfigure cannot connect and recovers."""
|
|
|
|
controller.connect.side_effect = HeosError()
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
result = await config_entry.start_reconfigure_flow(hass)
|
|
|
|
assert config_entry.data[CONF_HOST] == "127.0.0.1"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_HOST: "127.0.0.2"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert controller.connect.call_count == 1
|
|
|
|
assert controller.disconnect.call_count == 1
|
|
|
|
host = next(
|
|
|
|
key.default() for key in result["data_schema"].schema if key == CONF_HOST
|
|
|
|
)
|
|
|
|
assert host == "127.0.0.2"
|
|
|
|
assert result["errors"][CONF_HOST] == "cannot_connect"
|
|
|
|
assert result["step_id"] == "reconfigure"
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
|
|
|
|
# Test reconfigure recovers and successfully updates.
|
|
|
|
controller.connect.side_effect = None
|
|
|
|
controller.connect.reset_mock()
|
|
|
|
controller.disconnect.reset_mock()
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_HOST: "127.0.0.2"},
|
|
|
|
)
|
|
|
|
assert controller.connect.call_count == 2 # Also called when entry reloaded
|
|
|
|
assert controller.disconnect.call_count == 1
|
|
|
|
assert config_entry.data == {CONF_HOST: "127.0.0.2"}
|
|
|
|
assert config_entry.unique_id == DOMAIN
|
|
|
|
assert result["reason"] == "reconfigure_successful"
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|