From cf1e700d39403f2473407bfbd8da9799076cbfd6 Mon Sep 17 00:00:00 2001 From: Raphael Hehl <7577984+RaHehl@users.noreply.github.com> Date: Sun, 11 Oct 2026 12:50:19 +0200 Subject: [PATCH] Fix UniFi Protect alarm hubs added at runtime and without a name (#185774) --- .../components/unifiprotect/binary_sensor.py | 31 +------ homeassistant/components/unifiprotect/data.py | 7 +- .../components/unifiprotect/entity.py | 2 +- .../components/unifiprotect/sensor.py | 2 + .../components/unifiprotect/strings.json | 3 - .../snapshots/test_alarm_hub.ambr | 4 +- .../components/unifiprotect/test_alarm_hub.py | 83 ++++++++++++++++++- 7 files changed, 95 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/unifiprotect/binary_sensor.py b/homeassistant/components/unifiprotect/binary_sensor.py index 179bffda8590..6a9db1d48700 100644 --- a/homeassistant/components/unifiprotect/binary_sensor.py +++ b/homeassistant/components/unifiprotect/binary_sensor.py @@ -76,32 +76,6 @@ _ALARM_HUB_INPUT_DEVICE_CLASS: dict[AlarmHubInputType, BinarySensorDeviceClass] } -def _async_motion_sensor_enabled_public(obj: PublicDeviceModel) -> bool: - # Mirrors Sensor.is_motion_sensor_enabled over the public API. - sensor = cast(PublicSensor, obj) - return sensor.mount_type is not MountType.LEAK and sensor.motion_settings.is_enabled - - -def _async_contact_sensor_enabled_public(obj: PublicDeviceModel) -> bool: - # Mirrors Sensor.is_contact_sensor_enabled over the public API. - return cast(PublicSensor, obj).is_contact_sensor_enabled - - -def _async_leak_sensor_enabled_public(obj: PublicDeviceModel) -> bool: - # Leak-mounted (UP Sense), or the capability map advertises water_leak with a - # leak channel enabled — the USL family detects leaks without a leak mount. - # Settings alone are not a valid gate: sensors without the capability report - # inert default leak settings. - sensor = cast(PublicSensor, obj) - return sensor.is_leak_sensor_enabled or ( - sensor.supports(SensorFeatureCapability.WATER_LEAK) - and ( - sensor.leak_settings.is_internal_enabled - or sensor.leak_settings.is_external_enabled - ) - ) - - _RELAY_INPUT_STATE_MAP: dict[RelayInputState, bool] = { RelayInputState.ON: True, RelayInputState.OFF: False, @@ -858,13 +832,11 @@ def _alarm_hub_battery_problem(hub: LinkStation) -> bool: ALARM_HUB_BINARY_SENSORS: tuple[ProtectAlarmHubBinaryEntityDescription, ...] = ( ProtectAlarmHubBinaryEntityDescription( key="tamper", - translation_key="alarm_hub_tamper", device_class=BinarySensorDeviceClass.TAMPER, value_fn=_alarm_hub_tamper, ), ProtectAlarmHubBinaryEntityDescription( key="battery", - translation_key="alarm_hub_battery", device_class=BinarySensorDeviceClass.BATTERY, entity_category=EntityCategory.DIAGNOSTIC, value_fn=_alarm_hub_battery_problem, @@ -1005,7 +977,8 @@ async def async_setup_entry( ) return if isinstance(device, LinkStation): - async_add_entities(_async_alarm_hub_entities(data, device)) + if device.is_alarm_hub: + async_add_entities(_async_alarm_hub_entities(data, device)) return async_add_entities(_async_model_entities(data, public_device=device)) diff --git a/homeassistant/components/unifiprotect/data.py b/homeassistant/components/unifiprotect/data.py index 77b6cc9e559c..d1a4137318db 100644 --- a/homeassistant/components/unifiprotect/data.py +++ b/homeassistant/components/unifiprotect/data.py @@ -85,7 +85,12 @@ def _async_dispatch_id(entry: UFPConfigEntry, dispatch: str) -> str: # Device families the public API alone provides; the private bootstrap has no # store for them, so hybrid has no adopt path either and their add always goes # through the public add signal. -_PUBLIC_ONLY_MODELS = {ModelType.FOB, ModelType.RELAY, ModelType.SIREN} +_PUBLIC_ONLY_MODELS = { + ModelType.FOB, + ModelType.LINK_STATION, + ModelType.RELAY, + ModelType.SIREN, +} def _pair_public_private[ diff --git a/homeassistant/components/unifiprotect/entity.py b/homeassistant/components/unifiprotect/entity.py index 44bad7bdbba3..438621947eb4 100644 --- a/homeassistant/components/unifiprotect/entity.py +++ b/homeassistant/components/unifiprotect/entity.py @@ -871,7 +871,7 @@ class BaseAlarmHubEntity(Entity): identifiers={(DOMAIN, hub.mac)}, manufacturer=DEFAULT_BRAND, model="Alarm Hub", - name=hub.name, + name=hub.name or f"Alarm Hub {hub.mac}", via_device_id=data.nvr_device_id, ) self._async_update_attrs(hub) diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py index 565a137cdf60..0e7394508012 100644 --- a/homeassistant/components/unifiprotect/sensor.py +++ b/homeassistant/components/unifiprotect/sensor.py @@ -758,6 +758,8 @@ def _async_public_entities( ProtectFobSensor(data, device, description) for description in FOB_SENSORS ] if isinstance(device, LinkStation): + if not device.is_alarm_hub: + return [] return [ ProtectAlarmHubSensor(data, device, description) for description in ALARM_HUB_SENSORS diff --git a/homeassistant/components/unifiprotect/strings.json b/homeassistant/components/unifiprotect/strings.json index 9392038652e6..f05347b0df00 100644 --- a/homeassistant/components/unifiprotect/strings.json +++ b/homeassistant/components/unifiprotect/strings.json @@ -175,9 +175,6 @@ "alarm_hub_zone": { "name": "Zone {zone}" }, - "alarm_sound_detection": { - "name": "Alarm sound detection" - }, "animal_detected": { "name": "Animal detected" }, diff --git a/tests/components/unifiprotect/snapshots/test_alarm_hub.ambr b/tests/components/unifiprotect/snapshots/test_alarm_hub.ambr index d677c9986d9c..55355e7923c4 100644 --- a/tests/components/unifiprotect/snapshots/test_alarm_hub.ambr +++ b/tests/components/unifiprotect/snapshots/test_alarm_hub.ambr @@ -98,7 +98,7 @@ 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': 'alarm_hub_battery', + 'translation_key': None, 'unique_id': 'AABBCCDDEEFF_battery', 'unit_of_measurement': None, }) @@ -306,7 +306,7 @@ 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': 'alarm_hub_tamper', + 'translation_key': None, 'unique_id': 'AABBCCDDEEFF_tamper', 'unit_of_measurement': None, }) diff --git a/tests/components/unifiprotect/test_alarm_hub.py b/tests/components/unifiprotect/test_alarm_hub.py index b1a2cb482a72..3f954af79ec0 100644 --- a/tests/components/unifiprotect/test_alarm_hub.py +++ b/tests/components/unifiprotect/test_alarm_hub.py @@ -369,14 +369,19 @@ async def test_alarm_hub_disconnected_battery( assert voltage.state == "unknown" +@pytest.mark.parametrize( + "is_public_only", + [pytest.param(False, id="hybrid"), pytest.param(True, id="api_key_only")], +) async def test_alarm_hub_added_at_runtime( hass: HomeAssistant, entity_registry: er.EntityRegistry, ufp: MockUFPFixture, alarm_hub: LinkStation, + is_public_only: bool, ) -> None: """A hub adopted after setup is discovered from its public add frame.""" - ufp.api.is_public_only = True + ufp.api.is_public_only = is_public_only ufp.api.has_public_bootstrap = True pb = _make_public_bootstrap(None) ufp.api.public_bootstrap = pb @@ -401,6 +406,82 @@ async def test_alarm_hub_added_at_runtime( ) +async def test_alarm_hub_known_at_setup_not_added_again( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + ufp_with_alarm_hub: MockUFPFixture, + alarm_hub: LinkStation, +) -> None: + """An add frame for a hub that existed at setup does not add it twice.""" + await init_entry(hass, ufp_with_alarm_hub, []) + entity_ids = hass.states.async_entity_ids() + + msg = public_device_ws_message(alarm_hub) + msg.action = WSAction.ADD + assert ufp_with_alarm_hub.devices_ws_subscription is not None + ufp_with_alarm_hub.devices_ws_subscription(msg) + await hass.async_block_till_done() + + assert hass.states.async_entity_ids() == entity_ids + assert "does not generate unique IDs" not in caplog.text + + +@pytest.mark.parametrize( + "is_public_only", + [pytest.param(False, id="hybrid"), pytest.param(True, id="api_key_only")], +) +async def test_plain_link_station_added_at_runtime( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + ufp: MockUFPFixture, + alarm_hub: LinkStation, + is_public_only: bool, +) -> None: + """A link station that is not an alarm hub gets no alarm hub entities.""" + link_station = alarm_hub.model_copy(update={"is_alarm_hub": False}) + ufp.api.is_public_only = is_public_only + ufp.api.has_public_bootstrap = True + pb = _make_public_bootstrap(None) + ufp.api.public_bootstrap = pb + ufp.api.update_public = AsyncMock(return_value=pb) + + await init_entry(hass, ufp, []) + msg = public_device_ws_message(link_station) + msg.action = WSAction.ADD + assert ufp.devices_ws_subscription is not None + ufp.devices_ws_subscription(msg) + await hass.async_block_till_done() + + assert ( + device_registry.async_get_device_by_identifier( + (DOMAIN, ALARM_HUB_MAC), ufp.entry.entry_id + ) + is None + ) + assert hass.states.get("sensor.alarm_hub_battery_voltage") is None + + +async def test_alarm_hub_without_name( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + ufp: MockUFPFixture, + alarm_hub: LinkStation, +) -> None: + """A hub without a name falls back to its MAC for the device name.""" + unnamed = alarm_hub.model_copy(update={"name": None}) + ufp.api.has_public_bootstrap = True + ufp.api.public_bootstrap = _make_public_bootstrap(unnamed) + + await init_entry(hass, ufp, []) + + device = device_registry.async_get_device_by_identifier( + (DOMAIN, ALARM_HUB_MAC), ufp.entry.entry_id + ) + assert device is not None + assert device.name == f"Alarm Hub {ALARM_HUB_MAC}" + assert hass.states.get(f"sensor.alarm_hub_{ALARM_HUB_MAC.lower()}_battery_voltage") + + async def test_alarm_hub_unavailable_when_public_bootstrap_lost( hass: HomeAssistant, ufp_with_alarm_hub: MockUFPFixture,