Use config entry unique id for deCONZ (#30122)
* Use config entry unique id * Clean up * Backwards compatiblity note * Fix some of Balloobs comments * Bump dependency to v66 * Black somehow missed config flow tests... * Move set unique ID til after possibility to update existing entry
This commit is contained in:
1 parent
c130e81638
commit
0a4f3ec1ec
12 files changed
+136
-226
No files matched your search
@@ -4,8 +4,8 @@ import voluptuous as vol
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
|
||||
from .config_flow import get_master_gateway
|
||||
from .const import CONF_BRIDGEID, CONF_MASTER_GATEWAY, CONF_UUID, DOMAIN
|
||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||
from .const import CONF_MASTER_GATEWAY, DOMAIN
|
||||
from .gateway import DeconzGateway
|
||||
from .services import async_setup_services, async_unload_services
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
@@ -35,13 +35,16 @@ async def async_setup_entry(hass, config_entry):
|
||||
if not await gateway.async_setup():
|
||||
return False
|
||||
|
||||
hass.data[DOMAIN][gateway.bridgeid] = gateway
|
||||
# 0.104 introduced config entry unique id, this makes upgrading possible
|
||||
if config_entry.unique_id is None:
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry, unique_id=gateway.api.config.bridgeid
|
||||
)
|
||||
|
||||
hass.data[DOMAIN][config_entry.unique_id] = gateway
|
||||
|
||||
await gateway.async_update_device_registry()
|
||||
|
||||
if CONF_UUID not in config_entry.data:
|
||||
await async_add_uuid_to_config_entry(hass, config_entry)
|
||||
|
||||
await async_setup_services(hass)
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gateway.shutdown)
|
||||
@@ -51,7 +54,7 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
async def async_unload_entry(hass, config_entry):
|
||||
"""Unload deCONZ config entry."""
|
||||
gateway = hass.data[DOMAIN].pop(config_entry.data[CONF_BRIDGEID])
|
||||
gateway = hass.data[DOMAIN].pop(config_entry.unique_id)
|
||||
|
||||
if not hass.data[DOMAIN]:
|
||||
await async_unload_services(hass)
|
||||
@@ -74,11 +77,3 @@ async def async_update_master_gateway(hass, config_entry):
|
||||
options = {**config_entry.options, CONF_MASTER_GATEWAY: master}
|
||||
|
||||
hass.config_entries.async_update_entry(config_entry, options=options)
|
||||
|
||||
|
||||
async def async_add_uuid_to_config_entry(hass, config_entry):
|
||||
"""Add UUID to config entry to help discovery identify entries."""
|
||||
gateway = get_gateway_from_config_entry(hass, config_entry)
|
||||
config = {**config_entry.data, CONF_UUID: gateway.api.config.uuid}
|
||||
|
||||
hass.config_entries.async_update_entry(config_entry, data=config)
|
||||
@@ -4,7 +4,12 @@ from urllib.parse import urlparse
|
||||
|
||||
import async_timeout
|
||||
from pydeconz.errors import RequestError, ResponseError
|
||||
from pydeconz.utils import async_discovery, async_get_api_key, async_get_gateway_config
|
||||
from pydeconz.utils import (
|
||||
async_discovery,
|
||||
async_get_api_key,
|
||||
async_get_bridge_id,
|
||||
normalize_bridge_id,
|
||||
)
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
@@ -14,11 +19,9 @@ from homeassistant.core import callback
|
||||
from homeassistant.helpers import aiohttp_client
|
||||
|
||||
from .const import (
|
||||
_LOGGER,
|
||||
CONF_ALLOW_CLIP_SENSOR,
|
||||
CONF_ALLOW_DECONZ_GROUPS,
|
||||
CONF_BRIDGEID,
|
||||
CONF_UUID,
|
||||
DEFAULT_ALLOW_CLIP_SENSOR,
|
||||
DEFAULT_ALLOW_DECONZ_GROUPS,
|
||||
DEFAULT_PORT,
|
||||
@@ -29,15 +32,6 @@ DECONZ_MANUFACTURERURL = "http://www.dresden-elektronik.de"
|
||||
CONF_SERIAL = "serial"
|
||||
|
||||
|
||||
@callback
|
||||
def configured_gateways(hass):
|
||||
"""Return a set of all configured gateways."""
|
||||
return {
|
||||
entry.data[CONF_BRIDGEID]: entry
|
||||
for entry in hass.config_entries.async_entries(DOMAIN)
|
||||
}
|
||||
|
||||
|
||||
@callback
|
||||
def get_master_gateway(hass):
|
||||
"""Return the gateway which is marked as master."""
|
||||
@@ -62,6 +56,7 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the deCONZ config flow."""
|
||||
self.bridge_id = None
|
||||
self.bridges = []
|
||||
self.deconz_config = {}
|
||||
|
||||
@@ -79,7 +74,11 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if user_input is not None:
|
||||
for bridge in self.bridges:
|
||||
if bridge[CONF_HOST] == user_input[CONF_HOST]:
|
||||
self.deconz_config = bridge
|
||||
self.bridge_id = bridge[CONF_BRIDGEID]
|
||||
self.deconz_config = {
|
||||
CONF_HOST: bridge[CONF_HOST],
|
||||
CONF_PORT: bridge[CONF_PORT],
|
||||
}
|
||||
return await self.async_step_link()
|
||||
|
||||
self.deconz_config = user_input
|
||||
@@ -95,8 +94,7 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self.bridges = []
|
||||
|
||||
if len(self.bridges) == 1:
|
||||
self.deconz_config = self.bridges[0]
|
||||
return await self.async_step_link()
|
||||
return await self.async_step_user(self.bridges[0])
|
||||
|
||||
if len(self.bridges) > 1:
|
||||
hosts = []
|
||||
@@ -141,23 +139,21 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
async def _create_entry(self):
|
||||
"""Create entry for gateway."""
|
||||
if CONF_BRIDGEID not in self.deconz_config:
|
||||
if not self.bridge_id:
|
||||
session = aiohttp_client.async_get_clientsession(self.hass)
|
||||
|
||||
try:
|
||||
with async_timeout.timeout(10):
|
||||
gateway_config = await async_get_gateway_config(
|
||||
self.bridge_id = await async_get_bridge_id(
|
||||
session, **self.deconz_config
|
||||
)
|
||||
self.deconz_config[CONF_BRIDGEID] = gateway_config.bridgeid
|
||||
self.deconz_config[CONF_UUID] = gateway_config.uuid
|
||||
await self.async_set_unique_id(self.bridge_id)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
return self.async_abort(reason="no_bridges")
|
||||
|
||||
return self.async_create_entry(
|
||||
title="deCONZ-" + self.deconz_config[CONF_BRIDGEID], data=self.deconz_config
|
||||
)
|
||||
return self.async_create_entry(title=self.bridge_id, data=self.deconz_config)
|
||||
|
||||
def _update_entry(self, entry, host, port, api_key=None):
|
||||
"""Update existing entry."""
|
||||
@@ -182,27 +178,17 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if discovery_info[ssdp.ATTR_UPNP_MANUFACTURER_URL] != DECONZ_MANUFACTURERURL:
|
||||
return self.async_abort(reason="not_deconz_bridge")
|
||||
|
||||
uuid = discovery_info[ssdp.ATTR_UPNP_UDN].replace("uuid:", "")
|
||||
|
||||
_LOGGER.debug("deCONZ gateway discovered (%s)", uuid)
|
||||
|
||||
self.bridge_id = normalize_bridge_id(discovery_info[ssdp.ATTR_UPNP_SERIAL])
|
||||
parsed_url = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION])
|
||||
|
||||
for entry in self.hass.config_entries.async_entries(DOMAIN):
|
||||
if uuid == entry.data.get(CONF_UUID):
|
||||
if self.bridge_id == entry.unique_id:
|
||||
if entry.source == "hassio":
|
||||
return self.async_abort(reason="already_configured")
|
||||
return self._update_entry(entry, parsed_url.hostname, parsed_url.port)
|
||||
|
||||
bridgeid = discovery_info[ssdp.ATTR_UPNP_SERIAL]
|
||||
if any(
|
||||
bridgeid == flow["context"][CONF_BRIDGEID]
|
||||
for flow in self._async_in_progress()
|
||||
):
|
||||
return self.async_abort(reason="already_in_progress")
|
||||
|
||||
await self.async_set_unique_id(self.bridge_id)
|
||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||
self.context[CONF_BRIDGEID] = bridgeid
|
||||
self.context["title_placeholders"] = {"host": parsed_url.hostname}
|
||||
|
||||
self.deconz_config = {
|
||||
@@ -217,18 +203,18 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
This flow is triggered by the discovery component.
|
||||
"""
|
||||
bridgeid = user_input[CONF_SERIAL]
|
||||
gateway_entries = configured_gateways(self.hass)
|
||||
self.bridge_id = normalize_bridge_id(user_input[CONF_SERIAL])
|
||||
gateway = self.hass.data.get(DOMAIN, {}).get(self.bridge_id)
|
||||
|
||||
if bridgeid in gateway_entries:
|
||||
entry = gateway_entries[bridgeid]
|
||||
if gateway:
|
||||
return self._update_entry(
|
||||
entry,
|
||||
gateway.config_entry,
|
||||
user_input[CONF_HOST],
|
||||
user_input[CONF_PORT],
|
||||
user_input[CONF_API_KEY],
|
||||
)
|
||||
|
||||
await self.async_set_unique_id(self.bridge_id)
|
||||
self._hassio_discovery = user_input
|
||||
|
||||
return await self.async_step_hassio_confirm()
|
||||
@@ -239,7 +225,6 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self.deconz_config = {
|
||||
CONF_HOST: self._hassio_discovery[CONF_HOST],
|
||||
CONF_PORT: self._hassio_discovery[CONF_PORT],
|
||||
CONF_BRIDGEID: self._hassio_discovery[CONF_SERIAL],
|
||||
CONF_API_KEY: self._hassio_discovery[CONF_API_KEY],
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ _LOGGER = logging.getLogger(__package__)
|
||||
DOMAIN = "deconz"
|
||||
|
||||
CONF_BRIDGEID = "bridgeid"
|
||||
CONF_UUID = "uuid"
|
||||
|
||||
DEFAULT_PORT = 80
|
||||
DEFAULT_ALLOW_CLIP_SENSOR = False
|
||||
|
||||
@@ -15,9 +15,7 @@ from homeassistant.const import (
|
||||
)
|
||||
|
||||
from . import DOMAIN
|
||||
from .config_flow import configured_gateways
|
||||
from .deconz_event import CONF_DECONZ_EVENT, CONF_UNIQUE_ID
|
||||
from .gateway import get_gateway_from_config_entry
|
||||
|
||||
CONF_SUBTYPE = "subtype"
|
||||
|
||||
@@ -287,10 +285,8 @@ TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
|
||||
|
||||
def _get_deconz_event_from_device_id(hass, device_id):
|
||||
"""Resolve deconz event from device id."""
|
||||
deconz_config_entries = configured_gateways(hass)
|
||||
for config_entry in deconz_config_entries.values():
|
||||
for gateway in hass.data.get(DOMAIN):
|
||||
|
||||
gateway = get_gateway_from_config_entry(hass, config_entry)
|
||||
for deconz_event in gateway.events:
|
||||
|
||||
if device_id == deconz_event.device_id:
|
||||
|
||||
@@ -22,7 +22,6 @@ from .const import (
|
||||
_LOGGER,
|
||||
CONF_ALLOW_CLIP_SENSOR,
|
||||
CONF_ALLOW_DECONZ_GROUPS,
|
||||
CONF_BRIDGEID,
|
||||
CONF_MASTER_GATEWAY,
|
||||
DEFAULT_ALLOW_CLIP_SENSOR,
|
||||
DEFAULT_ALLOW_DECONZ_GROUPS,
|
||||
@@ -36,7 +35,7 @@ from .errors import AuthenticationRequired, CannotConnect
|
||||
@callback
|
||||
def get_gateway_from_config_entry(hass, config_entry):
|
||||
"""Return gateway with a matching bridge id."""
|
||||
return hass.data[DOMAIN][config_entry.data[CONF_BRIDGEID]]
|
||||
return hass.data[DOMAIN][config_entry.unique_id]
|
||||
|
||||
|
||||
class DeconzGateway:
|
||||
@@ -56,7 +55,7 @@ class DeconzGateway:
|
||||
@property
|
||||
def bridgeid(self) -> str:
|
||||
"""Return the unique identifier of the gateway."""
|
||||
return self.config_entry.data[CONF_BRIDGEID]
|
||||
return self.config_entry.unique_id
|
||||
|
||||
@property
|
||||
def master(self) -> bool:
|
||||
@@ -92,11 +91,9 @@ class DeconzGateway:
|
||||
|
||||
async def async_setup(self) -> bool:
|
||||
"""Set up a deCONZ gateway."""
|
||||
hass = self.hass
|
||||
|
||||
try:
|
||||
self.api = await get_gateway(
|
||||
hass,
|
||||
self.hass,
|
||||
self.config_entry.data,
|
||||
self.async_add_device_callback,
|
||||
self.async_connection_status_callback,
|
||||
@@ -110,8 +107,8 @@ class DeconzGateway:
|
||||
return False
|
||||
|
||||
for component in SUPPORTED_PLATFORMS:
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(
|
||||
self.hass.async_create_task(
|
||||
self.hass.config_entries.async_forward_entry_setup(
|
||||
self.config_entry, component
|
||||
)
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/deconz",
|
||||
"requirements": [
|
||||
"pydeconz==65"
|
||||
"pydeconz==66"
|
||||
],
|
||||
"ssdp": [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user