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:
Robert Svensson authored and Paulus Schoutsen committed 2020-01-03 11:50:53 +01:00
1 parent c130e81638
commit 0a4f3ec1ec
12 files changed
+136 -226

No files matched your search

+10 -15
View File
@@ -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)