Home Information - Your home, your data, organized.
Python
Moved ZoneMinder timezone to editable integration attributes.
Anthony Cassandrab3e1cf82292e
diff --git a/src/hi/apps/attribute/enums.py b/src/hi/apps/attribute/enums.pyindex 4c589dc0..70d625e5 100644--- a/src/hi/apps/attribute/enums.py+++ b/src/hi/apps/attribute/enums.py@@ -13,9 +13,10 @@ class AttributeType(LabeledEnum):class AttributeValueType(LabeledEnum):- TEXT = ('Text', '' )- FILE = ('File', '' ) # relative filename of MEDIA_ROOT- SECRET = ('Secret', '' )+ TEXT = ('Text' , '' )+ FILE = ('File' , '' ) # relative filename of MEDIA_ROOT+ SECRET = ('Secret' , '' )+ TIMEZONE = ('Timezone' , '' , 'America/Chicago' )@classmethoddef default(cls):@@ -32,4 +33,15 @@ class AttributeValueType(LabeledEnum):@propertydef is_secret(self):return bool( self == AttributeValueType.SECRET )++ @property+ def is_timezone(self):+ return bool( self == AttributeValueType.TIMEZONE )+ def __init__( self,+ label : str,+ description : str,+ initial_value : str = '' ):+ super().__init__( label, description )+ self.initial_value = initial_value+ returndiff --git a/src/hi/integrations/core/helpers.py b/src/hi/integrations/core/helpers.pyindex 947a7ac7..1c7ce662 100644--- a/src/hi/integrations/core/helpers.py+++ b/src/hi/integrations/core/helpers.py@@ -2,6 +2,7 @@ from django.db import transactionfrom hi.apps.attribute.enums import AttributeType+from hi.integrations.core.enums import IntegrationAttributeTypefrom hi.integrations.core.integration_key import IntegrationKeyfrom hi.integrations.core.models import Integration, IntegrationAttributefrom hi.integrations.core.transient_models import IntegrationMetaData@@ -11,9 +12,14 @@ class IntegrationHelperMixin:def get_or_create_integration( self, integration_metadata : IntegrationMetaData ):try:- return Integration.objects.get(+ integration = Integration.objects.get(integration_id = integration_metadata.integration_id,)+ self._ensure_all_attributes_exist(+ integration_metadata = integration_metadata,+ integration = integration,+ )+ return integrationexcept Integration.DoesNotExist:pass@@ -22,22 +28,59 @@ class IntegrationHelperMixin:integration_id = integration_metadata.integration_id,is_enabled = False,)- IntegrationAttributeType = integration_metadata.attribute_type- for attribute_type in IntegrationAttributeType:- integration_key = IntegrationKey(- integration_id = integration.integration_id,- integration_name = str(attribute_type),- )- IntegrationAttribute.objects.create(+ AttributeType = integration_metadata.attribute_type+ for attribute_type in AttributeType:+ self._create_integration_attribute(integration = integration,- name = attribute_type.label,- value = '',- value_type_str = str(attribute_type.value_type),- integration_key = integration_key,- attribute_type_str = AttributeType.PREDEFINED,- is_editable = attribute_type.is_editable,- is_required = attribute_type.is_required,+ attribute_type = attribute_type,)continuereturn integration+ def _ensure_all_attributes_exist( self,+ integration_metadata : IntegrationMetaData,+ integration : Integration ):+ """+ After an integration is created, we need to be able to detect if any+ new attributes might have been defined. This allows new code+ features to be added for existing installations.+ """++ new_attribute_types = set()+ existing_attribute_names = set([ x.name for x in integration.attributes.all() ])++ AttributeType = integration_metadata.attribute_type+ for attribute_type in AttributeType:+ if attribute_type.label not in existing_attribute_names:+ new_attribute_types.add( attribute_type )+ continue++ if new_attribute_types:+ with transaction.atomic():+ for attribute_type in new_attribute_types:+ self._create_integration_attribute(+ integration = integration,+ attribute_type = attribute_type,+ )+ continue+ return++ def _create_integration_attribute( self,+ integration : Integration,+ attribute_type : IntegrationAttributeType ):+ integration_key = IntegrationKey(+ integration_id = integration.integration_id,+ integration_name = str(attribute_type),+ )+ IntegrationAttribute.objects.create(+ integration = integration,+ name = attribute_type.label,+ value = attribute_type.value_type.initial_value,+ value_type_str = str(attribute_type.value_type),+ integration_key = integration_key,+ attribute_type_str = AttributeType.PREDEFINED,+ is_editable = attribute_type.is_editable,+ is_required = attribute_type.is_required,+ )+ return+diff --git a/src/hi/integrations/zoneminder/enums.py b/src/hi/integrations/zoneminder/enums.pyindex 12880ce7..4852a251 100644--- a/src/hi/integrations/zoneminder/enums.py+++ b/src/hi/integrations/zoneminder/enums.py@@ -34,6 +34,13 @@ class ZmAttributeType( IntegrationAttributeType ):True,True,)+ TIMEZONE = (+ 'Timezone',+ '',+ AttributeValueType.TIMEZONE,+ True,+ True,+ )class ZmMonitorState(LabeledEnum):diff --git a/src/hi/integrations/zoneminder/zm_manager.py b/src/hi/integrations/zoneminder/zm_manager.pyindex 891536b3..51b95042 100644--- a/src/hi/integrations/zoneminder/zm_manager.py+++ b/src/hi/integrations/zoneminder/zm_manager.py@@ -16,7 +16,7 @@ from hi.apps.model_helper import HiModelHelperfrom hi.integrations.core.exceptions import IntegrationAttributeErrorfrom hi.integrations.core.integration_key import IntegrationKey-from hi.integrations.core.models import Integration+from hi.integrations.core.models import Integration, IntegrationAttributefrom .enums import (ZmAttributeType,@@ -87,7 +87,7 @@ class ZoneMinderManager( Singleton ):}attribute_dict = zm_integration.attributes_by_integration_key- for zm_attr_type in ZmAttributeType:+ for zm_attr_type in attr_to_api_option_key.keys():integration_key = IntegrationKey(integration_id = zm_integration.integration_id,integration_name = str(zm_attr_type),@@ -268,3 +268,17 @@ class ZoneMinderManager( Singleton ):integration_name = f'{sensor_prefix}.{zm_monitor_id}',)+ def get_zm_tzname(self) -> str:+ try:+ zm_integration = Integration.objects.get( integration_id = ZmMetaData.integration_id )+ integration_attribute = IntegrationAttribute.objects.get(+ integration = zm_integration,+ name = ZmAttributeType.TIMEZONE.label,+ )+ return integration_attribute.value+ except Integration.DoesNotExist:+ logger.error( 'ZoneMinder integration is not implemented.' )+ except IntegrationAttribute.DoesNotExist:+ logger.error( 'ZoneMinder timezone not found.' )++ return 'UTC'diff --git a/src/hi/integrations/zoneminder/zm_monitor.py b/src/hi/integrations/zoneminder/zm_monitor.pyindex d4cd770d..d5697d87 100644--- a/src/hi/integrations/zoneminder/zm_monitor.py+++ b/src/hi/integrations/zoneminder/zm_monitor.py@@ -46,13 +46,19 @@ 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.#- self._poll_from_datetime = datetimeproxy.now( self.ZONEMINDER_SERVER_TIMEZONE )+ # 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 )returnasync def do_work(self):+ current_poll_datetime = datetimeproxy.min()+options = {'from': self._poll_from_datetime.isoformat(), # This "from" only looks at event start time- 'tz': self.ZONEMINDER_SERVER_TIMEZONE,+ 'tz': self._zm_tzname,}response = self._zm_manager.zm_client.events( options )events = response.list()@@ -81,7 +87,7 @@ class ZoneMinderMonitor( PeriodicMonitor ):if self.TRACE:logger.debug( f'ZM Api Event: {zm_api_event.get()}' )zm_event = ZmEvent( zm_api_event = zm_api_event,- zm_tzname = self.ZONEMINDER_SERVER_TIMEZONE )+ zm_tzname = self._zm_tzname )if zm_event.event_id in self._fully_processed_event_ids:continue@@ -112,14 +118,14 @@ class ZoneMinderMonitor( PeriodicMonitor ):self._fully_processed_event_ids[zm_event.event_id] = Truecontinue- # If there are no events for a monitor, we want to emit the sensor- # response of it being idle.+ # If there are no events for a monitor, we still want to emit the+ # sensor response of it being idle.#for zm_monitor in self._get_zm_monitors():if zm_monitor.id() not in zm_monitor_ids_seen:idle_sensor_response = self._create_idle_sensor_response(zm_monitor = zm_monitor,- timestamp = self._poll_from_datetime,+ timestamp = current_poll_datetime,)sensor_response_map[idle_sensor_response.integration_key] = idle_sensor_responsecontinue