Skip to content

Home Information - Your home, your data, organized.

Python

Some cleanup and refinements around status displays.

Anthony Cassandra77fd22aaae72
diff --git a/src/hi/apps/api/views.py b/src/hi/apps/api/views.py
index 6d43cfcb..d3c50ebc 100644
--- a/src/hi/apps/api/views.py
+++ b/src/hi/apps/api/views.py
@@ -17,7 +17,7 @@ class StatusView( View ):
def get( self, *args, **kwargs ):
- entity_state_status_data_list = StatusDataHelper().get_entity_state_status_data_list()
+ entity_state_status_data_list = StatusDataHelper().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/entity/admin.py b/src/hi/apps/entity/admin.py
index 4062c044..f6380baf 100644
--- a/src/hi/apps/entity/admin.py
+++ b/src/hi/apps/entity/admin.py
@@ -1,5 +1,8 @@
from django.contrib import admin
+from hi.apps.control.models import Controller
+from hi.apps.sense.models import Sensor
+
from . import models
@@ -27,6 +30,18 @@ class PathInLine(admin.TabularInline):
show_change_link = True
+class ControllerInLine(admin.TabularInline):
+ model = Controller
+ extra = 0
+ show_change_link = True
+
+
+class SensorInLine(admin.TabularInline):
+ model = Sensor
+ extra = 0
+ show_change_link = True
+
+
class EntityStateDelegationInLine(admin.TabularInline):
model = models.EntityStateDelegation
extra = 0
@@ -64,6 +79,28 @@ class EntityAdmin(admin.ModelAdmin):
]
[email protected](models.EntityState)
+class EntityStateAdmin(admin.ModelAdmin):
+
+ show_full_result_count = False
+
+ list_display = (
+ 'entity',
+ 'name',
+ 'entity_state_type_str',
+ 'value_range',
+ 'units',
+ 'created_datetime',
+ )
+
+ search_fields = ['name']
+ readonly_fields = ( 'entity', 'created_datetime', )
+ inlines = [
+ ControllerInLine,
+ SensorInLine,
+ ]
+
+
@admin.register(models.EntityStateDelegation)
class EntityStateDelegationAdmin(admin.ModelAdmin):
diff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.py
index a87e8698..cec4af25 100644
--- a/src/hi/apps/entity/models.py
+++ b/src/hi/apps/entity/models.py
@@ -66,7 +66,7 @@ class Entity( IntegrationKeyModel, LocationItemModelMixin ):
]
def __str__(self):
- return f'Entity[{self.id}]: {self.name} [{self.entity_type_str}]'
+ return f'{self.name} ({self.entity_type_str}) [{self.id}]'
def __repr__(self):
return self.__str__()
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.html
index 4f6c8601..3fdbe0a5 100644
--- 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.html
@@ -1,22 +1,9 @@
<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 "sense/panes/sensor_response.html" with sensor_response=sensor_response %}
- {% else %}
- <div><em>No sensor responses.</em></div>
- {% endif %}
-
- {% for controller_data in entity_state_status_data.controller_data_list %}
- {% include "control/panes/controller_data.html" with controller_data=controller_data %}
- {% empty %}
- <div><em>No controllers.</em></div>
- {% endfor %}
- {% endwith %}
-
- {% empty %}
- <div><em>No state info.</em></div>
- {% endfor %}
+ {% include "entity/panes/entity_state_status_data.html" with entity_state_status_data=entity_state_status_data %}
+ </li>
+ {% empty %}
+ <div><em>No state info.</em></div>
+ {% endfor %}
</ul>
diff --git a/src/hi/apps/entity/templates/entity/panes/entity_state_status_data.html b/src/hi/apps/entity/templates/entity/panes/entity_state_status_data.html
new file mode 100644
index 00000000..e4dcc9d6
--- /dev/null
+++ b/src/hi/apps/entity/templates/entity/panes/entity_state_status_data.html
@@ -0,0 +1,17 @@
+{% if entity_state_status_data.controller_data_list %}
+
+{% for controller_data in entity_state_status_data.controller_data_list %}
+{% include "control/panes/controller_data.html" with controller_data=controller_data %}
+{% endfor %}
+
+{% else %}
+
+{% with sensor_response=entity_state_status_data.latest_sensor_response %}
+{% if sensor_response %}
+{% include "sense/panes/sensor_response.html" with sensor_response=sensor_response %}
+{% else %}
+<div><em>No sensors or controllers.</em></div>
+{% endif %}
+{% endwith %}
+
+{% endif %}
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.html
index f579bc99..39758841 100644
--- 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.html
@@ -1,19 +1,6 @@
{% 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 "sense/panes/sensor_response.html" with sensor_response=sensor_response %}
- {% else %}
- <div><em>No sensor responses.</em></div>
- {% endif %}
- {% endwith %}
-
- {% for controller_data in entity_state_status_data.controller_data_list %}
- {% include "control/panes/controller_data.html" with controller_data=controller_data %}
- {% empty %}
- <div><em>No controllers.</em></div>
- {% endfor %}
+ {% include "entity/panes/entity_state_status_data.html" with entity_state_status_data=entity_state_status_data %}
</div>
{% empty %}
diff --git a/src/hi/apps/monitor/status_data_helpers.py b/src/hi/apps/monitor/status_data_helpers.py
index f16b4379..21f3f48a 100644
--- a/src/hi/apps/monitor/status_data_helpers.py
+++ b/src/hi/apps/monitor/status_data_helpers.py
@@ -13,15 +13,68 @@ from .transient_models import EntityStatusData, EntityStateStatusData
class StatusDataHelper:
+ def get_all_entity_state_status_data_list( self ) -> List[ EntityStateStatusData ]:
+ """
+ Gets the latest sensor responses for all EntityStates. Used by client
+ background polling to refresh the UI visual display of the current
+ state.
+ """
+
+ 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.
+ #
+ # For a given EntityState, we do not display multiple sensors if it
+ # has them. The display for the state uses an amalgam of all those
+ # sensors where the most recent response determines the current
+ # state.
+
+ # Collate latest sensor responses by EntityState.
+ #
+ 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 )
+ continue
+
+ # Find the latest sensor response for each EntityState and create
+ # the EntityStateStatusData instance for each.
+ #
+ entity_state_status_data_list = list()
+ 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 )
+ latest_sensor_response = sensor_response_list[0]
+ controller_data_list = [
+ ControllerData(
+ controller = controller,
+ latest_sensor_response = latest_sensor_response,
+ )
+ for controller in entity_state.controllers.all()
+ ]
+ entity_state_status_data = EntityStateStatusData(
+ entity_state = entity_state,
+ sensor_response_list = sensor_response_list,
+ controller_data_list = controller_data_list,
+ )
+ entity_state_status_data_list.append( entity_state_status_data )
+ continue
+
+ return entity_state_status_data_list
+
def get_entity_status_data( self, entity : Entity ) -> EntityStatusData:
+ # The set of entity states used to define the state includes the
+ # principals when the entity is a delegate.
+ #
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_status_data = self.get_entity_state_to_entity_state_status_data(
+ entity_state_to_status_data = self._get_entity_state_to_entity_state_status_data(
entity_states = entity_state_set,
)
return EntityStatusData(
@@ -32,10 +85,19 @@ class StatusDataHelper:
def get_entity_status_data_list(
self,
entities : Sequence[ Entity ] ) -> List[ EntityStatusData ]:
-
- entity_to_entity_status_data = self.get_entity_to_entity_status_data(
+ """
+ Same as _get_entity_to_entity_status_data() but returns a List instead of a Dict.
+ Preserves the ordering so resulting list in same order as input list.
+ """
+
+ entity_to_entity_status_data = self._get_entity_to_entity_status_data(
entities = entities,
)
+
+ # Reform dict values as a list matching input list order and ensure
+ # each entity has EntityStatusData (even if there are no latest
+ # sensor responses).
+ #
entity_status_data_list = list()
for entity in entities:
entity_status_data = entity_to_entity_status_data.get( entity )
@@ -48,11 +110,13 @@ class StatusDataHelper:
continue
return entity_status_data_list
- def get_entity_to_entity_status_data(
+ def _get_entity_to_entity_status_data(
self,
entities : Sequence[ Entity ] ) -> Dict[ Entity, EntityStatusData ]:
- # Gather all EntityState of interest.
+ # Gather all EntityStates for all Entities so we can issue a single
+ # fetch of the latest SensorResponses.
+ #
entity_to_entity_state_set = { x: set( x.states.all() ) for x in entities }
all_entity_states = set()
for entity, entity_state_set in entity_to_entity_state_set.items():
@@ -63,12 +127,14 @@ class StatusDataHelper:
continue
continue
- # Single fetch for getting all latest sensor data.
- entity_state_to_status_data = self.get_entity_state_to_entity_state_status_data(
+ # Includes a single fetch for getting all latest sensor data.
+ #
+ entity_state_to_status_data = self._get_entity_state_to_entity_state_status_data(
entity_states = all_entity_states,
)
# Collate EntityStateStatusData by Entity
+ #
entity_to_entity_status_data = dict()
for entity_state, entity_state_status_data in entity_state_to_status_data.items():
entity = entity_state.entity
@@ -85,10 +151,13 @@ class StatusDataHelper:
return entity_to_entity_status_data
- def get_entity_state_to_entity_state_status_data(
+ def _get_entity_state_to_entity_state_status_data(
self,
entity_states : Sequence[ EntityState ] ) -> Dict[ EntityState, EntityStateStatusData ]:
+ # Collect all the sensors for al the input EntityStates, so we can
+ # issue one fetch of the latest SensorData.
+ #
entity_state_to_sensor_list = dict()
all_sensor_list = list()
for entity_state in entity_states:
@@ -97,10 +166,15 @@ class StatusDataHelper:
all_sensor_list.extend( entity_state_sensor_list )
continue
+ # Single fetch for getting all latest sensor data.
+ #
sensor_response_list_map = SensorResponseManager().get_latest_sensor_responses(
sensor_list = all_sensor_list,
)
-
+
+ # Collates SensorResponses by EntityState, finds latest
+ # SensorResponse and creates the EntityStateStatusData instances.
+ #
entity_state_to_entity_state_status_data_map = dict()
for entity_state in entity_states:
entity_state_sensor_response_list = list()
@@ -140,14 +214,18 @@ class StatusDataHelper:
location_view : LocationView,
entities : Set[ Entity ] ) -> Dict[ Entity, List[ EntityStateStatusData ] ]:
"""
- A map from Entity to EntityStateStatusData for the single EntityState
- that is the highest display priority for the given LocationView.
+ Builds a map from Entity to EntityStateStatusData for the single EntityState
+ that is the highest display priority for the given
+ LocationView. i.e., Picks a single EntityState for each Entity to
+ define the visual display used in the LocationView where we can
+ really only represent one thing at a time (visually).
"""
+
location_view_type = location_view.location_view_type
if location_view_type == LocationViewType.SUPPRESS:
return dict()
- entity_to_entity_state_list = self.get_entity_to_entity_state_list(
+ entity_to_entity_state_list = self._get_entity_to_entity_state_list(
location_view = location_view,
entities = entities,
)
@@ -156,7 +234,7 @@ class StatusDataHelper:
all_entity_states.update( entity_state_list )
continue
- entity_state_to_status_data = self.get_entity_state_to_entity_state_status_data(
+ entity_state_to_status_data = self._get_entity_state_to_entity_state_status_data(
entity_states = all_entity_states,
)
@@ -173,7 +251,7 @@ class StatusDataHelper:
return entity_to_entity_state_status_data_list
- def get_entity_to_entity_state_list(
+ def _get_entity_to_entity_state_list(
self,
location_view : LocationView,
entities : Set[ Entity ] ) -> Dict[ Entity, List[ EntityState ] ]:
@@ -190,7 +268,7 @@ class StatusDataHelper:
entity_to_entity_state_list = dict()
for entity in entities:
- entity_state_list = self.get_status_entity_states(
+ entity_state_list = self._get_entity_state_list_for_status(
entity = entity,
entity_state_type_priority_list = entity_state_type_priority_list,
)
@@ -200,7 +278,7 @@ class StatusDataHelper:
return entity_to_entity_state_list
- def get_status_entity_states(
+ def _get_entity_state_list_for_status(
self,
entity : Entity,
entity_state_type_priority_list : List[ EntityStateType ] ) -> List[ EntityState ]:
@@ -277,39 +355,3 @@ class StatusDataHelper:
return entity_state_sensor_response_list[0]
return None
-
- def get_entity_state_status_data_list( self ) -> List[ EntityStateStatusData ]:
-
- 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_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 )
- continue
-
- entity_state_status_data_list = list()
- 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 )
- latest_sensor_response = sensor_response_list[0]
- controller_data_list = [
- ControllerData(
- controller = controller,
- latest_sensor_response = latest_sensor_response,
- )
- for controller in entity_state.controllers.all()
- ]
- entity_state_status_data = EntityStateStatusData(
- entity_state = entity_state,
- sensor_response_list = sensor_response_list,
- controller_data_list = controller_data_list,
- )
- entity_state_status_data_list.append( entity_state_status_data )
- continue
-
- return entity_state_status_data_list
diff --git a/src/hi/apps/monitor/status_display_data.py b/src/hi/apps/monitor/status_display_data.py
index 4e7bf331..e98bc35f 100644
--- a/src/hi/apps/monitor/status_display_data.py
+++ b/src/hi/apps/monitor/status_display_data.py
@@ -211,10 +211,10 @@ class StatusDisplayData:
def _get_movement_status_style( self ):
- if self.latest_sensor_value == str(SensorValue.MOVEMENT_ACTIVE):
+ if self.latest_sensor_value == str(SensorValue.ACTIVE):
return StatusStyle.MovementActive
- if self.penultimate_sensor_value == str(SensorValue.MOVEMENT_ACTIVE):
+ if self.penultimate_sensor_value == str(SensorValue.ACTIVE):
movement_timedelta = datetimeproxy.now() - self.penultimate_sensor_timestamp
if movement_timedelta.seconds < 30:
return StatusStyle.MovementRecent
@@ -226,10 +226,10 @@ class StatusDisplayData:
def _get_presence_status_style( self ):
- if self.latest_sensor_value == str(SensorValue.PRESENCE_ACTIVE):
+ if self.latest_sensor_value == str(SensorValue.ACTIVE):
return StatusStyle.PresenceActive
- if self.penultimate_sensor_value == str(SensorValue.PRESENCE_ACTIVE):
+ if self.penultimate_sensor_value == str(SensorValue.ACTIVE):
presence_timedelta = datetimeproxy.now() - self.penultimate_sensor_timestamp
if presence_timedelta.seconds < 30:
return StatusStyle.MovementRecent
diff --git a/src/hi/apps/sense/enums.py b/src/hi/apps/sense/enums.py
index 9b8545c2..23ce936e 100644
--- a/src/hi/apps/sense/enums.py
+++ b/src/hi/apps/sense/enums.py
@@ -8,20 +8,17 @@ class SensorType(LabeledEnum):
class SensorValue(LabeledEnum):
- MOVEMENT_ACTIVE = ( 'Active', '' )
- MOVEMENT_IDLE = ( 'Idle', '' )
+ ACTIVE = ( 'Active', '' )
+ IDLE = ( 'Idle', '' )
- ON = ( 'On', '' )
- OFF = ( 'Off', '' )
+ ON = ( 'On', '' )
+ OFF = ( 'Off', '' )
- OPEN = ( 'Open', '' )
- CLOSED = ( 'Closed', '' )
+ OPEN = ( 'Open', '' )
+ CLOSED = ( 'Closed', '' )
- CONNECTED = ( 'Connected', '' )
- DISCONNECTED = ( 'Disconnected', '' )
+ CONNECTED = ( 'Connected', '' )
+ DISCONNECTED = ( 'Disconnected', '' )
- HIGH = ( 'High', '' )
- LOW = ( 'Low', '' )
-
- PRESENCE_ACTIVE = ( 'Active', '' )
- PRESENCE_IDLE = ( 'Idle', '' )
+ HIGH = ( 'High', '' )
+ LOW = ( 'Low', '' )
diff --git a/src/hi/apps/sense/templates/sense/panes/sensor_value_connectivity.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_connectivity.html
new file mode 100644
index 00000000..540c7e80
--- /dev/null
+++ b/src/hi/apps/sense/templates/sense/panes/sensor_value_connectivity.html
@@ -0,0 +1,5 @@
+{% if sensor_response.value == 'connected' %}
+<div class="bg-success">{{ sensor_response.value }}</div>
+{% else %}
+<div class="bg-danger">{{ sensor_response.value }}</div>
+{% endif %}
diff --git a/src/hi/apps/sense/templates/sense/panes/sensor_value_high_low.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_high_low.html
new file mode 100644
index 00000000..6864e1db
--- /dev/null
+++ b/src/hi/apps/sense/templates/sense/panes/sensor_value_high_low.html
@@ -0,0 +1,5 @@
+{% if sensor_response.value == 'high' %}
+<div class="bg-success">{{ sensor_response.value }}</div>
+{% else %}
+<div class="bg-danger">{{ sensor_response.value }}</div>
+{% endif %}
diff --git a/src/hi/apps/sense/templates/sense/panes/sensor_value_movement.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_movement.html
new file mode 100644
index 00000000..3cc55640
--- /dev/null
+++ b/src/hi/apps/sense/templates/sense/panes/sensor_value_movement.html
@@ -0,0 +1,5 @@
+{% if sensor_response.value == 'active' %}
+<div class="bg-danger">{{ sensor_response.value }}</div>
+{% else %}
+<div class="bg-success">{{ sensor_response.value }}</div>
+{% endif %}
diff --git a/src/hi/apps/sense/templates/sense/panes/sensor_value_on_off.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_on_off.html
new file mode 100644
index 00000000..e69d1037
--- /dev/null
+++ b/src/hi/apps/sense/templates/sense/panes/sensor_value_on_off.html
@@ -0,0 +1,5 @@
+{% if sensor_response.value == 'on' %}
+<div class="bg-success">{{ sensor_response.value }}</div>
+{% else %}
+<div class="bg-danger">{{ sensor_response.value }}</div>
+{% endif %}
diff --git a/src/hi/apps/sense/templates/sense/panes/sensor_value_open_close.html b/src/hi/apps/sense/templates/sense/panes/sensor_value_open_close.html
new file mode 100644
index 00000000..4bc86296
--- /dev/null
+++ b/src/hi/apps/sense/templates/sense/panes/sensor_value_open_close.html
@@ -0,0 +1,5 @@
+{% if sensor_response.value == 'open' %}
+<div class="bg-success">{{ sensor_response.value }}</div>
+{% else %}
+<div class="bg-danger">{{ sensor_response.value }}</div>
+{% endif %}
diff --git a/src/hi/integrations/zoneminder/zm_monitor.py b/src/hi/integrations/zoneminder/zm_monitor.py
index 845316e8..441ad0c3 100644
--- a/src/hi/integrations/zoneminder/zm_monitor.py
+++ b/src/hi/integrations/zoneminder/zm_monitor.py
@@ -217,7 +217,7 @@ class ZoneMinderMonitor( PeriodicMonitor ):
sensor_prefix = self._zm_manager.MOVEMENT_SENSOR_PREFIX,
zm_monitor_id = zm_event.monitor_id,
),
- value = str(SensorValue.MOVEMENT_ACTIVE),
+ value = str(SensorValue.ACTIVE),
timestamp = zm_event.start_datetime,
details = SensorResponseHelper.event_to_details( zm_event = zm_event ),
)
@@ -228,7 +228,7 @@ class ZoneMinderMonitor( PeriodicMonitor ):
sensor_prefix = self._zm_manager.MOVEMENT_SENSOR_PREFIX,
zm_monitor_id = zm_event.monitor_id,
),
- value = str(SensorValue.MOVEMENT_IDLE),
+ value = str(SensorValue.IDLE),
timestamp = zm_event.end_datetime,
)
@@ -238,7 +238,7 @@ class ZoneMinderMonitor( PeriodicMonitor ):
sensor_prefix = self._zm_manager.MOVEMENT_SENSOR_PREFIX,
zm_monitor_id = zm_monitor.id(),
),
- value = str(SensorValue.MOVEMENT_IDLE),
+ value = str(SensorValue.IDLE),
timestamp = timestamp,
)