diff --git a/homeassistant/components/unifiprotect/camera.py b/homeassistant/components/unifiprotect/camera.py index 08d69da84612..1f9c9a8d27ea 100644 --- a/homeassistant/components/unifiprotect/camera.py +++ b/homeassistant/components/unifiprotect/camera.py @@ -57,6 +57,7 @@ def _create_rtsp_repair( hass, DOMAIN, f"rtsp_disabled_{public.id}", + config_entry_id=entry.entry_id, is_fixable=True, is_persistent=False, learn_more_url="https://www.home-assistant.io/integrations/unifiprotect/#camera-streams", @@ -80,6 +81,9 @@ def _async_camera_entities( Stream URLs come from the public API because it carries the authoritative per-camera host (stacked consoles resolve correctly), SRTP-stripped for go2rtc. + + Paths which can't tell whether a camera's RTSP repair issue is still valid + keep it, so a refresh of the issues doesn't delete it. """ disable_stream = data.disable_stream entities: list[ProtectDeviceEntity] = [] @@ -113,8 +117,10 @@ def _async_camera_entities( camera.display_name, ) data.async_add_pending_camera_id(camera.id) + ir.async_keep_issue(hass, DOMAIN, f"rtsp_disabled_{camera.id}") continue + issue_id = f"rtsp_disabled_{public.id}" # Hybrid: a camera not yet in the private bootstrap (adopt race) is # skipped rather than built private-less — the adopt dispatch creates # it with its private fill, which would otherwise collide on unique_id. @@ -123,10 +129,10 @@ def _async_camera_entities( "Deferring camera %s until its private object is adopted", public.display_name, ) + ir.async_keep_issue(hass, DOMAIN, issue_id) continue streams = data.get_rtsps_streams(public.id) - issue_id = f"rtsp_disabled_{public.id}" tiers = public.hardware_stream_qualities() main_qualities = [q for q in _MAIN_QUALITIES if q in tiers] has_package = ChannelQuality.PACKAGE in tiers @@ -139,6 +145,7 @@ def _async_camera_entities( public.display_name, tiers, ) + ir.async_keep_issue(hass, DOMAIN, issue_id) continue # Active stream tiers come from the public ``rtsps_streams`` object. @@ -189,6 +196,7 @@ def _async_camera_entities( ), public.display_name, ) + ir.async_keep_issue(hass, DOMAIN, issue_id) else: _create_rtsp_repair(hass, entry, public) return entities @@ -223,7 +231,16 @@ async def async_setup_entry( async_dispatcher_connect(hass, data.channels_signal, _add_new_device) ) - async_add_entities(_async_camera_entities(hass, entry, data)) + # Without the public bootstrap no cameras are enumerated, so the RTSP repair + # issues can't be checked + if not data.api.has_public_bootstrap: + return + # Cameras which are gone or no longer adopted leave issues to be deleted + with ir.async_begin_issues_refresh( + hass, DOMAIN, issue_id_pattern="rtsp_disabled_", config_entry_id=entry.entry_id + ): + entities = _async_camera_entities(hass, entry, data) + async_add_entities(entities) _DISABLE_FEATURE = CameraEntityFeature(0) diff --git a/tests/components/unifiprotect/test_camera.py b/tests/components/unifiprotect/test_camera.py index 9070d324fb08..fd2855cba03a 100644 --- a/tests/components/unifiprotect/test_camera.py +++ b/tests/components/unifiprotect/test_camera.py @@ -6,6 +6,7 @@ from unittest.mock import AsyncMock, Mock, patch from aiohttp.client_exceptions import ServerDisconnectedError import pytest +from pytest_homeassistant.common import MockConfigEntry, async_capture_events from uiprotect.api import RTSPSStreams from uiprotect.data import ( Camera as ProtectCamera, @@ -1136,3 +1137,106 @@ async def test_public_only_camera_end_to_end( == camera.channels[0].rtsps_no_srtp_url ) assert len(hass.states.async_entity_ids(Platform.ALARM_CONTROL_PANEL.value)) == 1 + + +def _prime_streams_unreadable(ufp: MockUFPFixture, camera: ProtectCamera) -> None: + """Make the next prime fail to read the camera's RTSPS streams.""" + + async def _prime() -> Any: + pb = ufp.api.public_bootstrap + public = make_public_camera(camera) + public.rtsps_streams = None + pb.cameras = {camera.id: public} + return pb + + ufp.api.update_public = AsyncMock(side_effect=_prime) + + +def _prime_not_mirrored(ufp: MockUFPFixture, camera: ProtectCamera) -> None: + """Make the next prime leave the camera out of the public bootstrap.""" + + async def _prime() -> Any: + pb = ufp.api.public_bootstrap + pb.cameras = {} + return pb + + ufp.api.update_public = AsyncMock(side_effect=_prime) + + +@pytest.mark.parametrize( + "prepare_reload", + [ + pytest.param(lambda ufp, camera: None, id="still_valid"), + pytest.param(_prime_streams_unreadable, id="streams_unreadable"), + pytest.param(_prime_not_mirrored, id="not_mirrored"), + ], +) +async def test_ignored_rtsp_repair_kept_on_reload( + hass: HomeAssistant, + ufp: MockUFPFixture, + camera: ProtectCamera, + issue_registry: ir.IssueRegistry, + prepare_reload: Callable[[MockUFPFixture, ProtectCamera], None], +) -> None: + """An ignored RTSP repair is kept when it's still valid or can't be checked.""" + camera.channels = [c.model_copy() for c in camera.channels] + for channel in camera.channels: + channel.is_rtsp_enabled = False + await init_entry(hass, ufp, [camera]) + issue_id = f"rtsp_disabled_{camera.id}" + ir.async_ignore_issue(hass, DOMAIN, issue_id, True) + issue = issue_registry.async_get_issue(DOMAIN, issue_id) + assert issue is not None + dismissed_version = issue.dismissed_version + assert dismissed_version is not None + events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED) + + prepare_reload(ufp, camera) + await hass.config_entries.async_reload(ufp.entry.entry_id) + await hass.async_block_till_done() + + issue = issue_registry.async_get_issue(DOMAIN, issue_id) + assert issue is not None + assert issue.active + assert issue.dismissed_version == dismissed_version + assert not [event for event in events if event.data["action"] == "remove"] + + +async def test_stale_rtsp_repairs_deleted( + hass: HomeAssistant, + ufp: MockUFPFixture, + camera_all: ProtectCamera, + issue_registry: ir.IssueRegistry, +) -> None: + """RTSP repairs of the entry's cameras which are gone are deleted on setup. + + Repairs owned by another NVR's entry, or created before issues were linked to + their config entry, are left alone. + """ + other_entry = MockConfigEntry(domain=DOMAIN) + other_entry.add_to_hass(hass) + for issue_id, config_entry_id in ( + ("rtsp_disabled_gone", ufp.entry.entry_id), + ("rtsp_disabled_other_nvr", other_entry.entry_id), + ("rtsp_disabled_unlinked", None), + ): + ir.async_create_issue( + hass, + DOMAIN, + issue_id, + config_entry_id=config_entry_id, + is_fixable=True, + severity=ir.IssueSeverity.WARNING, + translation_key="rtsp_disabled", + translation_placeholders={"camera": "Camera"}, + data={ + "entry_id": config_entry_id or ufp.entry.entry_id, + "camera_id": issue_id, + }, + ) + + await init_entry(hass, ufp, [camera_all]) + + assert issue_registry.async_get_issue(DOMAIN, "rtsp_disabled_gone") is None + assert issue_registry.async_get_issue(DOMAIN, "rtsp_disabled_other_nvr") + assert issue_registry.async_get_issue(DOMAIN, "rtsp_disabled_unlinked")