Allow ignoring discovery config flow helper (#34740)

This commit is contained in:
Paulus Schoutsen 2020-04-26 23:35:04 -07:00 committed by GitHub
parent a643d6cd3e
commit cc14dfa31c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 5 deletions

View File

@ -33,6 +33,8 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow):
if self._async_current_entries(): if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed") return self.async_abort(reason="single_instance_allowed")
await self.async_set_unique_id(self._domain, raise_on_progress=False)
return await self.async_step_confirm() return await self.async_step_confirm()
async def async_step_confirm(self, user_input=None): async def async_step_confirm(self, user_input=None):
@ -40,10 +42,7 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow):
if user_input is None: if user_input is None:
return self.async_show_form(step_id="confirm") return self.async_show_form(step_id="confirm")
if ( # pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167 if self.source == config_entries.SOURCE_USER:
self.context
and self.context.get("source") != config_entries.SOURCE_DISCOVERY
):
# Get current discovered entries. # Get current discovered entries.
in_progress = self._async_in_progress() in_progress = self._async_in_progress()
@ -67,6 +66,8 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow):
if self._async_in_progress() or self._async_current_entries(): if self._async_in_progress() or self._async_current_entries():
return self.async_abort(reason="single_instance_allowed") return self.async_abort(reason="single_instance_allowed")
await self.async_set_unique_id(self._domain)
return await self.async_step_confirm() return await self.async_step_confirm()
async_step_zeroconf = async_step_discovery async_step_zeroconf = async_step_discovery

View File

@ -44,6 +44,7 @@ async def test_single_entry_allowed(hass, discovery_flow_conf):
"""Test only a single entry is allowed.""" """Test only a single entry is allowed."""
flow = config_entries.HANDLERS["test"]() flow = config_entries.HANDLERS["test"]()
flow.hass = hass flow.hass = hass
flow.context = {}
MockConfigEntry(domain="test").add_to_hass(hass) MockConfigEntry(domain="test").add_to_hass(hass)
result = await flow.async_step_user() result = await flow.async_step_user()
@ -67,6 +68,7 @@ async def test_user_has_confirmation(hass, discovery_flow_conf):
"""Test user requires no confirmation to setup.""" """Test user requires no confirmation to setup."""
flow = config_entries.HANDLERS["test"]() flow = config_entries.HANDLERS["test"]()
flow.hass = hass flow.hass = hass
flow.context = {}
discovery_flow_conf["discovered"] = True discovery_flow_conf["discovered"] = True
result = await flow.async_step_user() result = await flow.async_step_user()
@ -93,7 +95,7 @@ async def test_discovery_confirmation(hass, discovery_flow_conf, source):
"""Test we ask for confirmation via discovery.""" """Test we ask for confirmation via discovery."""
flow = config_entries.HANDLERS["test"]() flow = config_entries.HANDLERS["test"]()
flow.hass = hass flow.hass = hass
flow.context = {} flow.context = {"source": source}
result = await getattr(flow, f"async_step_{source}")({}) result = await getattr(flow, f"async_step_{source}")({})
@ -150,6 +152,7 @@ async def test_import_no_confirmation(hass, discovery_flow_conf):
"""Test import requires no confirmation to set up.""" """Test import requires no confirmation to set up."""
flow = config_entries.HANDLERS["test"]() flow = config_entries.HANDLERS["test"]()
flow.hass = hass flow.hass = hass
flow.context = {}
discovery_flow_conf["discovered"] = True discovery_flow_conf["discovered"] = True
result = await flow.async_step_import(None) result = await flow.async_step_import(None)
@ -160,6 +163,7 @@ async def test_import_single_instance(hass, discovery_flow_conf):
"""Test import doesn't create second instance.""" """Test import doesn't create second instance."""
flow = config_entries.HANDLERS["test"]() flow = config_entries.HANDLERS["test"]()
flow.hass = hass flow.hass = hass
flow.context = {}
discovery_flow_conf["discovered"] = True discovery_flow_conf["discovered"] = True
MockConfigEntry(domain="test").add_to_hass(hass) MockConfigEntry(domain="test").add_to_hass(hass)
@ -167,6 +171,38 @@ async def test_import_single_instance(hass, discovery_flow_conf):
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
async def test_ignored_discoveries(hass, discovery_flow_conf):
"""Test we can ignore discovered entries."""
mock_entity_platform(hass, "config_flow.test", None)
result = await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
flow = next(
(
flw
for flw in hass.config_entries.flow.async_progress()
if flw["flow_id"] == result["flow_id"]
),
None,
)
# Ignore it.
await hass.config_entries.flow.async_init(
flow["handler"],
context={"source": config_entries.SOURCE_IGNORE},
data={"unique_id": flow["context"]["unique_id"]},
)
# Second discovery should be aborted
result = await hass.config_entries.flow.async_init(
"test", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
async def test_webhook_single_entry_allowed(hass, webhook_flow_conf): async def test_webhook_single_entry_allowed(hass, webhook_flow_conf):
"""Test only a single entry is allowed.""" """Test only a single entry is allowed."""
flow = config_entries.HANDLERS["test_single"]() flow = config_entries.HANDLERS["test_single"]()