Keep UniFi Protect fobs, relays and sirens from removal in hybrid mode (#186404)

This commit is contained in:
Raphael Hehl
2026-10-18 12:02:24 +02:00
committed by GitHub
parent 75506b68d2
commit faff7dfd47
2 changed files with 46 additions and 8 deletions
@@ -375,15 +375,16 @@ async def async_remove_config_entry_device(
if connection[0] == dr.CONNECTION_NETWORK_MAC
}
api = config_entry.runtime_data.api
# all_devices() walks every public device family (and the NVR), including
# the ones the private bootstrap does not have, like fobs and relays.
if any(
device.mac and _async_unifi_mac_from_hass(device.mac) in unifi_macs
for device in api.public_bootstrap.all_devices(include_nvr=True)
if isinstance(device, PublicDeviceModel | PublicNVR)
):
return False
if api.is_public_only:
# No private bootstrap: judge against the public cache. all_devices()
# walks every public device family (and the NVR), so new families are
# covered without changes here.
return not any(
device.mac and _async_unifi_mac_from_hass(device.mac) in unifi_macs
for device in api.public_bootstrap.all_devices(include_nvr=True)
if isinstance(device, PublicDeviceModel | PublicNVR)
)
return True
if api.bootstrap.nvr.mac in unifi_macs:
return False
for device in async_get_devices(api.bootstrap, DEVICES_THAT_ADOPT):
@@ -12,8 +12,12 @@ from uiprotect.data import (
NVR,
Bootstrap,
CloudAccount,
Fob,
Light,
LinkStation,
NvrArmModeStatus,
Relay,
Siren,
Version,
)
from uiprotect.data.public_devices import PublicCamera
@@ -449,6 +453,39 @@ async def test_device_remove_devices_nvr(
assert not response["success"]
@pytest.mark.parametrize(
("store", "model"),
[
pytest.param("fobs", Fob, id="fob"),
pytest.param("relays", Relay, id="relay"),
pytest.param("sirens", Siren, id="siren"),
pytest.param("alarm_hubs", LinkStation, id="alarm_hub"),
],
)
async def test_device_remove_public_only_family(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
ufp: MockUFPFixture,
light: Light,
store: str,
model: type,
) -> None:
"""A device only the public cache knows refuses removal while it exists."""
await init_entry(hass, ufp, [light])
device = Mock(spec=model)
device.mac = "aa:bb:cc:dd:ee:02"
setattr(ufp.api.public_bootstrap, store, {"device-id": device})
device_entry = device_registry.async_get_or_create(
config_entry_id=ufp.entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "AABBCCDDEE02")},
)
assert not await async_remove_config_entry_device(hass, ufp.entry, device_entry)
setattr(ufp.api.public_bootstrap, store, {})
assert await async_remove_config_entry_device(hass, ufp.entry, device_entry)
async def test_remove_config_entry_device_rejects_child_device(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,