2019-03-30 13:52:17 +00:00
|
|
|
"""Tests for the Heos config flow module."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2019-12-19 17:28:03 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2019-08-25 18:57:43 +00:00
|
|
|
from pyheos import HeosError
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
2020-02-27 11:19:54 +00:00
|
|
|
from homeassistant.components import heos, ssdp
|
2019-03-30 13:52:17 +00:00
|
|
|
from homeassistant.components.heos.config_flow import HeosFlowHandler
|
2020-05-26 15:51:50 +00:00
|
|
|
from homeassistant.components.heos.const import DATA_DISCOVERED_HOSTS, DOMAIN
|
2021-04-25 09:27:40 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_SSDP, SOURCE_USER
|
2019-12-19 17:28:03 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_aborts_already_setup(hass, config_entry):
|
|
|
|
"""Test flow aborts when entry already setup."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
flow = HeosFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-10-05 17:22:02 +00:00
|
|
|
assert result["reason"] == "single_instance_allowed"
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_no_host_shows_form(hass):
|
|
|
|
"""Test form is shown when host not provided."""
|
|
|
|
flow = HeosFlowHandler()
|
|
|
|
flow.hass = hass
|
|
|
|
result = await flow.async_step_user()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_cannot_connect_shows_error_form(hass, controller):
|
|
|
|
"""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
|
|
|
)
|
2019-08-25 18:57:43 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
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
|
|
|
|
controller.connect.reset_mock()
|
|
|
|
controller.disconnect.reset_mock()
|
2019-03-30 13:52:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_create_entry_when_host_valid(hass, controller):
|
|
|
|
"""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"}
|
2020-02-27 11:19:54 +00:00
|
|
|
with patch("homeassistant.components.heos.async_setup_entry", return_value=True):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
heos.DOMAIN, context={"source": SOURCE_USER}, data=data
|
2020-02-27 11:19:54 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-05-26 15:51:50 +00:00
|
|
|
assert result["result"].unique_id == DOMAIN
|
2020-02-27 11:19:54 +00:00
|
|
|
assert result["title"] == "Controller (127.0.0.1)"
|
|
|
|
assert result["data"] == data
|
|
|
|
assert controller.connect.call_count == 1
|
|
|
|
assert controller.disconnect.call_count == 1
|
2019-04-02 20:22:49 +00:00
|
|
|
|
|
|
|
|
2019-04-13 21:44:45 +00:00
|
|
|
async def test_create_entry_when_friendly_name_valid(hass, controller):
|
|
|
|
"""Test result type is create entry when friendly name is valid."""
|
|
|
|
hass.data[DATA_DISCOVERED_HOSTS] = {"Office (127.0.0.1)": "127.0.0.1"}
|
|
|
|
data = {CONF_HOST: "Office (127.0.0.1)"}
|
2020-02-27 11:19:54 +00:00
|
|
|
with patch("homeassistant.components.heos.async_setup_entry", return_value=True):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
heos.DOMAIN, context={"source": SOURCE_USER}, data=data
|
2020-02-27 11:19:54 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2020-05-26 15:51:50 +00:00
|
|
|
assert result["result"].unique_id == DOMAIN
|
2020-02-27 11:19:54 +00:00
|
|
|
assert result["title"] == "Controller (127.0.0.1)"
|
|
|
|
assert result["data"] == {CONF_HOST: "127.0.0.1"}
|
|
|
|
assert controller.connect.call_count == 1
|
|
|
|
assert controller.disconnect.call_count == 1
|
|
|
|
assert DATA_DISCOVERED_HOSTS not in hass.data
|
2019-04-13 21:44:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_shows_create_form(hass, controller, discovery_data):
|
|
|
|
"""Test discovery shows form to confirm setup and subsequent abort."""
|
2020-05-26 15:51:50 +00:00
|
|
|
|
2019-04-09 02:24:40 +00:00
|
|
|
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
|
|
|
)
|
2019-04-09 02:24:40 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-26 15:51:50 +00:00
|
|
|
flows_in_progress = hass.config_entries.flow.async_progress()
|
|
|
|
assert flows_in_progress[0]["context"]["unique_id"] == DOMAIN
|
|
|
|
assert len(flows_in_progress) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.data[DATA_DISCOVERED_HOSTS] == {"Office (127.0.0.1)": "127.0.0.1"}
|
2019-04-09 02:24:40 +00:00
|
|
|
|
2019-12-19 17:28:03 +00:00
|
|
|
port = urlparse(discovery_data[ssdp.ATTR_SSDP_LOCATION]).port
|
|
|
|
discovery_data[ssdp.ATTR_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
|
|
|
|
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
|
2020-05-26 15:51:50 +00:00
|
|
|
|
2019-04-09 02:24:40 +00:00
|
|
|
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
|
|
|
)
|
2019-04-09 02:24:40 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-05-26 15:51:50 +00:00
|
|
|
flows_in_progress = hass.config_entries.flow.async_progress()
|
|
|
|
assert flows_in_progress[0]["context"]["unique_id"] == DOMAIN
|
|
|
|
assert len(flows_in_progress) == 1
|
2019-04-13 21:44:45 +00:00
|
|
|
assert hass.data[DATA_DISCOVERED_HOSTS] == {
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-26 15:51:50 +00:00
|
|
|
async def test_discovery_flow_aborts_already_setup(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, controller, discovery_data, config_entry
|
|
|
|
):
|
2019-04-13 21:44:45 +00:00
|
|
|
"""Test discovery flow aborts when entry already setup."""
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
flow = HeosFlowHandler()
|
|
|
|
flow.hass = hass
|
2019-06-12 23:08:08 +00:00
|
|
|
result = await flow.async_step_ssdp(discovery_data)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2020-10-05 17:22:02 +00:00
|
|
|
assert result["reason"] == "single_instance_allowed"
|
2020-05-26 15:51:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_sets_the_unique_id(hass, controller, discovery_data):
|
|
|
|
"""Test discovery sets the unique id."""
|
|
|
|
|
|
|
|
port = urlparse(discovery_data[ssdp.ATTR_SSDP_LOCATION]).port
|
|
|
|
discovery_data[ssdp.ATTR_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
|
|
|
|
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
|
|
|
|
|
|
|
|
await hass.config_entries.flow.async_init(
|
|
|
|
heos.DOMAIN, context={"source": SOURCE_SSDP}, data=discovery_data
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
flows_in_progress = hass.config_entries.flow.async_progress()
|
|
|
|
assert flows_in_progress[0]["context"]["unique_id"] == DOMAIN
|
|
|
|
assert len(flows_in_progress) == 1
|
|
|
|
assert hass.data[DATA_DISCOVERED_HOSTS] == {"Bedroom (127.0.0.2)": "127.0.0.2"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_import_sets_the_unique_id(hass, controller):
|
|
|
|
"""Test import sets the unique id."""
|
|
|
|
|
|
|
|
with patch("homeassistant.components.heos.async_setup_entry", return_value=True):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
heos.DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_HOST: "127.0.0.2"},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
assert result["result"].unique_id == DOMAIN
|