Skip to content

NVR with realtime local object detection for IP cameras

Fix deleting the only camera when the cameras key has a trailing comment (#24542)

Josh Hawkinse912d0852b3c
diff --git a/frigate/api/camera.py b/frigate/api/camera.py
index bc0a318206..6498901b97 100644
--- a/frigate/api/camera.py
+++ b/frigate/api/camera.py
@@ -35,7 +35,11 @@ from frigate.config.camera.updater import (
)
from frigate.config.env import substitute_frigate_vars
from frigate.models import User
-from frigate.util.builtin import clean_camera_user_pass, get_record_segment_time
+from frigate.util.builtin import (
+ clean_camera_user_pass,
+ clear_orphaned_comments,
+ get_record_segment_time,
+)
from frigate.util.camera_cleanup import cleanup_camera_db, cleanup_camera_files
from frigate.util.config import find_config_file
from frigate.util.image import run_ffmpeg_snapshot
@@ -1201,6 +1205,7 @@ async def delete_camera(
# Remove camera from config
if "cameras" in data and camera_name in data["cameras"]:
del data["cameras"][camera_name]
+ clear_orphaned_comments(data["cameras"], data, "cameras")
# Remove camera from auth roles
auth = data.get("auth", {})
diff --git a/frigate/test/test_update_yaml.py b/frigate/test/test_update_yaml.py
index e9e160c8f4..2f06b480f2 100644
--- a/frigate/test/test_update_yaml.py
+++ b/frigate/test/test_update_yaml.py
@@ -6,7 +6,7 @@ import unittest
from ruamel.yaml import YAML
-from frigate.util.builtin import update_yaml_file_bulk
+from frigate.util.builtin import clear_orphaned_comments, update_yaml_file_bulk
class TestUpdateYaml(unittest.TestCase):
@@ -178,6 +178,27 @@ class TestUpdateYaml(unittest.TestCase):
assert data["cameras"]["cam1"]["detect"]["fps"] == 5
assert "# tuned for the pi" in self._read()
+ def test_delete_only_camera_with_comment_on_cameras_key(self):
+ """Deleting the only camera stays parseable when the cameras key
+ carries the default config's trailing comment."""
+ self._write(
+ "cameras: # No cameras defined, UI wizard should be used\n"
+ " cam1:\n"
+ " enabled: true\n"
+ "version: 0.18-0\n"
+ )
+ yaml = YAML()
+ with open(self.config_path) as f:
+ data = yaml.load(f)
+ del data["cameras"]["cam1"]
+ clear_orphaned_comments(data["cameras"], data, "cameras")
+ with open(self.config_path, "w") as f:
+ yaml.dump(data, f)
+
+ data = self._load()
+ assert data["cameras"] == {}
+ assert data["version"] == "0.18-0"
+
if __name__ == "__main__":
unittest.main(verbosity=2)