2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ devices."""
|
2018-01-01 16:08:13 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-01-17 23:28:34 +00:00
|
|
|
from homeassistant.config_entries import _UNDEF
|
2019-09-14 20:53:59 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
from .config_flow import get_master_gateway
|
2020-01-17 23:28:34 +00:00
|
|
|
from .const import CONF_BRIDGE_ID, CONF_GROUP_ID_BASE, CONF_MASTER_GATEWAY, DOMAIN
|
2020-01-03 10:50:53 +00:00
|
|
|
from .gateway import DeconzGateway
|
2019-09-14 13:15:06 +00:00
|
|
|
from .services import async_setup_services, async_unload_services
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2019-09-14 20:53:59 +00:00
|
|
|
{DOMAIN: vol.Schema({}, extra=vol.ALLOW_EXTRA)}, extra=vol.ALLOW_EXTRA
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-02-14 00:23:04 +00:00
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_setup(hass, config):
|
2019-09-14 20:53:59 +00:00
|
|
|
"""Old way of setting up deCONZ integrations."""
|
2018-04-18 14:27:44 +00:00
|
|
|
return True
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
2018-04-23 16:00:16 +00:00
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up a deCONZ bridge for a config entry.
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
Load config, group, light and sensor data for server information.
|
|
|
|
Start websocket for push notification of state changes from deCONZ.
|
|
|
|
"""
|
2019-04-05 00:48:24 +00:00
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
if not config_entry.options:
|
2019-09-05 23:38:00 +00:00
|
|
|
await async_update_master_gateway(hass, config_entry)
|
2018-04-23 16:00:16 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
gateway = DeconzGateway(hass, config_entry)
|
2018-05-05 14:11:00 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
if not await gateway.async_setup():
|
2018-01-01 16:08:13 +00:00
|
|
|
return False
|
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
# 0.104 introduced config entry unique id, this makes upgrading possible
|
|
|
|
if config_entry.unique_id is None:
|
2020-01-17 23:28:34 +00:00
|
|
|
|
|
|
|
new_data = _UNDEF
|
|
|
|
if CONF_BRIDGE_ID in config_entry.data:
|
|
|
|
new_data = dict(config_entry.data)
|
|
|
|
new_data[CONF_GROUP_ID_BASE] = config_entry.data[CONF_BRIDGE_ID]
|
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
hass.config_entries.async_update_entry(
|
2020-01-17 23:28:34 +00:00
|
|
|
config_entry, unique_id=gateway.api.config.bridgeid, data=new_data
|
2020-01-03 10:50:53 +00:00
|
|
|
)
|
2019-02-27 20:04:55 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
hass.data[DOMAIN][config_entry.unique_id] = gateway
|
2018-08-24 17:37:22 +00:00
|
|
|
|
2020-01-03 10:50:53 +00:00
|
|
|
await gateway.async_update_device_registry()
|
2019-09-25 16:56:31 +00:00
|
|
|
|
2019-09-14 13:15:06 +00:00
|
|
|
await async_setup_services(hass)
|
2018-09-04 07:24:42 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gateway.shutdown)
|
2019-09-14 13:15:06 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
return True
|
2018-04-29 14:16:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload deCONZ config entry."""
|
2020-01-03 10:50:53 +00:00
|
|
|
gateway = hass.data[DOMAIN].pop(config_entry.unique_id)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
if not hass.data[DOMAIN]:
|
2019-09-14 13:15:06 +00:00
|
|
|
await async_unload_services(hass)
|
2019-05-27 04:56:00 +00:00
|
|
|
|
2019-04-05 00:48:24 +00:00
|
|
|
elif gateway.master:
|
2019-09-05 23:38:00 +00:00
|
|
|
await async_update_master_gateway(hass, config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
new_master_gateway = next(iter(hass.data[DOMAIN].values()))
|
2019-09-05 23:38:00 +00:00
|
|
|
await async_update_master_gateway(hass, new_master_gateway.config_entry)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-10-31 21:38:04 +00:00
|
|
|
return await gateway.async_reset()
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
|
2019-09-05 23:38:00 +00:00
|
|
|
async def async_update_master_gateway(hass, config_entry):
|
|
|
|
"""Update master gateway boolean.
|
2019-04-05 00:48:24 +00:00
|
|
|
|
|
|
|
Called by setup_entry and unload_entry.
|
|
|
|
Makes sure there is always one master available.
|
|
|
|
"""
|
|
|
|
master = not get_master_gateway(hass)
|
2019-09-25 16:56:31 +00:00
|
|
|
options = {**config_entry.options, CONF_MASTER_GATEWAY: master}
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2019-09-25 16:56:31 +00:00
|
|
|
hass.config_entries.async_update_entry(config_entry, options=options)
|