Fix issue with group unique id when normalising bridge id (#30904)

This commit is contained in:
Robert Svensson 2020-01-18 00:28:34 +01:00 committed by Paulus Schoutsen
parent 1d41cf96ca
commit 7b29a498c6
6 changed files with 28 additions and 15 deletions

View File

@ -1,10 +1,11 @@
"""Support for deCONZ devices."""
import voluptuous as vol
from homeassistant.config_entries import _UNDEF
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from .config_flow import get_master_gateway
from .const import CONF_MASTER_GATEWAY, DOMAIN
from .const import CONF_BRIDGE_ID, CONF_GROUP_ID_BASE, CONF_MASTER_GATEWAY, DOMAIN
from .gateway import DeconzGateway
from .services import async_setup_services, async_unload_services
@ -37,8 +38,14 @@ async def async_setup_entry(hass, config_entry):
# 0.104 introduced config entry unique id, this makes upgrading possible
if config_entry.unique_id is None:
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]
hass.config_entries.async_update_entry(
config_entry, unique_id=gateway.api.config.bridgeid
config_entry, unique_id=gateway.api.config.bridgeid, data=new_data
)
hass.data[DOMAIN][config_entry.unique_id] = gateway

View File

@ -21,7 +21,7 @@ from homeassistant.helpers import aiohttp_client
from .const import (
CONF_ALLOW_CLIP_SENSOR,
CONF_ALLOW_DECONZ_GROUPS,
CONF_BRIDGEID,
CONF_BRIDGE_ID,
DEFAULT_ALLOW_CLIP_SENSOR,
DEFAULT_ALLOW_DECONZ_GROUPS,
DEFAULT_PORT,
@ -74,7 +74,7 @@ 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.bridge_id = bridge[CONF_BRIDGEID]
self.bridge_id = bridge[CONF_BRIDGE_ID]
self.deconz_config = {
CONF_HOST: bridge[CONF_HOST],
CONF_PORT: bridge[CONF_PORT],

View File

@ -5,7 +5,8 @@ _LOGGER = logging.getLogger(__package__)
DOMAIN = "deconz"
CONF_BRIDGEID = "bridgeid"
CONF_BRIDGE_ID = "bridgeid"
CONF_GROUP_ID_BASE = "group_id_base"
DEFAULT_PORT = 80
DEFAULT_ALLOW_CLIP_SENSOR = False

View File

@ -22,6 +22,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.util.color as color_util
from .const import (
CONF_GROUP_ID_BASE,
COVER_TYPES,
DOMAIN as DECONZ_DOMAIN,
NEW_GROUP,
@ -205,7 +206,11 @@ class DeconzGroup(DeconzLight):
"""Set up group and create an unique id."""
super().__init__(device, gateway)
self._unique_id = f"{self.gateway.api.config.bridgeid}-{self._device.deconz_id}"
group_id_base = self.gateway.config_entry.unique_id
if CONF_GROUP_ID_BASE in self.gateway.config_entry.data:
group_id_base = self.gateway.config_entry.data[CONF_GROUP_ID_BASE]
self._unique_id = f"{group_id_base}-{self._device.deconz_id}"
@property
def unique_id(self):

View File

@ -6,7 +6,7 @@ from homeassistant.helpers import config_validation as cv
from .config_flow import get_master_gateway
from .const import (
_LOGGER,
CONF_BRIDGEID,
CONF_BRIDGE_ID,
DOMAIN,
NEW_GROUP,
NEW_LIGHT,
@ -27,14 +27,14 @@ SERVICE_CONFIGURE_DEVICE_SCHEMA = vol.All(
vol.Optional(SERVICE_ENTITY): cv.entity_id,
vol.Optional(SERVICE_FIELD): cv.matches_regex("/.*"),
vol.Required(SERVICE_DATA): dict,
vol.Optional(CONF_BRIDGEID): str,
vol.Optional(CONF_BRIDGE_ID): str,
}
),
cv.has_at_least_one_key(SERVICE_ENTITY, SERVICE_FIELD),
)
SERVICE_DEVICE_REFRESH = "device_refresh"
SERVICE_DEVICE_REFRESH_SCHEMA = vol.All(vol.Schema({vol.Optional(CONF_BRIDGEID): str}))
SERVICE_DEVICE_REFRESH_SCHEMA = vol.All(vol.Schema({vol.Optional(CONF_BRIDGE_ID): str}))
async def async_setup_services(hass):
@ -97,7 +97,7 @@ async def async_configure_service(hass, data):
See Dresden Elektroniks REST API documentation for details:
http://dresden-elektronik.github.io/deconz-rest-doc/rest/
"""
bridgeid = data.get(CONF_BRIDGEID)
bridgeid = data.get(CONF_BRIDGE_ID)
field = data.get(SERVICE_FIELD, "")
entity_id = data.get(SERVICE_ENTITY)
data = data[SERVICE_DATA]
@ -119,8 +119,8 @@ async def async_configure_service(hass, data):
async def async_refresh_devices_service(hass, data):
"""Refresh available devices from deCONZ."""
gateway = get_master_gateway(hass)
if CONF_BRIDGEID in data:
gateway = hass.data[DOMAIN][data[CONF_BRIDGEID]]
if CONF_BRIDGE_ID in data:
gateway = hass.data[DOMAIN][data[CONF_BRIDGE_ID]]
groups = set(gateway.api.groups.keys())
lights = set(gateway.api.lights.keys())

View File

@ -5,7 +5,7 @@ import pytest
import voluptuous as vol
from homeassistant.components import deconz
from homeassistant.components.deconz.const import CONF_BRIDGEID
from homeassistant.components.deconz.const import CONF_BRIDGE_ID
from .test_gateway import BRIDGEID, setup_deconz_integration
@ -91,7 +91,7 @@ async def test_configure_service_with_field(hass):
data = {
deconz.services.SERVICE_FIELD: "/light/2",
CONF_BRIDGEID: BRIDGEID,
CONF_BRIDGE_ID: BRIDGEID,
deconz.services.SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20},
}
@ -180,7 +180,7 @@ async def test_service_refresh_devices(hass):
"""Test that service can refresh devices."""
gateway = await setup_deconz_integration(hass)
data = {CONF_BRIDGEID: BRIDGEID}
data = {CONF_BRIDGE_ID: BRIDGEID}
with patch(
"pydeconz.DeconzSession.request",