Home Information - Your home, your data, organized.
Python
Frigate: correct _reload_implementation health claims (#349)
Tony Cfaa25ef67cbe
diff --git a/src/hi/services/frigate/frigate_manager.py b/src/hi/services/frigate/frigate_manager.pyindex cdbc2999..c8b816ad 100644--- a/src/hi/services/frigate/frigate_manager.py+++ b/src/hi/services/frigate/frigate_manager.py@@ -5,7 +5,6 @@ from typing import Dict, List, Optionalfrom asgiref.sync import sync_to_asyncfrom hi.apps.common.utils import str_to_bool-from hi.apps.system.enums import ApiHealthStatusTypefrom hi.apps.common.singleton_manager import SingletonManagerfrom hi.apps.system.aggregate_health_provider import AggregateHealthProvider@@ -89,10 +88,10 @@ class FrigateManager( SingletonManager, AggregateHealthProvider, ApiHealthStatusintegration_id = FrigateMetaData.integration_id,)except Integration.DoesNotExist:- self.update_api_health_status( ApiHealthStatusType.DISABLED )+ self.record_disabled( 'Frigate integration disabled' )returnif not integration.is_enabled:- self.update_api_health_status( ApiHealthStatusType.DISABLED )+ self.record_disabled( 'Frigate integration disabled' )returnintegration_attributes = list( integration.attributes.all() )self._attribute_map = self._build_attribute_map(@@ -104,9 +103,9 @@ class FrigateManager( SingletonManager, AggregateHealthProvider, ApiHealthStatus)except Exception:logger.exception( 'Failed to build Frigate client.' )- self.update_api_health_status( ApiHealthStatusType.UNAVAILABLE )+ self.record_error( 'Failed to build Frigate client' )return- self.update_api_health_status( ApiHealthStatusType.HEALTHY )+ self.record_healthy( 'Reloaded' )return@staticmethoddiff --git a/src/hi/services/frigate/tests/test_frigate_integration.py b/src/hi/services/frigate/tests/test_frigate_integration.pyindex a007570e..1c61df7d 100644--- a/src/hi/services/frigate/tests/test_frigate_integration.py+++ b/src/hi/services/frigate/tests/test_frigate_integration.py@@ -18,7 +18,7 @@ from django.test import TestCasefrom hi.apps.entity.enums import EntityTypefrom hi.apps.entity.models import Entity-from hi.apps.system.enums import ApiHealthStatusType+from hi.apps.system.enums import ApiHealthStatusType, HealthStatusTypefrom hi.integrations.models import Integrationfrom hi.apps.entity.enums import VideoStreamType@@ -286,32 +286,36 @@ class TestFrigateGatewayVideoStream( TestCase ):class TestFrigateManagerHealthRecording( TestCase ):- """``FrigateManager._reload_implementation`` must record its own- API-health outcome on every reload. The manager registers itself- as an API health provider; without an explicit record_* call the- default UNKNOWN status leaks into the aggregate and the UI banner- shows WARNING even when the monitor is reporting Healthy.-- Mirrors ``ZoneMinderManager._reload_implementation`` — same bug- pattern, same fix shape."""+ """``FrigateManager._reload_implementation`` records the manager's+ own health based on what reload actually does (DB read + client+ construction). The api_health_status slot is reserved for actual+ API-call outcomes set by ``api_call_context`` wrappers; reload+ never touches it because reload doesn't call the API."""def setUp(self):- # Fresh manager per test — the singleton retains health state- # across tests otherwise.+ # FrigateManager is a singleton — health state from prior tests+ # leaks otherwise. Force a fresh provider setup on both slots.self.manager = FrigateManager()+ self.manager._health_status = self.manager.initial_health_status+ if hasattr( self.manager, '_api_health_status' ):+ del self.manager._api_health_status- def test_no_integration_row_marks_disabled(self):+ def test_no_integration_row_records_disabled(self):Integration.objects.filter(integration_id = FrigateMetaData.integration_id,).delete()self.manager._reload_implementation()+ self.assertEqual(+ self.manager.health_status._base_status,+ HealthStatusType.DISABLED,+ )self.assertEqual(self.manager.api_health_status.status,- ApiHealthStatusType.DISABLED,+ ApiHealthStatusType.UNKNOWN,)self.assertIsNone( self.manager._frigate_client )- def test_disabled_integration_marks_disabled(self):+ def test_disabled_integration_records_disabled(self):integration, _ = Integration.objects.get_or_create(integration_id = FrigateMetaData.integration_id,defaults = { 'is_enabled': False },@@ -319,12 +323,17 @@ class TestFrigateManagerHealthRecording( TestCase ):integration.is_enabled = Falseintegration.save()self.manager._reload_implementation()+ self.assertEqual(+ self.manager.health_status._base_status,+ HealthStatusType.DISABLED,+ )self.assertEqual(self.manager.api_health_status.status,- ApiHealthStatusType.DISABLED,+ ApiHealthStatusType.UNKNOWN,)+ self.assertIsNone( self.manager._frigate_client )- def test_successful_reload_marks_healthy(self):+ def test_successful_reload_records_manager_healthy_and_leaves_api_unknown(self):integration, _ = Integration.objects.get_or_create(integration_id = FrigateMetaData.integration_id,defaults = { 'is_enabled': True },@@ -336,13 +345,17 @@ class TestFrigateManagerHealthRecording( TestCase ):return_value = Mock( base_url = 'http://frigate.example' ),):self.manager._reload_implementation()+ self.assertEqual(+ self.manager.health_status._base_status,+ HealthStatusType.HEALTHY,+ )self.assertEqual(self.manager.api_health_status.status,- ApiHealthStatusType.HEALTHY,+ ApiHealthStatusType.UNKNOWN,)self.assertIsNotNone( self.manager._frigate_client )- def test_client_build_failure_marks_unavailable(self):+ def test_client_build_failure_records_manager_error_and_leaves_api_unknown(self):integration, _ = Integration.objects.get_or_create(integration_id = FrigateMetaData.integration_id,defaults = { 'is_enabled': True },@@ -354,12 +367,13 @@ class TestFrigateManagerHealthRecording( TestCase ):side_effect = ValueError( 'Base URL is required.' ),):self.manager._reload_implementation()- # record_error pushes the API status to UNAVAILABLE so the- # aggregate maps the manager's own slot to ERROR rather than- # the leak-through WARNING that UNKNOWN would produce.+ self.assertEqual(+ self.manager.health_status._base_status,+ HealthStatusType.ERROR,+ )self.assertEqual(self.manager.api_health_status.status,- ApiHealthStatusType.UNAVAILABLE,+ ApiHealthStatusType.UNKNOWN,)self.assertIsNone( self.manager._frigate_client )