mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +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
|
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 = await dr.async_get_registry(hass)
|
||||||
device_registry.async_get_or_create(
|
device_registry.async_get_or_create(
|
||||||
config_entry_id=entry.entry_id,
|
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."""
|
"""Validate the user input allows us to connect."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bond = Bond(data[CONF_HOST], data[CONF_ACCESS_TOKEN])
|
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()
|
await bond.devices()
|
||||||
except ClientConnectionError:
|
except ClientConnectionError:
|
||||||
raise CannotConnect
|
raise CannotConnect
|
||||||
@ -30,8 +32,8 @@ async def validate_input(data):
|
|||||||
raise InvalidAuth
|
raise InvalidAuth
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# Return info to be stored in the config entry.
|
# Return unique ID from the hub to be stored in the config entry.
|
||||||
return {"title": data[CONF_HOST]}
|
return version["bondid"]
|
||||||
|
|
||||||
|
|
||||||
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
@ -45,7 +47,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
try:
|
try:
|
||||||
info = await validate_input(user_input)
|
bond_id = await validate_input(user_input)
|
||||||
except CannotConnect:
|
except CannotConnect:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
except InvalidAuth:
|
except InvalidAuth:
|
||||||
@ -54,7 +56,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
_LOGGER.exception("Unexpected exception")
|
_LOGGER.exception("Unexpected exception")
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
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(
|
return self.async_show_form(
|
||||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
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.components.bond.const import DOMAIN
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
|
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
|
from tests.async_mock import Mock, patch
|
||||||
|
|
||||||
@ -20,7 +20,9 @@ async def test_form(hass: core.HomeAssistant):
|
|||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
assert result["errors"] == {}
|
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
|
"homeassistant.components.bond.async_setup", return_value=True
|
||||||
) as mock_setup, patch(
|
) as mock_setup, patch(
|
||||||
"homeassistant.components.bond.async_setup_entry", return_value=True,
|
"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["type"] == "create_entry"
|
||||||
assert result2["title"] == "some host"
|
assert result2["title"] == "test-bond-id"
|
||||||
assert result2["data"] == {
|
assert result2["data"] == {
|
||||||
CONF_HOST: "some host",
|
CONF_HOST: "some host",
|
||||||
CONF_ACCESS_TOKEN: "test-token",
|
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}
|
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),
|
side_effect=ClientResponseError(Mock(), Mock(), status=401),
|
||||||
):
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
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}
|
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)
|
side_effect=ClientResponseError(Mock(), Mock(), status=500)
|
||||||
):
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
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.entry_id in hass.data[DOMAIN]
|
||||||
assert config_entry.state == ENTRY_STATE_LOADED
|
assert config_entry.state == ENTRY_STATE_LOADED
|
||||||
|
assert config_entry.unique_id == "test-bond-id"
|
||||||
|
|
||||||
# verify hub device is registered correctly
|
# verify hub device is registered correctly
|
||||||
device_registry = await dr.async_get_registry(hass)
|
device_registry = await dr.async_get_registry(hass)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user