Fix Hue setup failing when devices share a Zigbee MAC (#184672)

This commit is contained in:
Franck Nijhof
2026-10-07 12:44:24 +02:00
committed by GitHub
parent a0961e66d7
commit 4d94658695
2 changed files with 140 additions and 5 deletions
+27 -4
View File
@@ -35,6 +35,9 @@ async def async_setup_devices(bridge: HueBridge):
api: HueBridgeV2 = bridge.api # to satisfy typing
dev_reg = dr.async_get(hass)
dev_controller = api.devices
# Multi-channel Zigbee devices show up as several Hue devices sharing one MAC,
# while a connection must be unique within the config entry.
mac_owners: dict[str, str] = {}
@callback
def add_device(hue_resource: Device | Room | Zone | ServiceGroup) -> dr.DeviceEntry:
@@ -80,13 +83,20 @@ async def async_setup_devices(bridge: HueBridge):
)
zigbee = dev_controller.get_zigbee_connectivity(hue_resource.id)
if zigbee and zigbee.mac_address:
params[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, zigbee.mac_address)}
mac_owner = mac_owners.setdefault(zigbee.mac_address, hue_resource.id)
if mac_owner == hue_resource.id:
params[ATTR_CONNECTIONS] = {
(dr.CONNECTION_NETWORK_MAC, zigbee.mac_address)
}
return dev_reg.async_get_or_create(config_entry_id=entry.entry_id, **params)
@callback
def remove_device(hue_device_id: str) -> None:
"""Remove device from registry."""
for mac, owner in list(mac_owners.items()):
if owner == hue_device_id:
del mac_owners[mac]
if device := dev_reg.async_get_device_by_identifier(
(DOMAIN, hue_device_id), entry.entry_id
):
@@ -104,10 +114,23 @@ async def async_setup_devices(bridge: HueBridge):
# updates to existing device will also be handled by this call
add_device(hue_resource)
def registration_order(hue_device: Device) -> tuple[bool, bool]:
"""Sort the bridge first, then devices already in the registry.
A new device claiming a MAC that a known device holds would be merged
into that device by the registry.
"""
is_bridge = hue_device.id == api.config.bridge_device.id
is_known = (
dev_reg.async_get_device_by_identifier(
(DOMAIN, hue_device.id), entry.entry_id
)
is not None
)
return (not is_bridge, not is_known)
# create/update all current devices found in controllers
# sort the devices to ensure bridges are added first
hue_devices = list(dev_controller)
hue_devices.sort(key=lambda dev: dev.id != api.config.bridge_device.id)
hue_devices = sorted(dev_controller, key=registration_order)
known_devices = [add_device(hue_device) for hue_device in hue_devices]
known_devices += [add_device(hue_room) for hue_room in api.groups.room]
known_devices += [add_device(hue_zone) for hue_zone in api.groups.zone]
+113 -1
View File
@@ -15,12 +15,14 @@ from homeassistant.components.hue.const import (
CONF_ALLOW_UNREACHABLE,
DOMAIN,
)
from homeassistant.components.hue.v2.device import async_setup_devices
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.util.json import JsonArrayType
from .conftest import setup_platform
from .conftest import create_config_entry, replace_resources, setup_platform
from .const import FAKE_DEVICE, FAKE_ZIGBEE_CONNECTIVITY
from .test_light_v1 import LIGHT_RESPONSE
from tests.common import MockConfigEntry, async_capture_events
@@ -147,6 +149,116 @@ async def test_bridge_device_v2(
assert len(create_events) == 1
# Two lights in the v2 fixture; the first one's MAC is shared in the tests below
SHARED_MAC_DEVICE_ID = "0b216218-d811-4c95-8c55-bbcda50f9d50"
SECOND_DEVICE_ID = "b9e76da7-ac22-476a-986d-e466e62e962f"
SHARED_MAC = "00:17:88:01:09:aa:bb:65"
@pytest.mark.parametrize(
("registered_before", "mac_owner_id", "other_device_id"),
[
pytest.param({}, SHARED_MAC_DEVICE_ID, SECOND_DEVICE_ID, id="fresh_install"),
pytest.param(
{SHARED_MAC_DEVICE_ID: False, SECOND_DEVICE_ID: False},
SHARED_MAC_DEVICE_ID,
SECOND_DEVICE_ID,
id="devices_already_registered",
),
pytest.param(
{SECOND_DEVICE_ID: True},
SECOND_DEVICE_ID,
SHARED_MAC_DEVICE_ID,
id="new_device_listed_before_registered_mac_holder",
),
],
)
async def test_devices_sharing_a_mac_v2(
hass: HomeAssistant,
mock_bridge_v2: Mock,
v2_resources_test_data: JsonArrayType,
device_registry: dr.DeviceRegistry,
registered_before: dict[str, bool],
mac_owner_id: str,
other_device_id: str,
) -> None:
"""Test setting up Hue devices that share one Zigbee MAC.
Multi-channel controllers (like GLEDOPTO or Dresden FLS-PP3) show up as
several Hue devices with the same MAC address.
"""
await mock_bridge_v2.api.load_test_data(
replace_resources(
v2_resources_test_data,
[
{
"id": "717afeb6-b1ce-426e-96de-48e8fe037fb0",
"id_v1": "/lights/16",
"mac_address": SHARED_MAC,
"owner": {"rid": SECOND_DEVICE_ID, "rtype": "device"},
"status": "connected",
"type": "zigbee_connectivity",
}
],
)
)
config_entry = create_config_entry(api_version=2)
config_entry.add_to_hass(hass)
mock_bridge_v2.config_entry = config_entry
for hue_device_id, holds_mac in registered_before.items():
device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, hue_device_id)},
connections={(dr.CONNECTION_NETWORK_MAC, SHARED_MAC)}
if holds_mac
else set(),
)
await async_setup_devices(mock_bridge_v2)
mac_owner = device_registry.async_get_device_by_identifier(
(DOMAIN, mac_owner_id), config_entry.entry_id
)
other_device = device_registry.async_get_device_by_identifier(
(DOMAIN, other_device_id), config_entry.entry_id
)
assert mac_owner is not None
assert other_device is not None
assert mac_owner.id != other_device.id
assert mac_owner.connections == {(dr.CONNECTION_NETWORK_MAC, SHARED_MAC)}
assert other_device.connections == set()
async def test_device_sharing_a_mac_added_v2(
hass: HomeAssistant,
mock_bridge_v2: Mock,
v2_resources_test_data: JsonArrayType,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test a device added at runtime that shares a MAC with a known device."""
await mock_bridge_v2.api.load_test_data(v2_resources_test_data)
await setup_platform(hass, mock_bridge_v2, [])
mock_bridge_v2.api.emit_event(
"add", {**FAKE_ZIGBEE_CONNECTIVITY, "mac_address": SHARED_MAC}
)
mock_bridge_v2.api.emit_event("add", FAKE_DEVICE)
await hass.async_block_till_done()
entry_id = mock_bridge_v2.config_entry.entry_id
first_device = device_registry.async_get_device_by_identifier(
(DOMAIN, SHARED_MAC_DEVICE_ID), entry_id
)
added_device = device_registry.async_get_device_by_identifier(
(DOMAIN, FAKE_DEVICE["id"]), entry_id
)
assert first_device is not None
assert added_device is not None
assert first_device.id != added_device.id
assert first_device.connections == {(dr.CONNECTION_NETWORK_MAC, SHARED_MAC)}
assert added_device.connections == set()
async def test_bridge_setup_v2(hass: HomeAssistant, mock_api_v2: Mock) -> None:
"""Test a successful setup for V2 bridge."""
config_entry = MockConfigEntry(