Add unique ID to config entries (#29806)
* Add unique ID to config entries * Unload existing entries with same unique ID if flow with unique ID is finished * Remove unused exception * Fix typing * silence pylint * Fix tests * Add unique ID to Hue * Address typing comment * Tweaks to comments * lint
This commit is contained in:
1 parent
87ca61ddd7
commit
d851cb6f9e
10 files changed
+305
-46
No files matched your search
@@ -4,11 +4,11 @@ import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant import config_entries, core
|
||||
from homeassistant.const import CONF_FILENAME, CONF_HOST
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
|
||||
from .bridge import HueBridge
|
||||
from .bridge import HueBridge, normalize_bridge_id
|
||||
from .config_flow import ( # Loading the config flow file will register the flow
|
||||
configured_hosts,
|
||||
)
|
||||
@@ -102,7 +102,9 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
async def async_setup_entry(
|
||||
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
|
||||
):
|
||||
"""Set up a bridge from a config entry."""
|
||||
host = entry.data["host"]
|
||||
config = hass.data[DATA_CONFIGS].get(host)
|
||||
@@ -121,6 +123,13 @@ async def async_setup_entry(hass, entry):
|
||||
|
||||
hass.data[DOMAIN][host] = bridge
|
||||
config = bridge.api.config
|
||||
|
||||
# For backwards compat
|
||||
if entry.unique_id is None:
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, unique_id=normalize_bridge_id(config.bridgeid)
|
||||
)
|
||||
|
||||
device_registry = await dr.async_get_registry(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
|
||||
@@ -201,3 +201,25 @@ async def get_bridge(hass, host, username=None):
|
||||
except aiohue.AiohueException:
|
||||
LOGGER.exception("Unknown Hue linking error occurred")
|
||||
raise AuthenticationRequired
|
||||
|
||||
|
||||
def normalize_bridge_id(bridge_id: str):
|
||||
"""Normalize a bridge identifier.
|
||||
|
||||
There are three sources where we receive bridge ID from:
|
||||
- ssdp/upnp: <host>/description.xml, field root/device/serialNumber
|
||||
- nupnp: "id" field
|
||||
- Hue Bridge API: config.bridgeid
|
||||
|
||||
The SSDP/UPNP source does not contain the middle 4 characters compared
|
||||
to the other sources. In all our tests the middle 4 characters are "fffe".
|
||||
"""
|
||||
if len(bridge_id) == 16:
|
||||
return bridge_id[0:6] + bridge_id[-6:]
|
||||
|
||||
if len(bridge_id) == 12:
|
||||
return bridge_id
|
||||
|
||||
LOGGER.warning("Unexpected bridge id number found: %s", bridge_id)
|
||||
|
||||
return bridge_id
|
||||
@@ -12,7 +12,7 @@ from homeassistant.components.ssdp import ATTR_MANUFACTURERURL, ATTR_NAME
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
||||
from .bridge import get_bridge
|
||||
from .bridge import get_bridge, normalize_bridge_id
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .errors import AuthenticationRequired, CannotConnect
|
||||
|
||||
@@ -154,17 +154,15 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if host in configured_hosts(self.hass):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
# This value is based off host/description.xml and is, weirdly, missing
|
||||
# 4 characters in the middle of the serial compared to results returned
|
||||
# from the NUPNP API or when querying the bridge API for bridgeid.
|
||||
# (on first gen Hue hub)
|
||||
serial = discovery_info.get("serial")
|
||||
bridge_id = discovery_info.get("serial")
|
||||
|
||||
await self.async_set_unique_id(normalize_bridge_id(bridge_id))
|
||||
|
||||
return await self.async_step_import(
|
||||
{
|
||||
"host": host,
|
||||
# This format is the legacy format that Hue used for discovery
|
||||
"path": f"phue-{serial}.conf",
|
||||
"path": f"phue-{bridge_id}.conf",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -180,6 +178,10 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if host in configured_hosts(self.hass):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
await self.async_set_unique_id(
|
||||
normalize_bridge_id(homekit_info["properties"]["id"].replace(":", ""))
|
||||
)
|
||||
|
||||
return await self.async_step_import({"host": host})
|
||||
|
||||
async def async_step_import(self, import_info):
|
||||
@@ -234,18 +236,9 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
host = bridge.host
|
||||
bridge_id = bridge.config.bridgeid
|
||||
|
||||
same_hub_entries = [
|
||||
entry.entry_id
|
||||
for entry in self.hass.config_entries.async_entries(DOMAIN)
|
||||
if entry.data["bridge_id"] == bridge_id or entry.data["host"] == host
|
||||
]
|
||||
|
||||
if same_hub_entries:
|
||||
await asyncio.wait(
|
||||
[
|
||||
self.hass.config_entries.async_remove(entry_id)
|
||||
for entry_id in same_hub_entries
|
||||
]
|
||||
if self.unique_id is None:
|
||||
await self.async_set_unique_id(
|
||||
normalize_bridge_id(bridge_id), raise_on_progress=False
|
||||
)
|
||||
|
||||
return self.async_create_entry(
|
||||
|
||||
Reference in new issue
Block a user