Skip to content

Home Information - Your home, your data, organized.

Python

Added config app and integration controls. WIP.

Anthony Cassandra64e322ef52b7
diff --git a/src/hi/apps/config/__init__.py b/src/hi/apps/config/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/config/admin.py b/src/hi/apps/config/admin.py
new file mode 100644
index 00000000..8c38f3f3
--- /dev/null
+++ b/src/hi/apps/config/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/src/hi/apps/config/apps.py b/src/hi/apps/config/apps.py
new file mode 100644
index 00000000..93ec546a
--- /dev/null
+++ b/src/hi/apps/config/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class ConfigConfig(AppConfig):
+ default_auto_field = "django.db.models.BigAutoField"
+ name = "hi.apps.config"
diff --git a/src/hi/apps/config/migrations/__init__.py b/src/hi/apps/config/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/hi/apps/config/models.py b/src/hi/apps/config/models.py
new file mode 100644
index 00000000..71a83623
--- /dev/null
+++ b/src/hi/apps/config/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/src/hi/apps/config/templates/config/panes/home.html b/src/hi/apps/config/templates/config/panes/home.html
new file mode 100644
index 00000000..f2a68e69
--- /dev/null
+++ b/src/hi/apps/config/templates/config/panes/home.html
@@ -0,0 +1,49 @@
+<div class="">
+ <h1>Settings</h1>
+
+ <div><em>TBD </em></div>
+
+ <h2>Integrations</h2>
+ {% for integration in integration_list %}
+ <div class="row hi-config-integration">
+ <div class="col-2">
+ {{ integration.integration_type.label }}
+ </div>
+ <div class="col-2">
+ {% if integration.is_enabled %}
+ <span class="text-success">ENABLED</span>
+ {% else %}
+ <span class="text-danger">DISABLED</span>
+ {% endif %}
+ </div>
+ <div class="col-2">
+ {% if integration.is_enabled %}
+ <form action="{% url 'config_integration' action='disable' name=integration.integration_type.name %}"
+ method="POST" role="form" data-async="#hi-main-content">
+ {% csrf_token %}
+ <button type="submit" class="btn btn-danger btn-control" role="button"">DISABLE</button>
+ </form>
+
+ {% else %}
+ <form action="{% url 'config_integration' action='enable' name=integration.integration_type.name %}"
+ method="POST" role="form" data-async="#hi-main-content">
+ {% csrf_token %}
+ <button type="submit" class="btn btn-success btn-control" role="button" >ENABLE</button>
+ </form>
+ {% endif %}
+ </div>
+ <div class="col-2">
+ {% if integration.is_enabled %}
+ <form action="{% url 'config_integration' action='sync' name=integration.integration_type.name %}"
+ method="POST" role="form" data-async="#hi-main-content">
+ {% csrf_token %}
+ <button type="submit" class="btn btn-info btn-control" role="button" >SYNC</button>
+ </form>
+ {% else %}
+ ---
+ {% endif %}
+ </div>
+ </div>
+ {% endfor %}
+
+</div>
diff --git a/src/hi/apps/config/urls.py b/src/hi/apps/config/urls.py
new file mode 100644
index 00000000..acbda865
--- /dev/null
+++ b/src/hi/apps/config/urls.py
@@ -0,0 +1,16 @@
+from django.urls import re_path
+
+from . import views
+
+
+urlpatterns = [
+
+ re_path( r'^$',
+ views.ConfigHomePaneView.as_view(),
+ name='config_home_pane' ),
+
+ re_path( r'^integration/(?P<action>\w+)/(?P<name>\w+)$',
+ views.ConfigIntegrationView.as_view(),
+ name='config_integration' ),
+
+]
diff --git a/src/hi/apps/config/views.py b/src/hi/apps/config/views.py
new file mode 100644
index 00000000..ef2790cf
--- /dev/null
+++ b/src/hi/apps/config/views.py
@@ -0,0 +1,41 @@
+from django.shortcuts import render
+from django.views.generic import View
+
+from hi.integrations.core.enums import IntegrationType
+from hi.integrations.core.integration_manager import IntegrationManager
+
+
+class ConfigHomePaneView( View ):
+
+ def get(self, request, *args, **kwargs):
+
+ context = {
+ 'integration_list': IntegrationManager().get_all_integrations(),
+ }
+ return render( request, 'config/panes/home.html', context )
+
+
+class ConfigIntegrationView( View ):
+
+ def post(self, request, *args, **kwargs):
+
+ action = kwargs.get('action')
+ integration_type = IntegrationType.from_name( kwargs.get('name') )
+
+ integration = IntegrationManager().get_integration( integration_type = integration_type )
+
+
+ if action == 'enable':
+ zzz
+ elif action == 'disable':
+ zzz
+ elif action == 'sync':
+ zzz
+ else:
+ raise ValueError( f'Unknown integration action "{action}". )
+
+
+ context = {
+ }
+ return render( request, 'config/panes/home.html', context )
+
diff --git a/src/hi/integrations/core/apps.py b/src/hi/integrations/core/apps.py
index 0e6f7038..a7e2a5d8 100644
--- a/src/hi/integrations/core/apps.py
+++ b/src/hi/integrations/core/apps.py
@@ -1,47 +1,7 @@
-import logging
-import asyncio
-import sys
-import threading
-
from django.apps import AppConfig
-logger = logging.getLogger(__name__)
-
class CoreConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "hi.integrations.core"
-
- INITIALIZATION_LOCK_NAME = 'integration_initialization'
- INITIALIZATION_LOCK_TIME_SECS = 10
-
- def ready(self):
- if not self.is_runserver_command():
- return
- threading.Thread( target = self.initialize ).start()
- return
-
- def initialize(self):
- from hi.apps.common.database_lock import InitializationLockContext
- from .initialize import initialize_integrations # Need to be inside this method
-
- try:
- # Ensure this only runs once on startup. The ready() method
- # could be called multiple times and there could be multiple
- # processes / threads.
- with InitializationLockContext( name = self.INITIALIZATION_LOCK_NAME,
- timeout_seconds = self.INITIALIZATION_LOCK_TIME_SECS ):
- logger.info( 'Initializing integrations ...' )
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- loop.run_until_complete( initialize_integrations() )
- loop.close()
-
- except RuntimeError:
- pass
-
- return
-
- def is_runserver_command(self):
- return bool( len(sys.argv) > 1 and sys.argv[1] == 'runserver' )
diff --git a/src/hi/integrations/core/enums.py b/src/hi/integrations/core/enums.py
index ccfb7717..43449bf1 100644
--- a/src/hi/integrations/core/enums.py
+++ b/src/hi/integrations/core/enums.py
@@ -11,6 +11,6 @@ class PropertyValueType(LabeledEnum):
class IntegrationType(LabeledEnum):
ZONEMINDER = ( 'ZoneMinder', 'ZoneMinder camera streaming and motion detection.' )
- INSTEON_HASS = ( 'InsteonHASS', 'Insteon devices via Home Assistant API and its integration' )
- INSTEON_DIRECT = ( 'InsteonDirect', 'Insteon devices via modem on serial port.' )
+ INSTEON_HASS = ( 'Insteon (hass)', 'Insteon devices via Home Assistant API and its integration' )
+ INSTEON_SERIAL = ( 'Insteon (serial)', 'Insteon devices via modem on serial port.' )
diff --git a/src/hi/integrations/core/initialize.py b/src/hi/integrations/core/initialize.py
deleted file mode 100644
index 45392996..00000000
--- a/src/hi/integrations/core/initialize.py
+++ /dev/null
@@ -1,25 +0,0 @@
-from asgiref.sync import sync_to_async
-import logging
-import traceback
-
-from hi.integrations.zoneminder.zm_integration import ZoneMinderIntegration
-
-logger = logging.getLogger(__name__)
-
-
-async def initialize_integrations():
- """
- Each integration should:
- - run asynchronously;
- - check if they are enabled or not; and
- - sanity check their configuration properties.
- """
- try:
- await sync_to_async( ZoneMinderIntegration().sync)() # Need wrapper for ORM operations
- except Exception as e:
- logger.error( 'ZM sync error: {}'.format(str(e)) )
- logger.info( traceback.format_exc() )
-
- # Add other integration initializations here.
-
- return
diff --git a/src/hi/integrations/core/integration_manager.py b/src/hi/integrations/core/integration_manager.py
index 2fe5032c..4fc663ca 100644
--- a/src/hi/integrations/core/integration_manager.py
+++ b/src/hi/integrations/core/integration_manager.py
@@ -1,4 +1,5 @@
import logging
+from typing import List
from hi.apps.common.singleton import Singleton
@@ -13,6 +14,14 @@ class IntegrationManager( Singleton ):
def __init_singleton__( self ):
return
+ def get_all_integrations( self ) -> List[ Integration ]:
+ integration_list = list()
+ for integration_type in IntegrationType:
+ integration = self.get_integration( integration_type = integration_type )
+ integration_list.append( integration )
+ continue
+ return integration_list
+
def get_integration( self, integration_type : IntegrationType ) -> Integration:
try:
return Integration.objects.get( integration_type_str = str(integration_type) )
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.py
index ddb28648..7f07c30a 100644
--- a/src/hi/settings/base.py
+++ b/src/hi/settings/base.py
@@ -136,6 +136,7 @@ INSTALLED_APPS = [
'django.contrib.humanize',
'constance',
'hi.apps.common',
+ 'hi.apps.config',
'hi.apps.location',
'hi.apps.entity',
'hi.apps.collection',
diff --git a/src/hi/templates/panes/side_panel.html b/src/hi/templates/panes/side_panel.html
index 2e25dc75..da310328 100644
--- a/src/hi/templates/panes/side_panel.html
+++ b/src/hi/templates/panes/side_panel.html
@@ -1,4 +1,11 @@
<div class="border-left" >
SIDE PANEL
+<div class="">
+ <a href="{% url 'config_home_pane' %}"
+ class="btn btn-tertiary btn-control" role="button"
+ data-async="#hi-main-content" >
+ SETTINGS
+ </a>
+</div>
+
</div>
-
diff --git a/src/hi/urls.py b/src/hi/urls.py
index ef913be6..7b518744 100644
--- a/src/hi/urls.py
+++ b/src/hi/urls.py
@@ -26,6 +26,7 @@ urlpatterns = [
re_path( r'^$', views.HomeView.as_view(), name='home' ),
re_path( r'^index.html$', views.HomeView.as_view(), name='home_index' ),
+ re_path( r'^config/', include('hi.apps.config.urls' )),
re_path( r'^location/', include('hi.apps.location.urls' )),
]