mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Generate bond config entry ID from the hub metadata (#38354)
* Generate bond config entry ID from the hub metadata * Generate bond config entry ID from the hub metadata (PR feedback)
This commit is contained in:
parent
ecf22198c5
commit
0493f1c206
@ -41,6 +41,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = hub
|
||||
|
||||
if not entry.unique_id:
|
||||
hass.config_entries.async_update_entry(entry, unique_id=hub.bond_id)
|
||||
|
||||
device_registry = await dr.async_get_registry(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
|
@ -17,11 +17,13 @@ DATA_SCHEMA = vol.Schema(
|
||||
)
|
||||
|
||||
|
||||
async def validate_input(data):
|
||||
async def validate_input(data) -> str:
|
||||
"""Validate the user input allows us to connect."""
|
||||
|
||||
try:
|
||||
bond = Bond(data[CONF_HOST], data[CONF_ACCESS_TOKEN])
|
||||
version = await bond.version()
|
||||
# call to non-version API is needed to validate authentication
|
||||
await bond.devices()
|
||||
except ClientConnectionError:
|
||||
raise CannotConnect
|
||||
@ -30,8 +32,8 @@ async def validate_input(data):
|
||||
raise InvalidAuth
|
||||
raise
|
||||
|
||||
# Return info to be stored in the config entry.
|
||||
return {"title": data[CONF_HOST]}
|
||||
# Return unique ID from the hub to be stored in the config entry.
|
||||
return version["bondid"]
|
||||
|
||||
|
||||
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
@ -45,7 +47,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
try:
|
||||
info = await validate_input(user_input)
|
||||
bond_id = await validate_input(user_input)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except InvalidAuth:
|
||||
@ -54,7 +56,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
return self.async_create_entry(title=info["title"], data=user_input)
|
||||
await self.async_set_unique_id(bond_id)
|
||||
return self.async_create_entry(title=bond_id, data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
||||
|
@ -6,7 +6,7 @@ from homeassistant import config_entries, core, setup
|
||||
from homeassistant.components.bond.const import DOMAIN
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
|
||||
|
||||
from .common import patch_bond_device_ids
|
||||
from .common import patch_bond_device_ids, patch_bond_version
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
|
||||
@ -20,7 +20,9 @@ async def test_form(hass: core.HomeAssistant):
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch_bond_device_ids(), patch(
|
||||
with patch_bond_version(
|
||||
return_value={"bondid": "test-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,
|
||||
@ -31,7 +33,7 @@ async def test_form(hass: core.HomeAssistant):
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "some host"
|
||||
assert result2["title"] == "test-bond-id"
|
||||
assert result2["data"] == {
|
||||
CONF_HOST: "some host",
|
||||
CONF_ACCESS_TOKEN: "test-token",
|
||||
@ -47,7 +49,9 @@ async def test_form_invalid_auth(hass: core.HomeAssistant):
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch_bond_device_ids(
|
||||
with patch_bond_version(
|
||||
return_value={"bond_id": "test-bond-id"}
|
||||
), patch_bond_device_ids(
|
||||
side_effect=ClientResponseError(Mock(), Mock(), status=401),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
@ -81,7 +85,9 @@ async def test_form_unexpected_error(hass: core.HomeAssistant):
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch_bond_device_ids(
|
||||
with patch_bond_version(
|
||||
return_value={"bond_id": "test-bond-id"}
|
||||
), patch_bond_device_ids(
|
||||
side_effect=ClientResponseError(Mock(), Mock(), status=500)
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
|
@ -64,6 +64,7 @@ async def test_async_setup_entry_sets_up_hub_and_supported_domains(hass: HomeAss
|
||||
|
||||
assert config_entry.entry_id in hass.data[DOMAIN]
|
||||
assert config_entry.state == ENTRY_STATE_LOADED
|
||||
assert config_entry.unique_id == "test-bond-id"
|
||||
|
||||
# verify hub device is registered correctly
|
||||
device_registry = await dr.async_get_registry(hass)
|
||||
|
Loading…
x
Reference in New Issue
Block a user