Home Information - Your home, your data, organized.
Python
More refactoring around Entity Status displays in various contexts.
Anthony Cassandrafd5cc7cf0f85
diff --git a/src/hi/apps/api/views.py b/src/hi/apps/api/views.pyindex 9fcc8a45..1ddf47a1 100644--- a/src/hi/apps/api/views.py+++ b/src/hi/apps/api/views.py@@ -4,7 +4,7 @@ import loggingfrom django.http import HttpResponsefrom django.views.generic import View-from hi.apps.monitor.status_display_helper import StatusDisplayStatusViewHelper+from hi.apps.monitor.status_display_helpers import StatusDisplayStatusViewHelperlogger = logging.getLogger(__name__)diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.pyindex c2f637b7..a90c01d0 100644--- a/src/hi/apps/collection/collection_manager.py+++ b/src/hi/apps/collection/collection_manager.py@@ -6,10 +6,10 @@ from django.http import HttpRequestfrom hi.apps.collection.edit.forms import CollectionPositionFormfrom hi.apps.common.singleton import Singleton-from hi.apps.entity.entity_manager import EntityManagerfrom hi.apps.entity.models import Entityfrom hi.apps.location.models import Location, LocationViewfrom hi.apps.location.svg_item_factory import SvgItemFactory+from hi.apps.monitor.status_display_helpers import StatusDisplayEntityHelperfrom .models import (Collection,@@ -62,11 +62,9 @@ class CollectionManager(Singleton):collection : Collection,is_editing : bool ):- entity_manager = EntityManager()-entity_status_data_list = list()for collection_entity in collection.entities.all().order_by('order_id'):- entity_status_data = entity_manager.get_entity_status_data(+ entity_status_data = StatusDisplayEntityHelper().get_entity_status_data(entity = collection_entity.entity,is_editing = is_editing,)diff --git a/src/hi/apps/collection/transient_models.py b/src/hi/apps/collection/transient_models.pyindex 924e690c..ff94820f 100644--- a/src/hi/apps/collection/transient_models.py+++ b/src/hi/apps/collection/transient_models.py@@ -4,7 +4,7 @@ from typing import Listfrom hi.apps.collection.edit.forms import CollectionForm, CollectionPositionFormfrom hi.apps.entity.enums import EntityTypefrom hi.apps.entity.models import Entity-from hi.apps.entity.transient_models import EntityStatusData+from hi.apps.monitor.transient_models import EntityStatusDatafrom .models import Collectiondiff --git a/src/hi/apps/control/templates/control/panes/controller_default.html b/src/hi/apps/control/templates/control/panes/controller_default.htmlnew file mode 100644index 00000000..433cd1e5--- /dev/null+++ b/src/hi/apps/control/templates/control/panes/controller_default.html@@ -0,0 +1,5 @@+<div class="h6" >{{ controller.name }}</div>++<div>+TBD+</div>diff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.pyindex 331bf572..b7963ef8 100644--- a/src/hi/apps/entity/entity_manager.py+++ b/src/hi/apps/entity/entity_manager.py@@ -8,7 +8,7 @@ from hi.apps.entity.edit.forms import EntityPositionFormfrom hi.apps.location.models import Location, LocationViewfrom hi.apps.location.svg_item_factory import SvgItemFactoryfrom hi.apps.sense.sensor_history_manager import SensorHistoryManager-from hi.apps.sense.transient_models import SensorResponse+from hi.apps.sense.transient_models import EntityStateHistoryDatafrom .delegation_manager import DelegationManagerfrom .enums import (@@ -24,8 +24,6 @@ from .models import (from .transient_models import (EntityDetailsData,EntityEditData,- EntityStateHistoryData,- EntityStatusData,EntityViewGroup,EntityViewItem,)@@ -33,53 +31,9 @@ from .transient_models import (class EntityManager(Singleton):- ENTITY_STATE_HISTORY_ITEM_MAX = 5-def __init_singleton__(self):return- def get_entity_status_data( self,- entity : Entity,- is_editing : bool ) -> EntityStatusData:- sensor_history_list_map = SensorHistoryManager().get_latest_entity_sensor_history(- entity = entity,- max_items = self.ENTITY_STATE_HISTORY_ITEM_MAX,- )- latest_sensor_response_map = dict()- for sensor, sensor_history_list in sensor_history_list_map.items():- if sensor_history_list:- sensor_response = SensorResponse.from_sensor_history( sensor_history = sensor_history_list[0] )- else:- sensor_response = None- latest_sensor_response_map[sensor] = sensor_response- continue-- return EntityStatusData(- entity = entity,- latest_sensor_response_map = latest_sensor_response_map,- )-- def get_entity_state_history_data( self,- entity : Entity,- is_editing : bool ) -> EntityStateHistoryData:- sensor_history_list_map = SensorHistoryManager().get_latest_entity_sensor_history(- entity = entity,- max_items = self.ENTITY_STATE_HISTORY_ITEM_MAX,- )- latest_sensor_response_map = dict()- for sensor, sensor_history_list in sensor_history_list_map.items():- if sensor_history_list:- sensor_response = SensorResponse.from_sensor_history( sensor_history = sensor_history_list[0] )- else:- sensor_response = None- latest_sensor_response_map[sensor] = sensor_response- continue-- return EntityStateHistoryData(- entity = entity,- sensor_history_list_map = sensor_history_list_map,- )-def get_entity_details_data( self,entity : Entity,location_view : LocationView,diff --git a/src/hi/apps/entity/enums.py b/src/hi/apps/entity/enums.pyindex de4e14a4..2824a970 100644--- a/src/hi/apps/entity/enums.py+++ b/src/hi/apps/entity/enums.py@@ -110,7 +110,16 @@ class EntityStateType(LabeledEnum):it will fallback to the default template of"entity/panes/sensor_value_default.html""""- return f'entity/panes/sensor_value_{self.name.lower()}.html'+ return f'sense/panes/sensor_value_{self.name.lower()}.html'++ def controller_template_name(self):+ """+ Template used to render a sensor's value for this state. Create the+ template at the given location to define a state-specific rendering, else+ it will fallback to the default template of+ "entity/panes/sensor_value_default.html"+ """+ return f'control/panes/controller_{self.name.lower()}.html'class TemperatureUnit(LabeledEnum):diff --git a/src/hi/apps/entity/templates/entity/modals/entity_status.html b/src/hi/apps/entity/templates/entity/modals/entity_status.htmlindex b22cc2fb..111b8a3d 100644--- a/src/hi/apps/entity/templates/entity/modals/entity_status.html+++ b/src/hi/apps/entity/templates/entity/modals/entity_status.html@@ -1,5 +1,4 @@{% extends "modals/action_done.html" %}-{% load common_template_helpers %}{% block title_text %}{{ entity.name }}@@ -7,15 +6,7 @@{% block body %}-<ul>- {% for sensor, sensor_response in latest_sensor_response_map.items %}- <li>Sensor: {{ sensor.name }}- {% include_with_fallback sensor_response.sensor.entity_state.entity_state_type.value_template_name "entity/panes/sensor_value_default.html" %}- </li>- {% empty %}- <div><em>No sensors</em></div>- {% endfor %}-</ul>+{% include "entity/panes/entity_state_status_card.html" with entity_state_status_data_list=entity_state_status_data_list %}<div class="d-flex justify-content-between"><div>diff --git a/src/hi/apps/entity/templates/entity/panes/entity_state_status_card.html b/src/hi/apps/entity/templates/entity/panes/entity_state_status_card.htmlnew file mode 100644index 00000000..ea93c682--- /dev/null+++ b/src/hi/apps/entity/templates/entity/panes/entity_state_status_card.html@@ -0,0 +1,23 @@+{% load common_template_helpers %}+<ul>+ {% for entity_state_status_data in entity_state_status_data_list %}+ <li>State: {{ entity_state_status_data.entity_state.name }}++ {% with sensor_response=entity_state_status_data.latest_sensor_response %}+ {% if sensor_response %}+ {% include_with_fallback entity_state_status_data.entity_state.entity_state_type.value_template_name "sense/panes/sensor_value_default.html" %}+ {% else %}+ <div><em>No sensor responses.</em></div>+ {% endif %}+ {% endwith %}++ {% for controller in entity_state_status_data.controller_list %}+ {% include_with_fallback entity_state_status_data.entity_state.entity_state_type.controller_template_name "control/panes/controller_default.html" %}+ {% empty %}+ <div><em>No controllers.</em></div>+ {% endfor %}++ {% empty %}+ <div><em>No state info.</em></div>+ {% endfor %}+</ul>diff --git a/src/hi/apps/entity/templates/entity/panes/entity_state_status_row.html b/src/hi/apps/entity/templates/entity/panes/entity_state_status_row.htmlnew file mode 100644index 00000000..356106d9--- /dev/null+++ b/src/hi/apps/entity/templates/entity/panes/entity_state_status_row.html@@ -0,0 +1,23 @@+{% load common_template_helpers %}++{% for entity_state_status_data in entity_state_status_data_list %}+<div class="col-3">State: {{ entity_state_status_data.entity_state.name }}++ {% with sensor_response=entity_state_status_data.latest_sensor_response %}+ {% if sensor_response %}+ {% include_with_fallback entity_state_status_data.entity_state.entity_state_type.value_template_name "sense/panes/sensor_value_default.html" %}+ {% else %}+ <div><em>No sensor responses.</em></div>+ {% endif %}+ {% endwith %}++ {% for controller in entity_state_status_data.controller_list %}+ {% include_with_fallback entity_state_status_data.entity_state.entity_state_type.controller_template_name "control/panes/controller_default.html" %}+ {% empty %}+ <div><em>No controllers.</em></div>+ {% endfor %}+</div>++{% empty %}+<div class="col-4"><em>No state info.</em></div>+{% endfor %}diff --git a/src/hi/apps/entity/templates/entity/panes/entity_status_card.html b/src/hi/apps/entity/templates/entity/panes/entity_status_card.htmlindex d5310ebf..54124820 100644--- a/src/hi/apps/entity/templates/entity/panes/entity_status_card.html+++ b/src/hi/apps/entity/templates/entity/panes/entity_status_card.html@@ -1,4 +1,3 @@-{% load common_template_helpers %}<div>{% with entity=entity_status_data.entity %}<div>@@ -16,11 +15,6 @@</div>{% endwith %}- {% for sensor, sensor_response in entity_status_data.latest_sensor_response_map.items %}- <li>Sensor: {{ sensor.name }}- {% include_with_fallback sensor_response.sensor.entity_state.entity_state_type.value_template_name "entity/panes/sensor_value_default.html" %}- </li>- {% empty %}- <div><em>No sensors</em></div>- {% endfor %}+ {% include "entity/panes/entity_state_status_card.html" with entity_state_status_data_list=entity_status_data.entity_state_status_data_list %}+</div>diff --git a/src/hi/apps/entity/templates/entity/panes/entity_status_row.html b/src/hi/apps/entity/templates/entity/panes/entity_status_row.htmlindex d5310ebf..d1a65d73 100644--- a/src/hi/apps/entity/templates/entity/panes/entity_status_row.html+++ b/src/hi/apps/entity/templates/entity/panes/entity_status_row.html@@ -1,7 +1,6 @@-{% load common_template_helpers %}-<div>+<div class="row">{% with entity=entity_status_data.entity %}- <div>+ <div class="col-3">{% if request.is_editing %}<a href="{% url 'entity_details' entity_id=entity.id %}"data-async="true">@@ -16,11 +15,6 @@</div>{% endwith %}- {% for sensor, sensor_response in entity_status_data.latest_sensor_response_map.items %}- <li>Sensor: {{ sensor.name }}- {% include_with_fallback sensor_response.sensor.entity_state.entity_state_type.value_template_name "entity/panes/sensor_value_default.html" %}- </li>- {% empty %}- <div><em>No sensors</em></div>- {% endfor %}+ {% include "entity/panes/entity_state_status_row.html" with entity_state_status_data_list=entity_status_data.entity_state_status_data_list %}+</div>diff --git a/src/hi/apps/entity/transient_models.py b/src/hi/apps/entity/transient_models.pyindex 217fa0b1..239f5d6d 100644--- a/src/hi/apps/entity/transient_models.py+++ b/src/hi/apps/entity/transient_models.py@@ -3,7 +3,6 @@ from typing import Dict, Listfrom hi.apps.entity.edit.forms import EntityPositionFormfrom hi.apps.sense.models import Sensor, SensorHistory-from hi.apps.sense.transient_models import SensorResponsefrom .enums import EntityTypefrom .forms import (@@ -20,7 +19,7 @@ class EntityViewItem:entity : Entityexists_in_view : bool-+@dataclassclass EntityViewGroup:"""All entities of a given type and flagged as in the view or not."""@@ -66,32 +65,6 @@ class EntityEditData:}-@dataclass-class EntityStatusData:- entity : Entity- latest_sensor_response_map : Dict[ Sensor, SensorResponse ]-- def to_template_context(self):- context = {- 'entity': self.entity,- 'latest_sensor_response_map': self.latest_sensor_response_map,- }- return context---@dataclass-class EntityStateHistoryData:- entity : Entity- sensor_history_list_map : Dict[ Sensor, List[ SensorHistory ] ]-- def to_template_context(self):- context = {- 'entity': self.entity,- 'sensor_history_list_map': self.sensor_history_list_map,- }- return context--@dataclassclass EntityDetailsData:"""diff --git a/src/hi/apps/entity/views.py b/src/hi/apps/entity/views.pyindex c3225e79..59425f52 100644--- a/src/hi/apps/entity/views.py+++ b/src/hi/apps/entity/views.py@@ -4,6 +4,8 @@ from django.db import transactionfrom django.views.generic import Viewfrom hi.apps.location.location_manager import LocationManager+from hi.apps.monitor.status_display_helpers import StatusDisplayEntityHelper+from hi.apps.sense.sensor_history_manager import SensorHistoryManagerfrom hi.hi_async_view import HiModalView, HiSideView@@ -99,10 +101,13 @@ class EntityStatusView( HiModalView, EntityViewMixin ):def get( self, request, *args, **kwargs ):entity = self.get_entity( request, *args, **kwargs )- entity_status_data = EntityManager().get_entity_status_data(+ entity_status_data = StatusDisplayEntityHelper().get_entity_status_data(entity = entity,is_editing = request.is_editing,)+ if not entity_status_data.entity_state_status_data_list:+ return EntityEditView().get( request, *args, **kwargs )+context = entity_status_data.to_template_context()return self.modal_response( request, context )@@ -115,7 +120,7 @@ class EntityStateHistoryView( HiModalView, EntityViewMixin ):def get( self, request, *args, **kwargs ):entity = self.get_entity( request, *args, **kwargs )- entity_state_history_data = EntityManager().get_entity_state_history_data(+ entity_state_history_data = SensorHistoryManager().get_entity_state_history_data(entity = entity,is_editing = request.is_editing,)diff --git a/src/hi/apps/location/location_manager.py b/src/hi/apps/location/location_manager.pyindex 29c98e1e..f2e6ec59 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 HttpRequestfrom hi.apps.common.singleton import Singletonfrom hi.apps.common.svg_models import SvgViewBox-from hi.apps.monitor.status_display_helper import StatusDisplayLocationHelper+from hi.apps.monitor.status_display_helpers import StatusDisplayLocationHelperfrom .enums import LocationViewTypefrom .location_view_data import LocationViewDatadiff --git a/src/hi/apps/monitor/status_display_helper.py b/src/hi/apps/monitor/status_display_helpers.pysimilarity index 56%rename from src/hi/apps/monitor/status_display_helper.pyrename to src/hi/apps/monitor/status_display_helpers.pyindex 828e4702..84958002 100644--- a/src/hi/apps/monitor/status_display_helper.py+++ b/src/hi/apps/monitor/status_display_helpers.py@@ -4,9 +4,12 @@ from hi.apps.entity.enums import EntityStateTypefrom hi.apps.entity.models import Entity, EntityStatefrom hi.apps.location.enums import LocationViewTypefrom hi.apps.location.models import LocationView-from hi.apps.monitor.status_display_data import StatusDisplayData+from hi.apps.sense.models import Sensorfrom hi.apps.sense.sensor_response_manager import SensorResponseManager+from .status_display_data import StatusDisplayData+from .transient_models import EntityStatusData, EntityStateStatusData+class StatusDisplayLocationHelper:@@ -79,21 +82,21 @@ class StatusDisplayStatusViewHelper:def get_status_display_data( self ) -> List[ StatusDisplayData ]:- sensor_response_list_map = SensorResponseManager().get_all_latest_sensor_responses()-+ sensor_to_sensor_response_list = SensorResponseManager().get_all_latest_sensor_responses()+# Since a given EntityState can have zero or more sensors, for each# EntityState, we need to collate all the sensor values to find the# latest status.- entity_state_sensor_response_list_map = dict()- for sensor, sensor_response_list in sensor_response_list_map.items():- if sensor.entity_state not in entity_state_sensor_response_list_map:- entity_state_sensor_response_list_map[sensor.entity_state] = list()- entity_state_sensor_response_list_map[sensor.entity_state].extend( sensor_response_list )+ entity_state_to_sensor_response_list = dict()+ for sensor, sensor_response_list in sensor_to_sensor_response_list.items():+ if sensor.entity_state not in entity_state_to_sensor_response_list:+ entity_state_to_sensor_response_list[sensor.entity_state] = list()+ entity_state_to_sensor_response_list[sensor.entity_state].extend( sensor_response_list )continuestatus_display_data_list = list()- for entity_state, sensor_response_list in entity_state_sensor_response_list_map.items():+ for entity_state, sensor_response_list in entity_state_to_sensor_response_list.items():sensor_response_list.sort( key = lambda item: item.timestamp, reverse = True )status_display_data = StatusDisplayData(entity_state = entity_state,@@ -103,3 +106,58 @@ class StatusDisplayStatusViewHelper:continuereturn status_display_data_list+++class StatusDisplayEntityHelper:++ def get_entity_status_data( self,+ entity : Entity,+ is_editing : bool ) -> EntityStatusData:++ entity_state_set = set( entity.states.all() )++ for entity_state_delegation in entity.entity_state_delegations.all():+ entity_state_set.add( entity_state_delegation.entity_state )+ continue++ entity_state_to_sensor_list = dict()++ sensor_list = list()+ for entity_state in entity_state_set:+ entity_state_sensor_list = list( entity_state.sensors.all() )+ entity_state_to_sensor_list[entity_state] = entity_state_sensor_list+ sensor_list.extend( entity_state_sensor_list )+ continue++ if not is_editing:+ sensor_response_list_map = SensorResponseManager().get_latest_sensor_responses(+ sensor_list = sensor_list,+ )+ else:+ sensor_response_list_map = dict()++ entity_state_status_data_list = list()+ for entity_state in entity_state_set:+ 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 )+ if sensor_response_list:+ entity_state_sensor_response_list.extend( sensor_response_list )+ continue+ entity_state_sensor_response_list.sort( key = lambda item: item.timestamp, reverse = True )++ controller_list = list( entity_state.controllers.all() )++ entity_state_status_data = EntityStateStatusData(+ entity_state = entity_state,+ sensor_response_list = entity_state_sensor_response_list,+ controller_list = controller_list,+ )+ entity_state_status_data_list.append( entity_state_status_data )+ continue++ return EntityStatusData(+ entity = entity,+ entity_state_status_data_list = entity_state_status_data_list,+ )+diff --git a/src/hi/apps/monitor/transient_models.py b/src/hi/apps/monitor/transient_models.pynew file mode 100644index 00000000..043b3a3c--- /dev/null+++ b/src/hi/apps/monitor/transient_models.py@@ -0,0 +1,34 @@+from dataclasses import dataclass+from typing import List++from hi.apps.control.models import Controller+from hi.apps.entity.models import Entity, EntityState+from hi.apps.sense.transient_models import SensorResponse+++@dataclass+class EntityStateStatusData:+ entity_state : EntityState+ sensor_response_list : List[ SensorResponse ]+ controller_list : List[ Controller ]++ @property+ def latest_sensor_response(self):+ if self.sensor_response_list:+ return self.sensor_response_list[0]+ return None+++@dataclass+class EntityStatusData:+ entity : Entity+ entity_state_status_data_list : List[ EntityStateStatusData ]++ def to_template_context(self):+ context = {+ 'entity': self.entity,+ 'entity_state_status_data_list': self.entity_state_status_data_list,+ }+ return context++diff --git a/src/hi/apps/sense/sensor_history_manager.py b/src/hi/apps/sense/sensor_history_manager.pyindex ecc703be..dcda6bdd 100644--- a/src/hi/apps/sense/sensor_history_manager.py+++ b/src/hi/apps/sense/sensor_history_manager.py@@ -6,13 +6,15 @@ from hi.apps.common.singleton import Singletonfrom hi.apps.entity.models import Entityfrom .models import Sensor, SensorHistory-from .transient_models import SensorResponse+from .transient_models import SensorResponse, EntityStateHistoryDatalogger = logging.getLogger(__name__)class SensorHistoryManager( Singleton ):+ ENTITY_STATE_HISTORY_ITEM_MAX = 5+def __init_singleton__( self ):return@@ -53,3 +55,17 @@ class SensorHistoryManager( Singleton ):continuereturn sensor_history_list_map++ def get_entity_state_history_data( self,+ entity : Entity,+ is_editing : bool ) -> EntityStateHistoryData:+ sensor_history_list_map = self.get_latest_entity_sensor_history(+ entity = entity,+ max_items = self.ENTITY_STATE_HISTORY_ITEM_MAX,+ )++ return EntityStateHistoryData(+ entity = entity,+ sensor_history_list_map = sensor_history_list_map,+ )+diff --git a/src/hi/apps/sense/sensor_response_manager.py b/src/hi/apps/sense/sensor_response_manager.pyindex 220bdef3..835ed906 100644--- a/src/hi/apps/sense/sensor_response_manager.py+++ b/src/hi/apps/sense/sensor_response_manager.py@@ -15,7 +15,8 @@ logger = logging.getLogger(__name__)class SensorResponseManager( Singleton ):- """Integrations are responsible for monitoring sensor values and+ """+ Integrations are responsible for monitoring sensor values andnormalizing them into SensorResponse objects. This module take it fromthere to store these for tracking the latest state. i.e., Integrationsshould be using this module to submit sensor values changes.@@ -34,7 +35,6 @@ class SensorResponseManager( Singleton ):- We will keep all the list cache keys in a Redis set so that we caneasily fetch them all without needing to know all the integrationkeys (using the SADD and SMEMBERS Redis functions).-"""SENSOR_RESPONSE_LIST_SIZE = 5SENSOR_RESPONSE_LIST_SET_KEY = 'hi.sr.list.keys'@@ -81,7 +81,7 @@ class SensorResponseManager( Singleton ):logger.debug( f'Sensor changed: {len(changed_sensor_response_list)} of {len(sensor_response_map)}' )return- def get_all_latest_sensor_responses( self ) -> Dict[ IntegrationKey, List[ SensorResponse ] ]:+ def get_all_latest_sensor_responses( self ) -> Dict[ Sensor, List[ SensorResponse ] ]:list_cache_keys = self._redis_client.smembers( self.SENSOR_RESPONSE_LIST_SET_KEY )@@ -96,9 +96,31 @@ class SensorResponseManager( Singleton ):if not cached_list:continuesensor_response_list = [ SensorResponse.from_string( x ) for x in cached_list ]- sensor = self._get_sensor( integration_key = sensor_response_list[0].integration_key )- if sensor:- sensor_response_list_map[sensor] = sensor_response_list+ if sensor_response_list:+ sensor = self._get_sensor( integration_key = sensor_response_list[0].integration_key )+ if sensor:++ sensor_response_list_map[sensor] = sensor_response_list+ continue++ return sensor_response_list_map++ def get_latest_sensor_responses( self,+ sensor_list : List[ Sensor ] ) -> Dict[ Sensor, List[ SensorResponse ] ]:++ list_cache_keys = [ self.to_sensor_response_list_cache_key( x.integration_key )+ for x in sensor_list ]++ pipeline = self._redis_client.pipeline()+ for list_cache_key in list_cache_keys:+ pipeline.lrange( list_cache_key, 0, -1 )+ continue+ cached_list_list = pipeline.execute()++ sensor_response_list_map = dict()+ for sensor, cached_list in zip( sensor_list, cached_list_list ):+ sensor_response_list = [ SensorResponse.from_string( x ) for x in cached_list ]+ sensor_response_list_map[sensor] = sensor_response_listcontinuereturn sensor_response_list_map@@ -136,14 +158,12 @@ class SensorResponseManager( Singleton ):def _get_sensor( self, integration_key : IntegrationKey ):if integration_key not in self._sensor_cache:- try:- sensor = Sensor.objects.select_related('entity_state').get(- integration_id = integration_key.integration_id,- integration_name = integration_key.integration_name,- )- self._sensor_cache[integration_key] = sensor- except Sensor.DoesNotExist:+ sensor_queryset = Sensor.objects.filter_by_integration_key(+ integration_key = integration_key,+ ).select_related('entity_state')+ if not sensor_queryset.exists():return None+ self._sensor_cache[integration_key] = sensor_queryset[0]return self._sensor_cache[integration_key]diff --git a/src/hi/apps/entity/templates/entity/panes/sensor_value_default.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_default.htmlsimilarity index 100%rename from src/hi/apps/entity/templates/entity/panes/sensor_value_default.htmlrename to src/hi/apps/sense/templates/sense/panes/sensor_value_default.htmldiff --git a/src/hi/apps/entity/templates/entity/panes/sensor_value_video_stream.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_video_stream.htmlsimilarity index 100%rename from src/hi/apps/entity/templates/entity/panes/sensor_value_video_stream.htmlrename to src/hi/apps/sense/templates/sense/panes/sensor_value_video_stream.htmldiff --git a/src/hi/apps/sense/transient_models.py b/src/hi/apps/sense/transient_models.pyindex c292291b..1e0404ef 100644--- a/src/hi/apps/sense/transient_models.py+++ b/src/hi/apps/sense/transient_models.py@@ -1,6 +1,9 @@from dataclasses import dataclassfrom datetime import datetimeimport json+from typing import Dict, List++from hi.apps.entity.models import Entityfrom hi.integrations.core.integration_key import IntegrationKey@@ -56,3 +59,14 @@ class SensorResponse:)+@dataclass+class EntityStateHistoryData:+ entity : Entity+ sensor_history_list_map : Dict[ Sensor, List[ SensorHistory ] ]++ def to_template_context(self):+ context = {+ 'entity': self.entity,+ 'sensor_history_list_map': self.sensor_history_list_map,+ }+ return contextdiff --git a/src/hi/integrations/core/managers.py b/src/hi/integrations/core/managers.pynew file mode 100644index 00000000..2d47ee63--- /dev/null+++ b/src/hi/integrations/core/managers.py@@ -0,0 +1,22 @@+from typing import Sequence++from django.db import models+from django.db.models import Q++from .integration_key import IntegrationKey+++class IntegrationKeyManager(models.Manager):++ def filter_by_integration_key( self, integration_key : IntegrationKey ):+ return self.filter( integration_id = integration_key.integration_id,+ integration_name = integration_key.integration_name )++ def filter_by_integration_keys( self, integration_keys : Sequence[ IntegrationKey ] ):+ query = Q()+ for integration_key in integration_keys:+ query |= Q( integration_id = integration_key.integration_id,+ integration_name = integration_key.integration_name )+ continue+ return self.filter(query)+diff --git a/src/hi/integrations/core/models.py b/src/hi/integrations/core/models.pyindex b2a88a32..8388a85f 100644--- a/src/hi/integrations/core/models.py+++ b/src/hi/integrations/core/models.py@@ -5,6 +5,7 @@ from django.db import modelsfrom hi.apps.attribute.models import AttributeModelfrom .integration_key import IntegrationKey+from .managers import IntegrationKeyManagerclass Integration( models.Model ):@@ -68,6 +69,7 @@ class IntegrationKeyModel( models.Model ):For use in DB objects that need to be associated with an integrationdevice, sensor, controller, attribute, etc."""+ objects = IntegrationKeyManager()class Meta:abstract = True