core/tests/components/heos/test_config_flow.py

460 lines
17 KiB
Python
Raw Normal View History

"""Tests for the Heos config flow module."""
2025-01-12 05:06:06 +00:00
from pyheos import CommandAuthenticationError, CommandFailedError, HeosError
import pytest
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
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
2025-01-03 18:11:10 +00:00
from tests.common import MockConfigEntry
async def test_flow_aborts_already_setup(hass: HomeAssistant, config_entry) -> None:
"""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}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"
async def test_no_host_shows_form(hass: HomeAssistant) -> None:
"""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}
)
assert result["type"] is FlowResultType.FORM
2019-07-31 19:25:30 +00:00
assert result["step_id"] == "user"
assert result["errors"] == {}
async def test_cannot_connect_shows_error_form(hass: HomeAssistant, controller) -> None:
"""Test form is shown with error when cannot connect."""
2019-08-25 18:57:43 +00:00
controller.connect.side_effect = HeosError()
result = await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": SOURCE_USER}, data={CONF_HOST: "127.0.0.1"}
)
assert result["type"] is FlowResultType.FORM
2019-08-25 18:57:43 +00:00
assert result["step_id"] == "user"
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
async def test_create_entry_when_host_valid(hass: HomeAssistant, controller) -> None:
"""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
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
async def test_create_entry_when_friendly_name_valid(
hass: HomeAssistant, controller
) -> None:
"""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"}
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
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
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,
) -> None:
2024-12-13 08:46:52 +00:00
"""Test discovery shows form to confirm setup."""
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(
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"
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] == {
"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",
}
2024-12-13 08:46:52 +00:00
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_in_progress"
async def test_discovery_flow_aborts_already_setup(
hass: HomeAssistant, controller, discovery_data: ssdp.SsdpServiceInfo, config_entry
) -> None:
"""Test discovery 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_SSDP}, data=discovery_data
)
2024-12-13 08:46:52 +00:00
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"
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
@pytest.mark.parametrize(
("error", "expected_error_key"),
[
(
2025-01-12 05:06:06 +00:00
CommandAuthenticationError("sign_in", "Invalid credentials", 6),
"invalid_auth",
),
(CommandFailedError("sign_in", "System error", 12), "unknown"),
(HeosError(), "unknown"),
],
)
async def test_options_flow_signs_in(
hass: HomeAssistant,
config_entry,
controller,
error: HeosError,
expected_error_key: str,
) -> None:
"""Test options flow signs-in with entered credentials."""
config_entry.add_to_hass(hass)
# Start the options flow. Entry has not current options.
assert CONF_USERNAME not in config_entry.options
assert CONF_PASSWORD not in config_entry.options
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["step_id"] == "init"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Invalid credentials, system error, or unexpected error.
user_input = {CONF_USERNAME: "user", CONF_PASSWORD: "pass"}
controller.sign_in.side_effect = error
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert result["step_id"] == "init"
assert result["errors"] == {"base": expected_error_key}
assert result["type"] is FlowResultType.FORM
# Valid credentials signs-in and creates entry
controller.sign_in.reset_mock()
controller.sign_in.side_effect = None
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert result["data"] == user_input
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_options_flow_signs_out(
hass: HomeAssistant, config_entry, controller
) -> None:
"""Test options flow signs-out when credentials cleared."""
config_entry.add_to_hass(hass)
# Start the options flow. Entry has not current options.
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["step_id"] == "init"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Fail to sign-out, show error
user_input = {}
controller.sign_out.side_effect = HeosError()
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 0
assert controller.sign_out.call_count == 1
assert result["step_id"] == "init"
assert result["errors"] == {"base": "unknown"}
assert result["type"] is FlowResultType.FORM
# Clear credentials
controller.sign_out.reset_mock()
controller.sign_out.side_effect = None
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 0
assert controller.sign_out.call_count == 1
assert result["data"] == user_input
assert result["type"] is FlowResultType.CREATE_ENTRY
@pytest.mark.parametrize(
("user_input", "expected_errors"),
[
({CONF_USERNAME: "user"}, {CONF_PASSWORD: "password_missing"}),
({CONF_PASSWORD: "pass"}, {CONF_USERNAME: "username_missing"}),
],
)
async def test_options_flow_missing_one_param_recovers(
hass: HomeAssistant,
config_entry,
controller,
user_input: dict[str, str],
expected_errors: dict[str, str],
) -> None:
"""Test options flow signs-in after recovering from only username or password being entered."""
config_entry.add_to_hass(hass)
# Start the options flow. Entry has not current options.
assert CONF_USERNAME not in config_entry.options
assert CONF_PASSWORD not in config_entry.options
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["step_id"] == "init"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Enter only username or password
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input
)
assert result["step_id"] == "init"
assert result["errors"] == expected_errors
assert result["type"] is FlowResultType.FORM
# Enter valid credentials
user_input = {CONF_USERNAME: "user", CONF_PASSWORD: "pass"}
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert result["data"] == user_input
assert result["type"] is FlowResultType.CREATE_ENTRY
2025-01-03 18:11:10 +00:00
@pytest.mark.parametrize(
("error", "expected_error_key"),
[
(
2025-01-12 05:06:06 +00:00
CommandAuthenticationError("sign_in", "Invalid credentials", 6),
2025-01-03 18:11:10 +00:00
"invalid_auth",
),
(CommandFailedError("sign_in", "System error", 12), "unknown"),
(HeosError(), "unknown"),
],
)
async def test_reauth_signs_in_aborts(
hass: HomeAssistant,
config_entry: MockConfigEntry,
controller,
error: HeosError,
expected_error_key: str,
) -> None:
"""Test reauth flow signs-in with entered credentials and aborts."""
config_entry.add_to_hass(hass)
result = await config_entry.start_reauth_flow(hass)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Invalid credentials, system error, or unexpected error.
user_input = {CONF_USERNAME: "user", CONF_PASSWORD: "pass"}
controller.sign_in.side_effect = error
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": expected_error_key}
assert result["type"] is FlowResultType.FORM
# Valid credentials signs-in, updates options, and aborts
controller.sign_in.reset_mock()
controller.sign_in.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert config_entry.options[CONF_USERNAME] == user_input[CONF_USERNAME]
assert config_entry.options[CONF_PASSWORD] == user_input[CONF_PASSWORD]
assert result["reason"] == "reauth_successful"
assert result["type"] is FlowResultType.ABORT
async def test_reauth_signs_out(hass: HomeAssistant, config_entry, controller) -> None:
"""Test reauth flow signs-out when credentials cleared and aborts."""
config_entry.add_to_hass(hass)
result = await config_entry.start_reauth_flow(hass)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Fail to sign-out, show error
user_input = {}
controller.sign_out.side_effect = HeosError()
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 0
assert controller.sign_out.call_count == 1
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {"base": "unknown"}
assert result["type"] is FlowResultType.FORM
# Cleared credentials signs-out, updates options, and aborts
controller.sign_out.reset_mock()
controller.sign_out.side_effect = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 0
assert controller.sign_out.call_count == 1
assert CONF_USERNAME not in config_entry.options
assert CONF_PASSWORD not in config_entry.options
assert result["reason"] == "reauth_successful"
assert result["type"] is FlowResultType.ABORT
@pytest.mark.parametrize(
("user_input", "expected_errors"),
[
({CONF_USERNAME: "user"}, {CONF_PASSWORD: "password_missing"}),
({CONF_PASSWORD: "pass"}, {CONF_USERNAME: "username_missing"}),
],
)
async def test_reauth_flow_missing_one_param_recovers(
hass: HomeAssistant,
config_entry,
controller,
user_input: dict[str, str],
expected_errors: dict[str, str],
) -> None:
"""Test reauth flow signs-in after recovering from only username or password being entered."""
config_entry.add_to_hass(hass)
# Start the options flow. Entry has not current options.
result = await config_entry.start_reauth_flow(hass)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == {}
assert result["type"] is FlowResultType.FORM
# Enter only username or password
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert result["step_id"] == "reauth_confirm"
assert result["errors"] == expected_errors
assert result["type"] is FlowResultType.FORM
# Enter valid credentials
user_input = {CONF_USERNAME: "user", CONF_PASSWORD: "pass"}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input
)
assert controller.sign_in.call_count == 1
assert controller.sign_out.call_count == 0
assert config_entry.options[CONF_USERNAME] == user_input[CONF_USERNAME]
assert config_entry.options[CONF_PASSWORD] == user_input[CONF_PASSWORD]
assert result["reason"] == "reauth_successful"
assert result["type"] is FlowResultType.ABORT