Home Information - Your home, your data, organized.
Python
Added refresh/reload of modules when integration settings are changed.
Anthony Cassandrab7ca0af3d234
diff --git a/src/hi/apps/common/datetimeproxy.py b/src/hi/apps/common/datetimeproxy.pyindex c6bd8381..cebe114c 100644--- a/src/hi/apps/common/datetimeproxy.py+++ b/src/hi/apps/common/datetimeproxy.py@@ -383,3 +383,8 @@ def iso_naive_to_datetime_utc( iso_str : str, tzname : str ):aware_time = to_zone.localize(naive_time)utc_time = aware_time.astimezone( pytz.UTC )return utc_time+++def change_timezone( original_datetime : datetime, new_tzname : str ):+ new_tz = pytz.timezone( new_tzname )+ return original_datetime.astimezone( new_tz )diff --git a/src/hi/integrations/core/integration_factory.py b/src/hi/integrations/core/integration_factory.pyindex 00c114e3..e6e9eb90 100644--- a/src/hi/integrations/core/integration_factory.py+++ b/src/hi/integrations/core/integration_factory.py@@ -3,6 +3,7 @@ from typing import Dict, Listfrom hi.apps.common.singleton import Singletonfrom hi.apps.monitor.monitor_manager import MonitorManager+from hi.apps.monitor.periodic_monitor import PeriodicMonitorfrom .integration_gateway import IntegrationGatewayfrom .models import Integration@@ -15,6 +16,7 @@ class IntegrationFactory( Singleton ):def __init_singleton__( self ):self._integration_gateway_map : Dict[ str, IntegrationGateway ] = dict()+ self._integration_monitor_map : Dict[ str, PeriodicMonitor ] = dict()returndef get_default_integration_data( self ):@@ -56,6 +58,9 @@ class IntegrationFactory( Singleton ):return self._integration_gateway_map[integration_id]raise KeyError( f'Unknown integration id "{integration_id}".' )+ def get_integration_monitor( self, integration_id : str ) -> PeriodicMonitor:+ return self._integration_monitor_map.get( integration_id )+def register( self, integration_gateway : IntegrationGateway ):integration_metadata = integration_gateway.get_meta_data()integration_id = integration_metadata.integration_id@@ -67,7 +72,9 @@ class IntegrationFactory( Singleton ):try:integration = Integration.objects.get( integration_id = integration_id )if integration.is_enabled:- MonitorManager().register( integration_gateway.get_monitor() )+ periodic_monitor = integration_gateway.get_monitor()+ self._integration_monitor_map[integration_id] = periodic_monitor+ MonitorManager().register( periodic_monitor )except Integration.DoesNotExist:passreturndiff --git a/src/hi/integrations/hass/hass_manager.py b/src/hi/integrations/hass/hass_manager.pyindex ee3359eb..54c52c73 100644--- a/src/hi/integrations/hass/hass_manager.py+++ b/src/hi/integrations/hass/hass_manager.py@@ -36,6 +36,7 @@ class HassManager( Singleton ):return self._hass_clientdef reload( self ):+ """ Should be called when integration settings are changed. """if self._is_loading:logger.warning( 'HAss is already loading.' )returndiff --git a/src/hi/integrations/hass/hass_monitor.py b/src/hi/integrations/hass/hass_monitor.pyindex 2f5d422a..f9adf2ad 100644--- a/src/hi/integrations/hass/hass_monitor.py+++ b/src/hi/integrations/hass/hass_monitor.py@@ -23,6 +23,10 @@ class HassMonitor( PeriodicMonitor ):self._logger = logging.getLogger(__name__)return+ def refresh( self ):+ """ Should be called when integration settings are changed. """+ return+async def do_work(self):id_to_hass_state_map = self._hass_manager.fetch_hass_states_from_api( verbose = False )diff --git a/src/hi/integrations/hass/views.py b/src/hi/integrations/hass/views.pyindex 1a82ae04..76c7fb0e 100644--- a/src/hi/integrations/hass/views.py+++ b/src/hi/integrations/hass/views.py@@ -9,6 +9,7 @@ from django.views.generic import Viewfrom hi.integrations.core.forms import IntegrationAttributeFormSetfrom hi.integrations.core.helpers import IntegrationHelperMixin+from hi.integrations.core.integration_factory import IntegrationFactoryfrom hi.integrations.core.views import IntegrationPageViewfrom hi.hi_async_view import HiModalView@@ -132,6 +133,13 @@ class HassSettingsView( View, IntegrationHelperMixin ):with transaction.atomic():integration_attribute_formset.save()+ HassManager().reload()+ hass_monitor = IntegrationFactory().get_integration_monitor(+ integration_id = HassMetaData.integration_id,+ )+ if hass_monitor:+ hass_monitor.refresh()+context = {'integration_attribute_formset': integration_attribute_formset,}diff --git a/src/hi/integrations/zoneminder/views.py b/src/hi/integrations/zoneminder/views.pyindex e52ffe21..60bf6077 100644--- a/src/hi/integrations/zoneminder/views.py+++ b/src/hi/integrations/zoneminder/views.py@@ -8,6 +8,7 @@ import hi.apps.common.antinode as antinodefrom hi.integrations.core.forms import IntegrationAttributeFormSetfrom hi.integrations.core.helpers import IntegrationHelperMixin+from hi.integrations.core.integration_factory import IntegrationFactoryfrom hi.integrations.core.views import IntegrationPageViewfrom hi.hi_async_view import HiModalView@@ -133,6 +134,13 @@ class ZmSettingsView( View, IntegrationHelperMixin ):with transaction.atomic():integration_attribute_formset.save()+ ZoneMinderManager().reload()+ zm_monitor = IntegrationFactory().get_integration_monitor(+ integration_id = ZmMetaData.integration_id,+ )+ if zm_monitor:+ zm_monitor.refresh()+context = {'integration_attribute_formset': integration_attribute_formset,}diff --git a/src/hi/integrations/zoneminder/zm_manager.py b/src/hi/integrations/zoneminder/zm_manager.pyindex 672a54b9..5bb16338 100644--- a/src/hi/integrations/zoneminder/zm_manager.py+++ b/src/hi/integrations/zoneminder/zm_manager.py@@ -53,6 +53,7 @@ class ZoneMinderManager( Singleton ):return self._zm_clientdef reload( self ):+ """ Should be called when integration settings are changed. """if self._is_loading:logger.warning( 'ZoneMinder manager is already loading.' )return@@ -271,9 +272,13 @@ class ZoneMinderManager( Singleton ):def get_zm_tzname(self) -> str:try:zm_integration = Integration.objects.get( integration_id = ZmMetaData.integration_id )+ integration_key = IntegrationKey(+ integration_id = ZmMetaData.integration_id,+ integration_name = str(ZmAttributeType.TIMEZONE),+ )integration_attribute = IntegrationAttribute.objects.filter(integration = zm_integration,- name = ZmAttributeType.TIMEZONE.label,+ integration_key_str = str(integration_key),).first()if integration_attribute:return integration_attribute.valuediff --git a/src/hi/integrations/zoneminder/zm_monitor.py b/src/hi/integrations/zoneminder/zm_monitor.pyindex 99922bfa..0f96617f 100644--- a/src/hi/integrations/zoneminder/zm_monitor.py+++ b/src/hi/integrations/zoneminder/zm_monitor.py@@ -23,7 +23,7 @@ class ZoneMinderMonitor( PeriodicMonitor ):# TODO: Move this into the integrations attributes for users to setZONEMINDER_SERVER_TIMEZONE = 'America/Chicago'- MONITOR_RFEFRESH_INTERVAL_SECS = 600+ MONITOR_REFRESH_INTERVAL_SECS = 600def __init__( self ):super().__init__(@@ -46,13 +46,24 @@ class ZoneMinderMonitor( PeriodicMonitor ):# important that we have this in the same TZ as the ZoneMinder# server and also pass in the TZ when filtering events.#- # TODO: If the user changes the ZM timezone attribute value, this- # process needs to be restarted.- #self._zm_tzname = self._zm_manager.get_zm_tzname()self._poll_from_datetime = datetimeproxy.now( self._zm_tzname )return+ def refresh( self ):+ """ Should be called when integration settings are changed. """+ new_tzname = self._zm_manager.get_zm_tzname()+ if new_tzname == self._zm_tzname:+ return++ logger.info( f'Refreshing ZoneMinder monitor timezone: {self._zm_tzname} -> {new_tzname}' )+ self._poll_from_datetime = datetimeproxy.change_timezone(+ original_datetime = self._poll_from_datetime,+ new_tzname = new_tzname,+ )+ self._zm_tzname = new_tzname+ return+async def do_work(self):current_poll_datetime = datetimeproxy.min()@@ -162,7 +173,7 @@ class ZoneMinderMonitor( PeriodicMonitor ):def _get_zm_monitors(self) -> List[ ZmMonitor ]:monitor_list_age = datetimeproxy.now() - self._zm_monitor_timestamp- if monitor_list_age.seconds > self.MONITOR_RFEFRESH_INTERVAL_SECS:+ if monitor_list_age.seconds > self.MONITOR_REFRESH_INTERVAL_SECS:self._zm_monitor_list = self._zm_manager.zm_client.monitors().list()self._zm_monitor_timestamp = datetimeproxy.now()return self._zm_monitor_list