Skip to content

Home Information - Your home, your data, organized.

Python

Added ability to override latest sensor values. Controllers need it.

Anthony Cassandra599fa6e6ee0a
diff --git a/src/hi/apps/api/views.py b/src/hi/apps/api/views.py
index d3c50ebc..b8ac1431 100644
--- a/src/hi/apps/api/views.py
+++ b/src/hi/apps/api/views.py
@@ -4,7 +4,7 @@ import logging
from django.http import HttpResponse
from django.views.generic import View
-from hi.apps.monitor.status_data_helpers import StatusDataHelper
+from hi.apps.monitor.status_display_manager import StatusDisplayManager
from hi.apps.monitor.status_display_data import StatusDisplayData
@@ -17,7 +17,7 @@ class StatusView( View ):
def get( self, *args, **kwargs ):
- entity_state_status_data_list = StatusDataHelper().get_all_entity_state_status_data_list()
+ entity_state_status_data_list = StatusDisplayManager().get_all_entity_state_status_data_list()
status_display_data_list = [ StatusDisplayData(x) for x in entity_state_status_data_list ]
css_class_update_map = dict()
diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.py
index 4d033787..16df64a6 100644
--- a/src/hi/apps/collection/collection_manager.py
+++ b/src/hi/apps/collection/collection_manager.py
@@ -9,7 +9,7 @@ from hi.apps.common.singleton import Singleton
from hi.apps.entity.models import Entity
from hi.apps.location.models import Location, LocationView
from hi.apps.location.svg_item_factory import SvgItemFactory
-from hi.apps.monitor.status_data_helpers import StatusDataHelper
+from hi.apps.monitor.status_display_manager import StatusDisplayManager
from .models import (
Collection,
@@ -64,7 +64,7 @@ class CollectionManager(Singleton):
collection_entity_list = list( collection.entities.all().order_by('order_id') )
entity_list = [ x.entity for x in collection_entity_list ]
- entity_status_data_list = StatusDataHelper().get_entity_status_data_list(
+ entity_status_data_list = StatusDisplayManager().get_entity_status_data_list(
entities = entity_list,
)
return CollectionData(
diff --git a/src/hi/apps/common/jwt_token_manager.py b/src/hi/apps/common/jwt_token_manager.py
index 1a148eb5..6a66a7a3 100644
--- a/src/hi/apps/common/jwt_token_manager.py
+++ b/src/hi/apps/common/jwt_token_manager.py
@@ -42,8 +42,7 @@ class JwtTokenManager( Singleton ):
TOKEN_CACHE_EXPIRY_SECS = 15 * 60 # Max is 20 min for Apple Connect Store API
def __init_singleton__( self ):
- self._token_cache = TTLCache( maxsize=4,
- ttl = self.TOKEN_CACHE_EXPIRY_SECS )
+ self._token_cache = TTLCache( maxsize = 4, ttl = self.TOKEN_CACHE_EXPIRY_SECS )
return
def get_token( self,
diff --git a/src/hi/apps/control/controller_manager.py b/src/hi/apps/control/controller_manager.py
index f8aa39ae..b69d514a 100644
--- a/src/hi/apps/control/controller_manager.py
+++ b/src/hi/apps/control/controller_manager.py
@@ -2,7 +2,7 @@ import logging
from typing import List
from hi.apps.common.singleton import Singleton
-from hi.apps.monitor.status_data_helpers import StatusDataHelper
+from hi.apps.monitor.status_display_manager import StatusDisplayManager
from .models import Controller
from .transient_models import ControllerData
@@ -17,7 +17,7 @@ class ControllerManager( Singleton ):
def get_controller_data( self, controller : Controller, error_list : List[ str ] = None ):
- latest_sensor_response = StatusDataHelper().get_latest_sensor_response(
+ latest_sensor_response = StatusDisplayManager().get_latest_sensor_response(
entity_state = controller.entity_state,
)
return ControllerData(
diff --git a/src/hi/apps/control/views.py b/src/hi/apps/control/views.py
index c4c259eb..bdaeaf88 100644
--- a/src/hi/apps/control/views.py
+++ b/src/hi/apps/control/views.py
@@ -2,6 +2,8 @@ import logging
from django.views.generic import View
+from hi.apps.monitor.status_display_manager import StatusDisplayManager
+
from hi.integrations.core.integration_factory import IntegrationFactory
from .view_mixin import ControlViewMixin
@@ -26,10 +28,30 @@ class ControllerView( View, ControlViewMixin ):
integration_key = controller.integration_key,
control_value = control_value,
)
+
+ # Because we use polling to fetch state/sensor values, when using a
+ # controller to change the value, the value can immediately differ
+ # from what we saw in the last polling interval. This is
+ # exacerbated because the server polls the sources for the value
+ # and the UI/client polls the server. These two polling intervals
+ # are not coordinated. To solve for this we do two things.
+ #
+ # 1) We immediately render to updated value to the UI/client.
+ #
+ # 2) We temporarily override the value in the
+ # StatusDisplayManager. This is to guard against the UI/client
+ # polling happening beforee the server has been able to update
+ # itrs values. This override is temporary and expires in a
+ # time just longer than the polling intervals' maximum gaps.
+
if control_result.has_errors:
override_sensor_value = None
else:
override_sensor_value = control_value
+ StatusDisplayManager().add_entity_state_value_override(
+ entity_state = controller.entity_state,
+ override_value = control_value,
+ )
return self.controller_data_response(
request = request,
diff --git a/src/hi/apps/entity/views.py b/src/hi/apps/entity/views.py
index c70cfa6f..11477c00 100644
--- a/src/hi/apps/entity/views.py
+++ b/src/hi/apps/entity/views.py
@@ -4,7 +4,7 @@ from django.db import transaction
from django.views.generic import View
from hi.apps.location.location_manager import LocationManager
-from hi.apps.monitor.status_data_helpers import StatusDataHelper
+from hi.apps.monitor.status_display_manager import StatusDisplayManager
from hi.apps.sense.sensor_history_manager import SensorHistoryManager
from hi.hi_async_view import HiModalView, HiSideView
@@ -101,7 +101,7 @@ class EntityStatusView( HiModalView, EntityViewMixin ):
def get( self, request, *args, **kwargs ):
entity = self.get_entity( request, *args, **kwargs )
- entity_status_data = StatusDataHelper().get_entity_status_data( entity = entity )
+ entity_status_data = StatusDisplayManager().get_entity_status_data( entity = entity )
if not entity_status_data.entity_state_status_data_list:
return EntityEditView().get( request, *args, **kwargs )
diff --git a/src/hi/apps/location/location_manager.py b/src/hi/apps/location/location_manager.py
index bed2f16c..84dd7f9d 100644
--- a/src/hi/apps/location/location_manager.py
+++ b/src/hi/apps/location/location_manager.py
@@ -8,7 +8,7 @@ from django.http import HttpRequest
from hi.apps.common.singleton import Singleton
from hi.apps.common.svg_models import SvgViewBox
-from hi.apps.monitor.status_data_helpers import StatusDataHelper
+from hi.apps.monitor.status_display_manager import StatusDisplayManager
from .enums import LocationViewType
from .location_view_data import LocationViewData
@@ -208,8 +208,8 @@ class LocationManager(Singleton):
unpositioned_collections.sort( key = lambda item : item.order_id )
if include_status_display_data:
- helper = StatusDataHelper()
- entity_to_entity_state_status_data_list = helper.get_entity_to_entity_state_status_data_list(
+ manager = StatusDisplayManager()
+ entity_to_entity_state_status_data_list = manager.get_entity_to_entity_state_status_data_list(
location_view = location_view,
entities = displayed_entities,
)
diff --git a/src/hi/apps/monitor/status_data_helpers.py b/src/hi/apps/monitor/status_display_manager.py
similarity index 86%
rename from src/hi/apps/monitor/status_data_helpers.py
rename to src/hi/apps/monitor/status_display_manager.py
index ccd34255..6cf8f04a 100644
--- a/src/hi/apps/monitor/status_data_helpers.py
+++ b/src/hi/apps/monitor/status_display_manager.py
@@ -1,18 +1,30 @@
+from cachetools import TTLCache
from typing import Dict, List, Set, Sequence
from hi.apps.control.transient_models import ControllerData
+from hi.apps.common.singleton import Singleton
from hi.apps.entity.enums import EntityStateType
from hi.apps.entity.models import Entity, EntityState
from hi.apps.location.enums import LocationViewType
from hi.apps.location.models import LocationView
+from hi.apps.sense.models import Sensor
from hi.apps.sense.sensor_response_manager import SensorResponseManager
from hi.apps.sense.transient_models import SensorResponse
from .transient_models import EntityStatusData, EntityStateStatusData
-class StatusDataHelper:
+class StatusDisplayManager( Singleton ):
+ STATUS_VALUE_OVERRIDES_SECS = 11
+
+ def __init_singleton__( self ):
+ self._status_value_overrides = TTLCache(
+ maxsize = 100,
+ ttl = self.STATUS_VALUE_OVERRIDES_SECS,
+ )
+ return
+
def get_all_entity_state_status_data_list( self ) -> List[ EntityStateStatusData ]:
"""
Gets the latest sensor responses for all EntityStates. Used by client
@@ -20,7 +32,7 @@ class StatusDataHelper:
state.
"""
- sensor_to_sensor_response_list = SensorResponseManager().get_all_latest_sensor_responses()
+ sensor_to_sensor_response_list = self._get_latest_sensor_responses_helper()
# Since a given EntityState can have zero or more sensors, for each
# EntityState, we need to collate all the sensor values to find the
@@ -169,7 +181,7 @@ class StatusDataHelper:
# Single fetch for getting all latest sensor data.
#
- sensor_response_list_map = SensorResponseManager().get_latest_sensor_responses(
+ sensor_to_sensor_response_list = self._get_latest_sensor_responses_helper(
sensor_list = all_sensor_list,
)
@@ -180,7 +192,7 @@ class StatusDataHelper:
for entity_state in entity_states:
entity_state_sensor_response_list = list()
for sensor in entity_state_to_sensor_list.get( entity_state ):
- sensor_response_list = sensor_response_list_map.get( sensor )
+ sensor_response_list = sensor_to_sensor_response_list.get( sensor )
if sensor_response_list:
entity_state_sensor_response_list.extend( sensor_response_list )
continue
@@ -341,12 +353,12 @@ class StatusDataHelper:
def get_latest_sensor_response( self, entity_state : EntityState ) -> SensorResponse:
sensor_list = list( entity_state.sensors.all() )
- sensor_response_list_map = SensorResponseManager().get_latest_sensor_responses(
+ sensor_to_sensor_response_list = self._get_latest_sensor_responses_helper(
sensor_list = sensor_list,
)
entity_state_sensor_response_list = list()
for sensor in sensor_list:
- sensor_response_list = sensor_response_list_map.get( sensor )
+ sensor_response_list = sensor_to_sensor_response_list.get( sensor )
if sensor_response_list:
entity_state_sensor_response_list.extend( sensor_response_list )
continue
@@ -356,3 +368,39 @@ class StatusDataHelper:
return entity_state_sensor_response_list[0]
return None
+
+ def _get_latest_sensor_responses_helper(
+ self,
+ sensor_list : List[ Sensor ] = None ) -> Dict[ Sensor, List[ SensorResponse ] ] :
+
+ if sensor_list is None:
+ sensor_to_sensor_response_list = SensorResponseManager().get_all_latest_sensor_responses()
+ else:
+ sensor_to_sensor_response_list = SensorResponseManager().get_latest_sensor_responses(
+ sensor_list = sensor_list,
+ )
+
+ for sensor, sensor_response_list in sensor_to_sensor_response_list.items():
+ if ( not sensor_response_list
+ or ( sensor.entity_state.id not in self._status_value_overrides )):
+ continue
+
+
+
+ print( f'\n\nAPPLYING OVERRIDE {sensor.entity_state} = {self._status_value_overrides[sensor.entity_state.id]}\n' )
+
+
+ sensor_response_list[0].value = self._status_value_overrides[sensor.entity_state.id]
+ continue
+
+ return sensor_to_sensor_response_list
+
+ def add_entity_state_value_override( self,
+ entity_state : EntityState,
+ override_value : str ):
+ """
+ Add a temporary override when values is explicitly chnaged by a controller to
+ compensate for the delays in value updates from the polling intervals.
+ """
+ self._status_value_overrides[entity_state.id] = override_value
+ return
diff --git a/src/hi/integrations/hass/hass_monitor.py b/src/hi/integrations/hass/hass_monitor.py
index f9adf2ad..7ee6a431 100644
--- a/src/hi/integrations/hass/hass_monitor.py
+++ b/src/hi/integrations/hass/hass_monitor.py
@@ -13,10 +13,12 @@ logger = logging.getLogger(__name__)
class HassMonitor( PeriodicMonitor ):
+ HASS_POLLING_INTERVAL_SECS = 10
+
def __init__( self ):
super().__init__(
id = 'hass-monitor',
- interval_secs = 10,
+ interval_secs = self.HASS_POLLING_INTERVAL_SECS,
)
self._hass_manager = HassManager()
self._sensor_response_manager = SensorResponseManager()
diff --git a/src/hi/integrations/zoneminder/zm_monitor.py b/src/hi/integrations/zoneminder/zm_monitor.py
index 441ad0c3..58e2c6aa 100644
--- a/src/hi/integrations/zoneminder/zm_monitor.py
+++ b/src/hi/integrations/zoneminder/zm_monitor.py
@@ -21,11 +21,12 @@ class ZoneMinderMonitor( PeriodicMonitor ):
# TODO: Move this into the integrations attributes for users to set
ZONEMINDER_SERVER_TIMEZONE = 'America/Chicago'
-
+ ZONEMINDER_POLLING_INTERVAL_SECS = 10
+
def __init__( self ):
super().__init__(
id = 'zm-monitor',
- interval_secs = 10,
+ interval_secs = self.ZONEMINDER_POLLING_INTERVAL_SECS,
)
self._zm_manager = ZoneMinderManager()
self._sensor_history_manager = SensorHistoryManager()