From faff7dfd47579a2ce80da12eaf639d57afab0148 Mon Sep 17 00:00:00 2001 From: Raphael Hehl <7577984+RaHehl@users.noreply.github.com> Date: Sun, 18 Oct 2026 12:02:24 +0200 Subject: [PATCH] Keep UniFi Protect fobs, relays and sirens from removal in hybrid mode (#186404) --- .../components/unifiprotect/__init__.py | 17 +++++---- tests/components/unifiprotect/test_init.py | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/unifiprotect/__init__.py b/homeassistant/components/unifiprotect/__init__.py index 5ccdaa7e9ed2..a3c96a547999 100644 --- a/homeassistant/components/unifiprotect/__init__.py +++ b/homeassistant/components/unifiprotect/__init__.py @@ -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): diff --git a/tests/components/unifiprotect/test_init.py b/tests/components/unifiprotect/test_init.py index 22f16436a08c..782e3fc251f0 100644 --- a/tests/components/unifiprotect/test_init.py +++ b/tests/components/unifiprotect/test_init.py @@ -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,