ConfigFlow default discovery without unique ID (#36754)
This commit is contained in:
1 parent
dfac9c5e03
commit
3cc94f7d6a
32 files changed
+408
-170
No files matched your search
@@ -82,7 +82,7 @@ async def test_abort_if_existing_entry(hass):
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_setup"
|
||||
|
||||
result = await flow.async_step_hassio()
|
||||
result = await flow.async_step_hassio({})
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_setup"
|
||||
|
||||
|
||||
@@ -264,10 +264,10 @@ async def test_zeroconf_with_uuid_device_exists_abort(
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_zeroconf_empty_unique_id_required_abort(
|
||||
async def test_zeroconf_empty_unique_id(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort zeroconf flow if printer lacks (empty) unique identification."""
|
||||
"""Test zeroconf flow if printer lacks (empty) unique identification."""
|
||||
mock_connection(aioclient_mock, no_unique_id=True)
|
||||
|
||||
discovery_info = {
|
||||
@@ -278,14 +278,13 @@ async def test_zeroconf_empty_unique_id_required_abort(
|
||||
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "unique_id_required"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
|
||||
|
||||
async def test_zeroconf_unique_id_required_abort(
|
||||
async def test_zeroconf_no_unique_id(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort zeroconf flow if printer lacks unique identification."""
|
||||
"""Test zeroconf flow if printer lacks unique identification."""
|
||||
mock_connection(aioclient_mock, no_unique_id=True)
|
||||
|
||||
discovery_info = MOCK_ZEROCONF_IPP_SERVICE_INFO.copy()
|
||||
@@ -293,8 +292,7 @@ async def test_zeroconf_unique_id_required_abort(
|
||||
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info,
|
||||
)
|
||||
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "unique_id_required"
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
|
||||
|
||||
async def test_full_user_flow_implementation(
|
||||
|
||||
@@ -13,6 +13,7 @@ from homeassistant.components.tellduslive import (
|
||||
SCAN_INTERVAL,
|
||||
config_flow,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_DISCOVERY
|
||||
from homeassistant.const import CONF_HOST
|
||||
|
||||
from tests.common import MockConfigEntry, mock_coro
|
||||
@@ -73,6 +74,7 @@ async def test_abort_if_already_setup(hass):
|
||||
async def test_full_flow_implementation(hass, mock_tellduslive):
|
||||
"""Test registering an implementation and finishing flow works."""
|
||||
flow = init_config_flow(hass)
|
||||
flow.context = {"source": SOURCE_DISCOVERY}
|
||||
result = await flow.async_step_discovery(["localhost", "tellstick"])
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
@@ -166,6 +168,7 @@ async def test_step_import_load_json(hass, mock_tellduslive):
|
||||
async def test_step_disco_no_local_api(hass, mock_tellduslive):
|
||||
"""Test that we trigger when configuring from discovery, not supporting local api."""
|
||||
flow = init_config_flow(hass)
|
||||
flow.context = {"source": SOURCE_DISCOVERY}
|
||||
|
||||
result = await flow.async_step_discovery(["localhost", "tellstick"])
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
@@ -242,7 +245,7 @@ async def test_discovery_already_configured(hass, mock_tellduslive):
|
||||
"""Test abort if already configured fires from discovery."""
|
||||
MockConfigEntry(domain="tellduslive", data={"host": "some-host"}).add_to_hass(hass)
|
||||
flow = init_config_flow(hass)
|
||||
flow.context = {"source": SOURCE_DISCOVERY}
|
||||
|
||||
result = await flow.async_step_discovery(["some-host", ""])
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_setup"
|
||||
with pytest.raises(data_entry_flow.AbortFlow):
|
||||
result = await flow.async_step_discovery(["some-host", ""])
|
||||
@@ -554,13 +554,15 @@ async def test_discovery_notification(hass):
|
||||
|
||||
VERSION = 5
|
||||
|
||||
async def async_step_discovery(self, user_input=None):
|
||||
async def async_step_discovery(self, discovery_info):
|
||||
"""Test discovery step."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
title="Test Title", data={"token": "abcd"}
|
||||
)
|
||||
return self.async_show_form(step_id="discovery")
|
||||
return self.async_show_form(step_id="discovery_confirm")
|
||||
|
||||
async def async_step_discovery_confirm(self, discovery_info):
|
||||
"""Test discovery confirm step."""
|
||||
return self.async_create_entry(
|
||||
title="Test Title", data={"token": "abcd"}
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"test", context={"source": config_entries.SOURCE_DISCOVERY}
|
||||
@@ -589,7 +591,7 @@ async def test_discovery_notification_not_created(hass):
|
||||
|
||||
VERSION = 5
|
||||
|
||||
async def async_step_discovery(self, user_input=None):
|
||||
async def async_step_discovery(self, discovery_info):
|
||||
"""Test discovery step."""
|
||||
return self.async_abort(reason="test")
|
||||
|
||||
@@ -1447,7 +1449,7 @@ async def test_partial_flows_hidden(hass, manager):
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_discovery(self, user_input):
|
||||
async def async_step_discovery(self, discovery_info):
|
||||
"""Test discovery step."""
|
||||
discovery_started.set()
|
||||
await pause_discovery.wait()
|
||||
@@ -1577,3 +1579,182 @@ async def test_async_setup_update_entry(hass):
|
||||
assert len(entries) == 1
|
||||
assert entries[0].state == config_entries.ENTRY_STATE_LOADED
|
||||
assert entries[0].data == {"value": "updated"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"discovery_source",
|
||||
(
|
||||
config_entries.SOURCE_DISCOVERY,
|
||||
config_entries.SOURCE_SSDP,
|
||||
config_entries.SOURCE_HOMEKIT,
|
||||
config_entries.SOURCE_ZEROCONF,
|
||||
config_entries.SOURCE_HASSIO,
|
||||
),
|
||||
)
|
||||
async def test_flow_with_default_discovery(hass, manager, discovery_source):
|
||||
"""Test that finishing a default discovery flow removes the unique ID in the entry."""
|
||||
mock_integration(
|
||||
hass, MockModule("comp", async_setup_entry=AsyncMock(return_value=True)),
|
||||
)
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Test user step."""
|
||||
if user_input is None:
|
||||
return self.async_show_form(step_id="user")
|
||||
|
||||
return self.async_create_entry(title="yo", data={})
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
# Create one to be in progress
|
||||
result = await manager.flow.async_init(
|
||||
"comp", context={"source": discovery_source}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert (
|
||||
flows[0]["context"]["unique_id"]
|
||||
== config_entries.DEFAULT_DISCOVERY_UNIQUE_ID
|
||||
)
|
||||
|
||||
# Finish flow
|
||||
result2 = await manager.flow.async_configure(
|
||||
result["flow_id"], user_input={"fake": "data"}
|
||||
)
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
|
||||
assert len(hass.config_entries.flow.async_progress()) == 0
|
||||
|
||||
entry = hass.config_entries.async_entries("comp")[0]
|
||||
assert entry.title == "yo"
|
||||
assert entry.source == discovery_source
|
||||
assert entry.unique_id is None
|
||||
|
||||
|
||||
async def test_flow_with_default_discovery_with_unique_id(hass, manager):
|
||||
"""Test discovery flow using the default discovery is ignored when unique ID is set."""
|
||||
mock_integration(hass, MockModule("comp"))
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_discovery(self, discovery_info):
|
||||
"""Test discovery step."""
|
||||
await self.async_set_unique_id("mock-unique-id")
|
||||
# This call should make no difference, as a unique ID is set
|
||||
await self._async_handle_discovery_without_unique_id()
|
||||
return self.async_show_form(step_id="mock")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
result = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_DISCOVERY}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
|
||||
|
||||
|
||||
async def test_default_discovery_abort_existing_entries(hass, manager):
|
||||
"""Test that a flow without discovery implementation aborts when a config entry exists."""
|
||||
hass.config.components.add("comp")
|
||||
entry = MockConfigEntry(domain="comp", data={}, unique_id="mock-unique-id")
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
mock_integration(hass, MockModule("comp"))
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
result = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_DISCOVERY}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_default_discovery_in_progress(hass, manager):
|
||||
"""Test that a flow using default discovery can only be triggered once."""
|
||||
mock_integration(hass, MockModule("comp"))
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_discovery(self, discovery_info):
|
||||
"""Test discovery step."""
|
||||
await self.async_set_unique_id(discovery_info.get("unique_id"))
|
||||
await self._async_handle_discovery_without_unique_id()
|
||||
return self.async_show_form(step_id="mock")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
result = await manager.flow.async_init(
|
||||
"comp",
|
||||
context={"source": config_entries.SOURCE_DISCOVERY},
|
||||
data={"unique_id": "mock-unique-id"},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
# Second discovery without a unique ID
|
||||
result2 = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
|
||||
)
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
|
||||
|
||||
|
||||
async def test_default_discovery_abort_on_new_unique_flow(hass, manager):
|
||||
"""Test that a flow using default discovery is aborted when a second flow with unique ID is created."""
|
||||
mock_integration(hass, MockModule("comp"))
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_discovery(self, discovery_info):
|
||||
"""Test discovery step."""
|
||||
await self.async_set_unique_id(discovery_info.get("unique_id"))
|
||||
await self._async_handle_discovery_without_unique_id()
|
||||
return self.async_show_form(step_id="mock")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
# First discovery with default, no unique ID
|
||||
result2 = await manager.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
|
||||
)
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
# Second discovery brings in a unique ID
|
||||
result = await manager.flow.async_init(
|
||||
"comp",
|
||||
context={"source": config_entries.SOURCE_DISCOVERY},
|
||||
data={"unique_id": "mock-unique-id"},
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
# Ensure the first one is cancelled and we end up with just the last one
|
||||
flows = hass.config_entries.flow.async_progress()
|
||||
assert len(flows) == 1
|
||||
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
|
||||
Reference in new issue
Block a user