From 1ffd24563bb53b11a9b836d1a93db656f370d2f6 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:32:17 -0600 Subject: [PATCH] catch onvif errors in preset and zoom moves and default the video source token GotoPreset, ContinuousMove and AbsoluteMove had no error handling, so a fault left the camera marked active and the exception ended the autotracker's maintenance thread or that camera's move queue. They now log and clear the active flag like the other movers. Init also raised a KeyError when GetVideoSources returned an empty list, because the token was only set on a non-empty result or an exception. It now defaults to None. --- frigate/ptz/onvif.py | 42 +++++++++++++++++++++------------ frigate/test/test_ptz_onvif.py | 43 +++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/frigate/ptz/onvif.py b/frigate/ptz/onvif.py index 3d2a117393..7930e51675 100644 --- a/frigate/ptz/onvif.py +++ b/frigate/ptz/onvif.py @@ -301,13 +301,13 @@ class OnvifController: logger.debug(f"Imaging service not supported for {camera_name}: {e}") imaging = None cam["imaging"] = imaging + cam["video_source_token"] = None try: video_sources = await media.GetVideoSources() if video_sources and len(video_sources) > 0: cam["video_source_token"] = video_sources[0].token except Exception as e: logger.debug(f"Unable to get video sources for {camera_name}: {e}") - cam["video_source_token"] = None # setup continuous moving request move_request = ptz.create_type("ContinuousMove") @@ -660,12 +660,15 @@ class OnvifController: move_request = cam["move_request"] preset_token = cam["presets"][preset] - await cam["ptz"].GotoPreset( - { - "ProfileToken": move_request.ProfileToken, - "PresetToken": preset_token, - } - ) + try: + await cam["ptz"].GotoPreset( + { + "ProfileToken": move_request.ProfileToken, + "PresetToken": preset_token, + } + ) + except Exception as e: + logger.warning(f"Onvif sending preset request to {camera_name} failed: {e}") cam["active"] = False @@ -690,7 +693,11 @@ class OnvifController: elif command == OnvifCommandEnum.zoom_out: move_request.Velocity = {"Zoom": {"x": -0.5}} - await cam["ptz"].ContinuousMove(move_request) + try: + await cam["ptz"].ContinuousMove(move_request) + except Exception as e: + logger.warning(f"Onvif sending zoom request to {camera_name} failed: {e}") + cam["active"] = False async def _zoom_absolute(self, camera_name: str, zoom, speed) -> None: cam = self.cams[camera_name] @@ -729,13 +736,18 @@ class OnvifController: logger.debug(f"{camera_name}: Absolute zoom: {zoom}") - await cam["ptz"].AbsoluteMove( - { - "ProfileToken": cam["move_request"].ProfileToken, - "Position": {"Zoom": zoom}, - "Speed": {"Zoom": speed}, - } - ) + try: + await cam["ptz"].AbsoluteMove( + { + "ProfileToken": cam["move_request"].ProfileToken, + "Position": {"Zoom": zoom}, + "Speed": {"Zoom": speed}, + } + ) + except Exception as e: + logger.warning( + f"Onvif sending absolute zoom request to {camera_name} failed: {e}" + ) cam["active"] = False diff --git a/frigate/test/test_ptz_onvif.py b/frigate/test/test_ptz_onvif.py index a09f7c7d9f..bb81d67d65 100644 --- a/frigate/test/test_ptz_onvif.py +++ b/frigate/test/test_ptz_onvif.py @@ -18,7 +18,7 @@ from unittest.mock import AsyncMock, MagicMock from frigate.camera import PTZMetrics from frigate.config import FrigateConfig from frigate.ptz.autotrack import ptz_moving_at_frame_time -from frigate.ptz.onvif import OnvifController +from frigate.ptz.onvif import OnvifCommandEnum, OnvifController CAMERA = "ptz_cam" @@ -157,6 +157,14 @@ class TestOnvifInitRequests(unittest.IsolatedAsyncioTestCase): ptz.GetServiceCapabilities.assert_not_called() ptz.GetStatus.assert_not_called() + async def test_init_completes_without_video_sources(self) -> None: + controller = _make_controller(autotracking_enabled=False) + media = await controller.cams[CAMERA]["onvif"].create_media_service() + media.GetVideoSources.return_value = [] + + self.assertTrue(await controller._init_onvif(CAMERA)) + self.assertIsNone(controller.cams[CAMERA]["video_source_token"]) + class TestManualRelativeMoveMetrics(unittest.IsolatedAsyncioTestCase): """A manual move from the UI (click to move, drag to zoom) sends move_relative @@ -204,6 +212,39 @@ class TestManualRelativeMoveMetrics(unittest.IsolatedAsyncioTestCase): self.assertFalse(controller.cams[CAMERA]["active"]) + async def test_failed_preset_and_zoom_moves_do_not_raise(self) -> None: + # camera_maintenance and the move queue consumer end on an exception + controller = _make_move_controller(autotracking_enabled=True) + cam = controller.cams[CAMERA] + cam["features"] += ["zoom", "zoom-a"] + cam["presets"] = {"home": "token_1"} + cam["move_request"] = MagicMock() + cam["absolute_zoom_range"] = {"XRange": {"Min": 0.0, "Max": 1.0}} + cam["ptz"].GotoPreset = AsyncMock(side_effect=TimeoutError) + cam["ptz"].ContinuousMove = AsyncMock(side_effect=TimeoutError) + cam["ptz"].AbsoluteMove = AsyncMock(side_effect=TimeoutError) + + moves = { + "preset": ( + controller._move_to_preset(CAMERA, "home"), + cam["ptz"].GotoPreset, + ), + "zoom": ( + controller._zoom(CAMERA, OnvifCommandEnum.zoom_in), + cam["ptz"].ContinuousMove, + ), + "zoom_absolute": ( + controller._zoom_absolute(CAMERA, 0.5, 1), + cam["ptz"].AbsoluteMove, + ), + } + + for name, (move, request) in moves.items(): + with self.subTest(move=name): + await move + request.assert_awaited_once() + self.assertFalse(cam["active"]) + async def test_metrics_written_when_autotracking_enabled(self) -> None: # get_camera_status resets stop_time once the camera reports IDLE, so the # autotracking path keeps its motion estimation timestamps