Abort bond hub config flow if hub is already registered (#38416)
Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
parent
0136c565eb
commit
89cbcdb9dc
5 changed files with 48 additions and 3 deletions
|
@ -57,6 +57,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
else:
|
||||||
await self.async_set_unique_id(bond_id)
|
await self.async_set_unique_id(bond_id)
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
return self.async_create_entry(title=bond_id, data=user_input)
|
return self.async_create_entry(title=bond_id, data=user_input)
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
|
|
|
@ -4,5 +4,6 @@
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/bond",
|
"documentation": "https://www.home-assistant.io/integrations/bond",
|
||||||
"requirements": ["bond-api==0.1.7"],
|
"requirements": ["bond-api==0.1.7"],
|
||||||
"codeowners": ["@prystupa"]
|
"codeowners": ["@prystupa"],
|
||||||
|
"quality_scale": "platinum"
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,9 @@
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||||
|
},
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
|
"abort": {
|
||||||
|
"already_configured": "Device is already configured"
|
||||||
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "Failed to connect",
|
"cannot_connect": "Failed to connect",
|
||||||
"invalid_auth": "Invalid authentication",
|
"invalid_auth": "Invalid authentication",
|
||||||
|
|
|
@ -9,6 +9,7 @@ from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
|
||||||
from .common import patch_bond_device_ids, patch_bond_version
|
from .common import patch_bond_device_ids, patch_bond_version
|
||||||
|
|
||||||
from tests.async_mock import Mock, patch
|
from tests.async_mock import Mock, patch
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass: core.HomeAssistant):
|
async def test_form(hass: core.HomeAssistant):
|
||||||
|
@ -69,7 +70,9 @@ async def test_form_cannot_connect(hass: core.HomeAssistant):
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch_bond_device_ids(side_effect=ClientConnectionError()):
|
with patch_bond_version(
|
||||||
|
side_effect=ClientConnectionError()
|
||||||
|
), patch_bond_device_ids():
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
|
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
|
||||||
|
@ -97,3 +100,37 @@ async def test_form_unexpected_error(hass: core.HomeAssistant):
|
||||||
|
|
||||||
assert result2["type"] == "form"
|
assert result2["type"] == "form"
|
||||||
assert result2["errors"] == {"base": "unknown"}
|
assert result2["errors"] == {"base": "unknown"}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_form_one_entry_per_device_allowed(hass: core.HomeAssistant):
|
||||||
|
"""Test that only one entry allowed per unique ID reported by Bond hub device."""
|
||||||
|
MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
unique_id="already-registered-bond-id",
|
||||||
|
data={CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
|
||||||
|
).add_to_hass(hass)
|
||||||
|
|
||||||
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch_bond_version(
|
||||||
|
return_value={"bondid": "already-registered-bond-id"}
|
||||||
|
), patch_bond_device_ids(), patch(
|
||||||
|
"homeassistant.components.bond.async_setup", return_value=True
|
||||||
|
) as mock_setup, patch(
|
||||||
|
"homeassistant.components.bond.async_setup_entry", return_value=True,
|
||||||
|
) as mock_setup_entry:
|
||||||
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result2["type"] == "abort"
|
||||||
|
assert result2["reason"] == "already_configured"
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(mock_setup.mock_calls) == 0
|
||||||
|
assert len(mock_setup_entry.mock_calls) == 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue