Support multiple deCONZ gateways (#22449)
* Store gateways inside a dict in deconz domain * Make reachable events gateway specific * Gateway shall always exist * Adapt new device signalling to support multiple gateways * Services follow gateway master * Working on unload entry * Make unload and master handover work Improve tests for init * Fix config flow * Fix linting * Clean up init tests * Clean up hassio discovery to fit with the rest * Store gateways inside a dict in deconz domain * Make reachable events gateway specific * Gateway shall always exist * Adapt new device signalling to support multiple gateways * Services follow gateway master * Working on unload entry * Make unload and master handover work Improve tests for init * Fix config flow * Fix linting * Clean up init tests * Clean up hassio discovery to fit with the rest * Add support for services to specify bridgeid
This commit is contained in:
1 parent
b9ec623ad9
commit
b50afec5f1
22 files changed
+535
-426
No files matched your search
@@ -4,12 +4,14 @@ import voluptuous as vol
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import (
|
||||
CONF_API_KEY, CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||
|
||||
# Loading the config flow file will register the flow
|
||||
from .config_flow import configured_hosts
|
||||
from .const import DEFAULT_PORT, DOMAIN, _LOGGER
|
||||
from .config_flow import get_master_gateway
|
||||
from .const import (
|
||||
CONF_ALLOW_CLIP_SENSOR, CONF_ALLOW_DECONZ_GROUPS, CONF_BRIDGEID,
|
||||
CONF_MASTER_GATEWAY, DEFAULT_PORT, DOMAIN, _LOGGER)
|
||||
from .gateway import DeconzGateway
|
||||
|
||||
REQUIREMENTS = ['pydeconz==54']
|
||||
@@ -32,26 +34,27 @@ SERVICE_SCHEMA = vol.All(vol.Schema({
|
||||
vol.Optional(SERVICE_ENTITY): cv.entity_id,
|
||||
vol.Optional(SERVICE_FIELD): cv.matches_regex('/.*'),
|
||||
vol.Required(SERVICE_DATA): dict,
|
||||
vol.Optional(CONF_BRIDGEID): str
|
||||
}), cv.has_at_least_one_key(SERVICE_ENTITY, SERVICE_FIELD))
|
||||
|
||||
SERVICE_DEVICE_REFRESH = 'device_refresh'
|
||||
|
||||
SERVICE_DEVICE_REFRESCH_SCHEMA = vol.All(vol.Schema({
|
||||
vol.Optional(CONF_BRIDGEID): str
|
||||
}))
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Load configuration for deCONZ component.
|
||||
|
||||
Discovery has loaded the component if DOMAIN is not present in config.
|
||||
"""
|
||||
if DOMAIN in config:
|
||||
deconz_config = None
|
||||
if CONF_HOST in config[DOMAIN]:
|
||||
deconz_config = config[DOMAIN]
|
||||
if deconz_config and not configured_hosts(hass):
|
||||
hass.async_add_job(hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={'source': config_entries.SOURCE_IMPORT},
|
||||
data=deconz_config
|
||||
))
|
||||
if not hass.config_entries.async_entries(DOMAIN) and DOMAIN in config:
|
||||
deconz_config = config[DOMAIN]
|
||||
hass.async_add_job(hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
|
||||
data=deconz_config
|
||||
))
|
||||
return True
|
||||
|
||||
|
||||
@@ -61,26 +64,20 @@ async def async_setup_entry(hass, config_entry):
|
||||
Load config, group, light and sensor data for server information.
|
||||
Start websocket for push notification of state changes from deCONZ.
|
||||
"""
|
||||
if DOMAIN in hass.data:
|
||||
_LOGGER.error(
|
||||
"Config entry failed since one deCONZ instance already exists")
|
||||
return False
|
||||
if DOMAIN not in hass.data:
|
||||
hass.data[DOMAIN] = {}
|
||||
|
||||
if not config_entry.options:
|
||||
await async_populate_options(hass, config_entry)
|
||||
|
||||
gateway = DeconzGateway(hass, config_entry)
|
||||
|
||||
if not await gateway.async_setup():
|
||||
return False
|
||||
|
||||
hass.data[DOMAIN] = gateway
|
||||
hass.data[DOMAIN][gateway.bridgeid] = gateway
|
||||
|
||||
device_registry = await \
|
||||
hass.helpers.device_registry.async_get_registry()
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={(CONNECTION_NETWORK_MAC, gateway.api.config.mac)},
|
||||
identifiers={(DOMAIN, gateway.api.config.bridgeid)},
|
||||
manufacturer='Dresden Elektronik', model=gateway.api.config.modelid,
|
||||
name=gateway.api.config.name, sw_version=gateway.api.config.swversion)
|
||||
await gateway.async_update_device_registry()
|
||||
|
||||
async def async_configure(call):
|
||||
"""Set attribute of device in deCONZ.
|
||||
@@ -100,8 +97,11 @@ async def async_setup_entry(hass, config_entry):
|
||||
"""
|
||||
field = call.data.get(SERVICE_FIELD, '')
|
||||
entity_id = call.data.get(SERVICE_ENTITY)
|
||||
data = call.data.get(SERVICE_DATA)
|
||||
gateway = hass.data[DOMAIN]
|
||||
data = call.data[SERVICE_DATA]
|
||||
|
||||
gateway = get_master_gateway(hass)
|
||||
if CONF_BRIDGEID in call.data:
|
||||
gateway = hass.data[DOMAIN][call.data[CONF_BRIDGEID]]
|
||||
|
||||
if entity_id:
|
||||
try:
|
||||
@@ -117,7 +117,9 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
async def async_refresh_devices(call):
|
||||
"""Refresh available devices from deCONZ."""
|
||||
gateway = hass.data[DOMAIN]
|
||||
gateway = get_master_gateway(hass)
|
||||
if CONF_BRIDGEID in call.data:
|
||||
gateway = hass.data[DOMAIN][call.data[CONF_BRIDGEID]]
|
||||
|
||||
groups = set(gateway.api.groups.keys())
|
||||
lights = set(gateway.api.lights.keys())
|
||||
@@ -151,7 +153,8 @@ async def async_setup_entry(hass, config_entry):
|
||||
)
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_DEVICE_REFRESH, async_refresh_devices)
|
||||
DOMAIN, SERVICE_DEVICE_REFRESH, async_refresh_devices,
|
||||
schema=SERVICE_DEVICE_REFRESCH_SCHEMA)
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, gateway.shutdown)
|
||||
return True
|
||||
@@ -159,7 +162,34 @@ async def async_setup_entry(hass, config_entry):
|
||||
|
||||
async def async_unload_entry(hass, config_entry):
|
||||
"""Unload deCONZ config entry."""
|
||||
gateway = hass.data.pop(DOMAIN)
|
||||
hass.services.async_remove(DOMAIN, SERVICE_DECONZ)
|
||||
hass.services.async_remove(DOMAIN, SERVICE_DEVICE_REFRESH)
|
||||
gateway = hass.data[DOMAIN].pop(config_entry.data[CONF_BRIDGEID])
|
||||
|
||||
if not hass.data[DOMAIN]:
|
||||
hass.services.async_remove(DOMAIN, SERVICE_DECONZ)
|
||||
hass.services.async_remove(DOMAIN, SERVICE_DEVICE_REFRESH)
|
||||
elif gateway.master:
|
||||
await async_populate_options(hass, config_entry)
|
||||
new_master_gateway = next(iter(hass.data[DOMAIN].values()))
|
||||
await async_populate_options(hass, new_master_gateway.config_entry)
|
||||
|
||||
return await gateway.async_reset()
|
||||
|
||||
|
||||
@callback
|
||||
async def async_populate_options(hass, config_entry):
|
||||
"""Populate default options for gateway.
|
||||
|
||||
Called by setup_entry and unload_entry.
|
||||
Makes sure there is always one master available.
|
||||
"""
|
||||
master = not get_master_gateway(hass)
|
||||
|
||||
options = {
|
||||
CONF_MASTER_GATEWAY: master,
|
||||
CONF_ALLOW_CLIP_SENSOR: config_entry.data.get(
|
||||
CONF_ALLOW_CLIP_SENSOR, False),
|
||||
CONF_ALLOW_DECONZ_GROUPS: config_entry.data.get(
|
||||
CONF_ALLOW_DECONZ_GROUPS, True)
|
||||
}
|
||||
|
||||
hass.config_entries.async_update_entry(config_entry, options=options)
|
||||
Reference in new issue
Block a user