Home Information - Your home, your data, organized.
Python
Added querying sensor responses to EntityInfo and CollectionView.
Anthony Cassandra4255dd56ea97
diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.pyindex fad32eee..3105645e 100644--- a/src/hi/apps/collection/collection_manager.py+++ b/src/hi/apps/collection/collection_manager.py@@ -6,6 +6,7 @@ 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@@ -74,16 +75,24 @@ class CollectionManager(Singleton):order_id = order_id,)- def get_collection_data( self, collection : Collection ):+ def get_collection_data( self,+ collection : Collection,+ is_editing : bool ):- entity_list = list()+ entity_manager = EntityManager()++ entity_info_data_list = list()for collection_entity in collection.entities.all().order_by('order_id'):- entity_list.append( collection_entity.entity )+ entity_info_data = entity_manager.get_entity_info_data(+ entity = collection_entity.entity,+ is_editing = is_editing,+ )+ entity_info_data_list.append( entity_info_data )continuereturn CollectionData(collection = collection,- entity_list = entity_list,+ entity_info_data_list = entity_info_data_list,)def create_collection_entity( self,diff --git a/src/hi/apps/collection/templates/collection/panes/collection_view.html b/src/hi/apps/collection/templates/collection/panes/collection_view.htmlindex 51baad0b..e49c555f 100644--- a/src/hi/apps/collection/templates/collection/panes/collection_view.html+++ b/src/hi/apps/collection/templates/collection/panes/collection_view.html@@ -1,7 +1,8 @@<div id="hi-collection-view-main" class="hi-collection-view draggable-container""><h5><sub>COLLECTION:</sub> {{ collection_data.collection.name }}</h5>- {% for entity in collection_data.entity_list %}+ {% for entity_info_data in collection_data.entity_info_data_list %}+ {% with entity=entity_info_data.entity %}<div id="{{ entity.html_id }}"class="{% if request.is_editing %}draggable{% endif %}"{% if request.is_editing %}draggable="true"{% endif %}@@ -24,10 +25,18 @@Controls</div><div>- States+ <h5>Sensor Responses</h5>+ <ul>+ {% for sensor, sensor_response in entity_info_data.sensor_response_map.items %}+ <li>Sensor: {{ sensor.name }}, Response: {{ sensor_response.value }}</li>+ {% empty %}+ <div><em>No sensors</em></div>+ {% endfor %}+ </ul></div></div></div>+ {% endwith %}{% empty %}<div><em>No entities in collection.</em></div>{% endfor %}diff --git a/src/hi/apps/collection/transient_models.py b/src/hi/apps/collection/transient_models.pyindex 875a8a59..b6b5a0a4 100644--- a/src/hi/apps/collection/transient_models.py+++ b/src/hi/apps/collection/transient_models.py@@ -4,6 +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 EntityInfoDatafrom hi.apps.location.models import LocationViewfrom .models import Collection@@ -13,7 +14,7 @@ from .models import Collectionclass CollectionData:collection : Collection- entity_list : List[ Entity ]+ entity_info_data_list : List[ EntityInfoData ]@dataclassdiff --git a/src/hi/apps/collection/views.py b/src/hi/apps/collection/views.pyindex 368e66ba..b92cd92b 100644--- a/src/hi/apps/collection/views.py+++ b/src/hi/apps/collection/views.py@@ -60,6 +60,7 @@ class CollectionViewView( HiGridView, CollectionViewMixin ):collection_data = CollectionManager().get_collection_data(collection = collection,+ is_editing = request.is_editing,)return {'is_async_request': is_ajax( request ),diff --git a/src/hi/apps/entity/entity_manager.py b/src/hi/apps/entity/entity_manager.pyindex e5c2854f..fad67770 100644--- a/src/hi/apps/entity/entity_manager.py+++ b/src/hi/apps/entity/entity_manager.py@@ -7,6 +7,7 @@ from hi.apps.common.singleton import Singletonfrom hi.apps.entity.edit.forms import EntityPositionFormfrom hi.apps.location.models import Location, LocationViewfrom hi.apps.location.svg_item_factory import SvgItemFactory+from hi.apps.sense.sensor_response_manager import SensorResponseManagerfrom .delegation_manager import DelegationManagerfrom .enums import (@@ -36,6 +37,8 @@ class EntityManager(Singleton):entity : Entity,is_editing : bool ) -> EntityDetailsData:entity_edit_data = EntityEditData( entity = entity )+ sensor_response_map = SensorResponseManager().get_entity_latest_sensor_responses( entity = entity )+entity_state_list = list(entity.states.all())@@ -44,9 +47,10 @@ class EntityManager(Singleton):)principal_state_list = [ x.entity_state for x in entity_state_delegation_list ]principal_entity_list = list({ x.entity for x in principal_state_list }) # de-dupe-+return EntityInfoData(entity_edit_data = entity_edit_data,+ sensor_response_map = sensor_response_map,entity_state_list = entity_state_list,principal_state_list = principal_state_list,principal_entity_list = principal_entity_list,diff --git a/src/hi/apps/entity/templates/entity/modals/entity_info.html b/src/hi/apps/entity/templates/entity/modals/entity_info.htmlindex d269cadf..8285656b 100644--- a/src/hi/apps/entity/templates/entity/modals/entity_info.html+++ b/src/hi/apps/entity/templates/entity/modals/entity_info.html@@ -6,6 +6,15 @@{% block body %}+<h5>Sensor Responses</h5>+<ul>+ {% for sensor, sensor_response in sensor_response_map.items %}+ <li>Sensor: {{ sensor.name }}, Response: {{ sensor_response.value }}</li>+ {% empty %}+ <div><em>No sensors</em></div>+ {% endfor %}+</ul>+<h5>Principal Entities</h5><ul>{% for principal_entity in principal_entity_list %}diff --git a/src/hi/apps/entity/transient_models.py b/src/hi/apps/entity/transient_models.pyindex c3924f6f..d8f94de4 100644--- a/src/hi/apps/entity/transient_models.py+++ b/src/hi/apps/entity/transient_models.py@@ -1,5 +1,5 @@from dataclasses import dataclass, field-from typing import List+from typing import Dict, Listfrom hi.apps.entity.edit.forms import (EntityAttributeFormSet,@@ -8,6 +8,8 @@ from hi.apps.entity.edit.forms import (EntityAttributeUploadForm,)from hi.apps.location.models import LocationView+from hi.apps.sense.models import Sensor+from hi.apps.sense.transient_models import SensorResponsefrom .enums import EntityTypefrom .models import Entity, EntityState@@ -71,13 +73,19 @@ class EntityInfoData:""" All the data needed to render the Entity info modal. """entity_edit_data : EntityEditData+ sensor_response_map : Dict[ Sensor, SensorResponse ]entity_state_list : List[ EntityState ]principal_state_list : List[ EntityState ]principal_entity_list : List[ Entity ]++ @property+ def entity(self):+ return self.entity_edit_data.entitydef to_template_context(self):context = {- 'entity': self.entity_edit_data.entity,+ 'entity': self.entity,+ 'sensor_response_map': self.sensor_response_map,'entity_state_list': self.entity_state_list,'principal_state_list': self.principal_state_list,'principal_entity_list': self.principal_entity_list,diff --git a/src/hi/apps/location/location_manager.py b/src/hi/apps/location/location_manager.pyindex bb4027a0..b228e0a1 100644--- a/src/hi/apps/location/location_manager.py+++ b/src/hi/apps/location/location_manager.py@@ -10,7 +10,7 @@ from hi.apps.common.singleton import Singletonfrom hi.apps.common.svg_models import SvgViewBoxfrom .enums import LocationViewType-from .status_display_helper import StatusDisplayHelper+from .status_display_helper import StatusDisplayLocationHelperfrom .transient_models import LocationViewDatafrom .models import (Location,@@ -208,7 +208,7 @@ class LocationManager(Singleton):unpositioned_collections.sort( key = lambda item : item.order_id )if include_status_display_data:- status_display_helper = StatusDisplayHelper( location_view = location_view )+ status_display_helper = StatusDisplayLocationHelper( location_view = location_view )status_display_data_map = status_display_helper.get_status_display_data_map(displayed_entities = displayed_entities,)diff --git a/src/hi/apps/location/status_display_helper.py b/src/hi/apps/location/status_display_helper.pyindex 813699ba..912fe731 100644--- a/src/hi/apps/location/status_display_helper.py+++ b/src/hi/apps/location/status_display_helper.py@@ -3,18 +3,20 @@ from typing import Dict, List, Setfrom hi.apps.entity.enums import EntityStateTypefrom hi.apps.entity.models import Entity, EntityStatefrom hi.apps.sense.sensor_response_manager import SensorResponseManager+from hi.apps.sense.models import Sensor+from hi.apps.sense.transient_models import SensorResponsefrom .enums import StatusDisplayTypefrom .models import LocationViewfrom .transient_models import StatusDisplayData-class StatusDisplayHelper:+class StatusDisplayLocationHelper:def __init__( self, location_view : LocationView ):self._status_display_type = location_view.status_display_typeself._entity_state_type_priority_list = self._status_display_type.entity_state_type_priority_list- self._latest_sensor_response_map = SensorResponseManager().get_latest_sensor_responses()+ self._latest_sensor_response_map = SensorResponseManager().get_all_latest_sensor_responses()returndef get_status_display_data_map(@@ -89,4 +91,3 @@ class StatusDisplayHelper:# This code should not really be reachable. Should always match one in fallback case.return None-diff --git a/src/hi/apps/sense/sensor_response_manager.py b/src/hi/apps/sense/sensor_response_manager.pyindex 662a4e36..1b901271 100644--- a/src/hi/apps/sense/sensor_response_manager.py+++ b/src/hi/apps/sense/sensor_response_manager.py@@ -3,9 +3,10 @@ from typing import Dict, Listfrom hi.apps.common.redis_client import get_redis_clientfrom hi.apps.common.singleton import Singleton-+from hi.apps.entity.models import Entityfrom hi.integrations.core.integration_key import IntegrationKey+from .models import Sensorfrom .sensor_history_manager import SensorHistoryManagerfrom .transient_models import SensorResponse@@ -71,12 +72,28 @@ class SensorResponseManager( Singleton ):logger.debug( f'Sensor changed: {len(changed_sensor_response_list)} of {len(sensor_response_map)}' )return- def get_latest_sensor_responses( self ) -> Dict[ IntegrationKey, SensorResponse ]:+ def get_all_latest_sensor_responses( self ) -> Dict[ IntegrationKey, SensorResponse ]:key_to_sensor_response_dict = self._redis_client.hgetall( self.LATEST_SENSOR_RESPONSE_HASH_NAME )key_to_sensor_response = { k: SensorResponse.from_string( v )for k, v in key_to_sensor_response_dict.items() }return { v.integration_key: v for k, v in key_to_sensor_response.items() }- def to_sensor_value_cache_key( self, integration_key : IntegrationKey ):+ def get_entity_latest_sensor_responses( self, entity : Entity ) -> Dict[ Sensor, SensorResponse ]:+ """ Maps all sensors to the latest response, but possibly 'None' if not latest response """++ sensor_response_map = dict()+ for entity_state in entity.states.all():+ for sensor in entity_state.sensors.all():+ cache_key = self.to_sensor_value_cache_key( sensor.integration_key )+ sensor_response_str = self._redis_client.hget( self.LATEST_SENSOR_RESPONSE_HASH_NAME,+ cache_key )+ sensor_response = SensorResponse.from_string( sensor_response_str )+ sensor_response_map[sensor] = sensor_response+ continue+ continue++ return sensor_response_map++ def to_sensor_value_cache_key( self, integration_key : IntegrationKey ) -> str:return f'hi.sr.{integration_key}'