Skip to content

Home Information - Your home, your data, organized.

Python

Working on monitor logic to fetch state values. WIP

Anthony Cassandradb771636621e
diff --git a/src/hi/apps/collection/models.py b/src/hi/apps/collection/models.py
index dcfe65a6..84a5e99b 100644
--- a/src/hi/apps/collection/models.py
+++ b/src/hi/apps/collection/models.py
@@ -58,13 +58,13 @@ class CollectionEntity(models.Model):
Collection,
related_name = 'entities',
verbose_name = 'Collection',
- on_delete=models.CASCADE,
+ on_delete = models.CASCADE,
)
entity = models.ForeignKey(
Entity,
related_name = 'collections',
verbose_name = 'Entity',
- on_delete=models.CASCADE,
+ on_delete = models.CASCADE,
)
order_id = models.PositiveIntegerField(
'Order Id',
diff --git a/src/hi/apps/control/models.py b/src/hi/apps/control/models.py
index c9de0597..37c995ce 100644
--- a/src/hi/apps/control/models.py
+++ b/src/hi/apps/control/models.py
@@ -21,7 +21,7 @@ class Controller( IntegrationKeyModel ):
EntityState,
related_name = 'controllers',
verbose_name = 'Entity State',
- on_delete=models.CASCADE,
+ on_delete = models.CASCADE,
)
controller_type_str = models.CharField(
'Controller Type',
@@ -32,6 +32,12 @@ class Controller( IntegrationKeyModel ):
class Meta:
verbose_name = 'Controller'
verbose_name_plural = 'Controllers'
+ constraints = [
+ models.UniqueConstraint(
+ fields = [ 'integration_id', 'integration_name' ],
+ name = 'controller_integration_key',
+ ),
+ ]
@property
def controller_type(self):
diff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.py
index 9453ca3e..0f9869b9 100644
--- a/src/hi/apps/entity/models.py
+++ b/src/hi/apps/entity/models.py
@@ -55,6 +55,12 @@ class Entity( IntegrationKeyModel, LocationItemModelMixin ):
class Meta:
verbose_name = 'Entity'
verbose_name_plural = 'Entities'
+ constraints = [
+ models.UniqueConstraint(
+ fields = [ 'integration_id', 'integration_name' ],
+ name = 'entity_integration_key',
+ ),
+ ]
def __str__(self):
return f'Entity[{self.id}]: {self.name} [{self.entity_type_str}]'
@@ -213,7 +219,7 @@ class EntityStateDelegation(models.Model):
EntityState,
related_name = 'entity_state_delegations',
verbose_name = 'Entity State',
- on_delete=models.CASCADE,
+ on_delete = models.CASCADE,
)
delegate_entity = models.ForeignKey(
Entity,
diff --git a/src/hi/apps/sense/models.py b/src/hi/apps/sense/models.py
index b192221f..8c70cc2a 100644
--- a/src/hi/apps/sense/models.py
+++ b/src/hi/apps/sense/models.py
@@ -21,7 +21,7 @@ class Sensor( IntegrationKeyModel ):
EntityState,
related_name = 'sensors',
verbose_name = 'Entity State',
- on_delete=models.CASCADE,
+ on_delete = models.CASCADE,
)
sensor_type_str = models.CharField(
'Sensor Type',
@@ -32,7 +32,13 @@ class Sensor( IntegrationKeyModel ):
class Meta:
verbose_name = 'Sensor'
verbose_name_plural = 'Sensors'
-
+ constraints = [
+ models.UniqueConstraint(
+ fields = [ 'integration_id', 'integration_name' ],
+ name = 'sensor_integration_key',
+ ),
+ ]
+
@property
def sensor_type(self):
return SensorType.from_name_safe( self.sensor_type_str )
diff --git a/src/hi/integrations/core/models.py b/src/hi/integrations/core/models.py
index 1b88a017..b2a88a32 100644
--- a/src/hi/integrations/core/models.py
+++ b/src/hi/integrations/core/models.py
@@ -52,7 +52,7 @@ class IntegrationAttribute( AttributeModel ):
Integration,
related_name = 'attributes',
verbose_name = 'Integration',
- on_delete=models.CASCADE,
+ on_delete = models.CASCADE,
)
class Meta:
diff --git a/src/hi/integrations/hass/hass_converter.py b/src/hi/integrations/hass/hass_converter.py
index f0e6c4c0..31310d4f 100644
--- a/src/hi/integrations/hass/hass_converter.py
+++ b/src/hi/integrations/hass/hass_converter.py
@@ -127,7 +127,15 @@ class HassConverter:
def hass_states_to_hass_devices( cls,
hass_entity_id_to_state : Dict[ str, HassState ]
) -> Dict[ str, HassDevice ]:
-
+ """
+ The Home Assistant (HAss) model we see by fetching the HAss states does
+ not explicitly define the 'devices' that those states are attached
+ to. These devices are the model equivalent of the 'Entity' model,
+ while HAss states will map 1-to-1 with the 'EntityState' models.
+ Thus, we use this routine to heuristally collate the HA states into
+ HAss devices to help map from the HAss model to this app's model.
+ """
+
##########
# First pass to gather candidate device names.
@@ -217,19 +225,19 @@ class HassConverter:
@classmethod
def create_models_for_hass_device( cls, hass_device : HassDevice ) -> Entity:
-
- integration_key = cls.hass_device_to_integration_key( hass_device = hass_device )
- entity_name = cls.hass_device_to_entity_name( hass_device )
- entity_type = cls.hass_device_to_entity_type( hass_device )
with transaction.atomic():
+ entity_integration_key = cls.hass_device_to_integration_key( hass_device = hass_device )
+ entity_name = cls.hass_device_to_entity_name( hass_device )
+ entity_type = cls.hass_device_to_entity_type( hass_device )
+
entity = Entity(
name = entity_name,
entity_type_str = str(entity_type),
can_user_delete = HassMetaData.allow_entity_deletion,
)
- entity.integration_key = integration_key
+ entity.integration_key = entity_integration_key
entity.save()
insteon_address = cls.hass_device_to_insteon_address( hass_device )
@@ -249,11 +257,13 @@ class HassConverter:
#
for hass_state in hass_device.hass_state_list:
+ state_integration_key = cls.hass_state_to_integration_key( hass_state = hass_state )
+
cls._create_hass_state_sensor_or_controller(
hass_device = hass_device,
hass_state = hass_state,
entity = entity,
- integration_key = integration_key,
+ integration_key = state_integration_key,
)
continue
@@ -486,3 +496,11 @@ class HassConverter:
integration_id = HassMetaData.integration_id,
integration_name = hass_device.device_id,
)
+
+ @classmethod
+ def hass_state_to_integration_key( cls, hass_state : HassState ) -> IntegrationKey:
+ return IntegrationKey(
+ integration_id = HassMetaData.integration_id,
+ integration_name = hass_state.entity_id,
+ )
+
diff --git a/src/hi/integrations/hass/hass_models.py b/src/hi/integrations/hass/hass_models.py
index bbf58523..5e22416b 100644
--- a/src/hi/integrations/hass/hass_models.py
+++ b/src/hi/integrations/hass/hass_models.py
@@ -93,6 +93,10 @@ class HassState:
def friendly_name(self):
return self.attributes.get( HassApi.FRIENDLY_NAME_ATTR )
+ @property
+ def state_value(self):
+ return self.attributes.get( HassApi.STATE_FIELD )
+
@property
def device_class(self):
return self.attributes.get( HassApi.DEVICE_CLASS_ATTR )
@@ -124,7 +128,7 @@ class HassDevice:
def __init__( self, device_id : str ):
self._device_id = device_id
- self._state_list = list()
+ self._hass_state_list = list()
return
def __str__(self):
@@ -138,25 +142,25 @@ class HassDevice:
return self._device_id
def add_state( self, hass_state : HassState ):
- self._state_list.append( hass_state )
+ self._hass_state_list.append( hass_state )
return
@property
def hass_state_list(self):
- return self._state_list
+ return self._hass_state_list
@property
def device_class_set(self):
- return { x.device_class for x in self._state_list if x.device_class }
+ return { x.device_class for x in self._hass_state_list if x.device_class }
@property
def entity_id_prefix_set(self):
- return { x.entity_id_prefix for x in self._state_list }
+ return { x.entity_id_prefix for x in self._hass_state_list }
def to_dict(self):
return {
'device_id': self.device_id,
- 'num_states': len(self._state_list),
+ 'num_states': len(self._hass_state_list),
'prefixes': list( self.entity_id_prefix_set ),
- 'states': [ x.api_dict for x in self._state_list ],
+ 'states': [ x.api_dict for x in self._hass_state_list ],
}
diff --git a/src/hi/integrations/hass/hass_monitor.py b/src/hi/integrations/hass/hass_monitor.py
index 355af136..7c82deec 100644
--- a/src/hi/integrations/hass/hass_monitor.py
+++ b/src/hi/integrations/hass/hass_monitor.py
@@ -3,6 +3,7 @@ import logging
from hi.apps.monitor.periodic_monitor import PeriodicMonitor
from hi.apps.monitor.state_monitor_mixin import EntityStateMonitorMixin
+from .hass_converter import HassConverter
from .hass_manager import HassManager
logger = logging.getLogger(__name__)
@@ -24,8 +25,12 @@ class HassMonitor( PeriodicMonitor, EntityStateMonitorMixin ):
id_to_hass_state_map = self._manager.fetch_hass_states_from_api( verbose = False )
for hass_state in id_to_hass_state_map.values():
+ state_integration_key = HassConverter.hass_state_to_integration_key( hass_state = hass_state )
+ hass_state.state_value
+
+