Skip to content

Home Information - Your home, your data, organized.

Python

Created better encapsulation for IntegrationId.

Anthony Cassandraea154bef1d87
diff --git a/src/hi/apps/control/admin.py b/src/hi/apps/control/admin.py
index 3546e111..931244e3 100644
--- a/src/hi/apps/control/admin.py
+++ b/src/hi/apps/control/admin.py
@@ -13,7 +13,7 @@ class ControllerAdmin(admin.ModelAdmin):
'entity_state',
'controller_type_str',
'integration_type_str',
- 'integration_id',
+ 'integration_key',
)
search_fields = ['name']
diff --git a/src/hi/apps/control/migrations/0003_rename_integration_id_controller_integration_key.py b/src/hi/apps/control/migrations/0003_rename_integration_id_controller_integration_key.py
new file mode 100644
index 00000000..27d33381
--- /dev/null
+++ b/src/hi/apps/control/migrations/0003_rename_integration_id_controller_integration_key.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.15 on 2024-08-25 23:55
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("control", "0002_controller_integration_id_and_more"),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name="controller",
+ old_name="integration_id",
+ new_name="integration_key",
+ ),
+ ]
diff --git a/src/hi/apps/control/migrations/0004_alter_controller_integration_key.py b/src/hi/apps/control/migrations/0004_alter_controller_integration_key.py
new file mode 100644
index 00000000..67df205e
--- /dev/null
+++ b/src/hi/apps/control/migrations/0004_alter_controller_integration_key.py
@@ -0,0 +1,20 @@
+# Generated by Django 4.2.15 on 2024-08-25 23:56
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("control", "0003_rename_integration_id_controller_integration_key"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="controller",
+ name="integration_key",
+ field=models.CharField(
+ blank=True, max_length=128, null=True, verbose_name="Integration Key"
+ ),
+ ),
+ ]
diff --git a/src/hi/apps/control/models.py b/src/hi/apps/control/models.py
index 3e9faa17..e75deb0a 100644
--- a/src/hi/apps/control/models.py
+++ b/src/hi/apps/control/models.py
@@ -1,12 +1,12 @@
from django.db import models
from hi.apps.entity.models import Entity, EntityState
-from hi.integrations.core.enums import IntegrationType
+from hi.integrations.core.models import IntegrationIdModel
from .enums import ControllerType
-class Controller( models.Model ):
+class Controller( IntegrationIdModel ):
"""
- Represents an action that can be taken.
- Will control exactly one EntityState
@@ -28,16 +28,6 @@ class Controller( models.Model ):
max_length = 32,
null = False, blank = False,
)
- integration_type_str = models.CharField(
- 'Integration Type',
- max_length = 32,
- null = True, blank = True,
- )
- integration_id = models.CharField(
- 'Integration Id',
- max_length = 128,
- null = True, blank = True,
- )
class Meta:
verbose_name = 'Controller'
@@ -52,15 +42,6 @@ class Controller( models.Model ):
self.controller_type_str = str(controller_type)
return
- @property
- def integration_type(self):
- return IntegrationType.from_name_safe( self.integration_type_str )
-
- @integration_type.setter
- def integration_type( self, integration_type : IntegrationType ):
- self.integration_type_str = str(integration_type)
- return
-
class ControlledEntity( models.Model ):
"""The Entity associated with a Controller's state is implicitly a
diff --git a/src/hi/apps/entity/admin.py b/src/hi/apps/entity/admin.py
index 3b2dc458..4f053821 100644
--- a/src/hi/apps/entity/admin.py
+++ b/src/hi/apps/entity/admin.py
@@ -36,7 +36,7 @@ class EntityAdmin(admin.ModelAdmin):
'name',
'entity_type_str',
'integration_type_str',
- 'integration_id',
+ 'integration_key',
'created_datetime',
)
diff --git a/src/hi/apps/entity/enums.py b/src/hi/apps/entity/enums.py
index d008b8bc..9f440c6b 100644
--- a/src/hi/apps/entity/enums.py
+++ b/src/hi/apps/entity/enums.py
@@ -73,7 +73,7 @@ class EntityType(LabeledEnum):
class EntityStateType(LabeledEnum):
# General types
- DICRETE = ( 'Dicrete' , 'Single value, fixed set of possible values' )
+ DISCRETE = ( 'Discrete' , 'Single value, fixed set of possible values' )
CONTINUOUS = ( 'Continuous' , 'For single value with a float type value' )
MULTVALUED = ( 'Multi-valued' , 'Provides multiple name-value pairs' )
BLOB = ( 'Blob' , 'Provides blob of uninterpreted data' )
diff --git a/src/hi/apps/entity/migrations/0003_rename_integration_id_entity_integration_key.py b/src/hi/apps/entity/migrations/0003_rename_integration_id_entity_integration_key.py
new file mode 100644
index 00000000..e4d04942
--- /dev/null
+++ b/src/hi/apps/entity/migrations/0003_rename_integration_id_entity_integration_key.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.15 on 2024-08-25 23:55
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("entity", "0002_entity_integration_id_entity_integration_type_str_and_more"),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name="entity",
+ old_name="integration_id",
+ new_name="integration_key",
+ ),
+ ]
diff --git a/src/hi/apps/entity/migrations/0004_alter_entity_integration_key.py b/src/hi/apps/entity/migrations/0004_alter_entity_integration_key.py
new file mode 100644
index 00000000..542b9e81
--- /dev/null
+++ b/src/hi/apps/entity/migrations/0004_alter_entity_integration_key.py
@@ -0,0 +1,20 @@
+# Generated by Django 4.2.15 on 2024-08-25 23:56
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("entity", "0003_rename_integration_id_entity_integration_key"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="entity",
+ name="integration_key",
+ field=models.CharField(
+ blank=True, max_length=128, null=True, verbose_name="Integration Key"
+ ),
+ ),
+ ]
diff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.py
index b3d5ce8e..15483cc3 100644
--- a/src/hi/apps/entity/models.py
+++ b/src/hi/apps/entity/models.py
@@ -1,7 +1,7 @@
from django.db import models
from hi.apps.location.models import Location
-from hi.integrations.core.enums import IntegrationType
+from hi.integrations.core.models import IntegrationIdModel
from .enums import (
EntityType,
@@ -11,7 +11,7 @@ from .enums import (
)
-class Entity( models.Model ):
+class Entity( IntegrationIdModel ):
"""
- A physical feature, device or software artifact.
- May have a fixed physical location (or can just be part of a collection)
@@ -36,18 +36,7 @@ class Entity( models.Model ):
'Entity Type',
max_length = 32,
null = False, blank = False,
- )
- integration_type_str = models.CharField(
- 'Integration Type',
- max_length = 32,
- null = True, blank = True,
- )
- integration_id = models.CharField(
- 'Integration Id',
- max_length = 128,
- null = True, blank = True,
- )
-
+ )
created_datetime = models.DateTimeField(
'Created',
auto_now_add = True,
@@ -72,15 +61,6 @@ class Entity( models.Model ):
self.entity_type_str = str(entity_type)
return
- @property
- def integration_type(self):
- return IntegrationType.from_name_safe( self.integration_type_str )
-
- @integration_type.setter
- def integration_type( self, integration_type : IntegrationType ):
- self.integration_type_str = str(integration_type)
- return
-
def get_attribute_map(self):
attribute_map = dict()
for attribute in self.attributes.all():
diff --git a/src/hi/apps/model_helper.py b/src/hi/apps/model_helper.py
index 43164d94..1270098d 100644
--- a/src/hi/apps/model_helper.py
+++ b/src/hi/apps/model_helper.py
@@ -12,6 +12,8 @@ from hi.apps.entity.models import Entity, EntityState
from hi.apps.sense.enums import SensorType
from hi.apps.sense.models import Sensor
+from hi.integrations.core.models import IntegrationId
+
class HiModelHelper:
""" Model creation helpers. """
@@ -19,312 +21,275 @@ class HiModelHelper:
@classmethod
def create_blob_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Blob'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.BLOB ),
- name = name,
- value_range = '',
- units = None,
- )
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
+ entity_state_type = EntityStateType.BLOB,
name = name,
+ integration_id = integration_id,
)
- return sensor
@classmethod
def create_multivalued_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Values'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.MULTVALUED ),
- name = name,
- value_range = '',
- units = None,
- )
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
+ entity_state_type = EntityStateType.MULTVALUED,
name = name,
+ integration_id = integration_id,
)
- return sensor
@classmethod
def create_connectivity_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Connection'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.CONNECTIVITY ),
+ entity_state_type = EntityStateType.CONNECTIVITY,
name = name,
- value_range = '',
- units = None,
+ integration_id = integration_id,
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_datetime_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Date/Time'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.DATETIME ),
- name = name,
- value_range = '',
- units = None,
- )
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
+ entity_state_type = EntityStateType.DATETIME,
name = name,
+ integration_id = integration_id,
)
- return sensor
@classmethod
def create_discrete_sensor( cls,
entity : Entity,
values : List[ str ],
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Value'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.DICRETE ),
+ entity_state_type = EntityStateType.DISCRETE,
name = name,
- value_range = '',
- units = None,
+ integration_id = integration_id,
+ value_range = json.dumps( values ),
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_high_low_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Level'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.HIGH_LOW ),
- name = name,
- value_range = '',
- units = None,
- )
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
+ entity_state_type = EntityStateType.HIGH_LOW,
name = name,
+ integration_id = integration_id,
)
- return sensor
@classmethod
def create_temperature_sensor( cls,
entity : Entity,
temperature_unit : TemperatureUnit,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Temperature'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.TEMPERATURE ),
+ entity_state_type = EntityStateType.TEMPERATURE,
name = name,
- value_range = '',
+ integration_id = integration_id,
units = str(temperature_unit),
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_humidity_sensor( cls,
entity : Entity,
humidity_unit : HumidityUnit,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Humidity'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.HUMIDITY ),
+ entity_state_type = EntityStateType.HUMIDITY,
name = name,
- value_range = '',
+ integration_id = integration_id,
units = str(humidity_unit),
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_on_off_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} On/Off'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.ON_OFF ),
+ entity_state_type = EntityStateType.ON_OFF,
name = name,
- value_range = '',
- units = None,
+ integration_id = integration_id,
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_open_close_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Open/Close'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.OPEN_CLOSE ),
+ entity_state_type = EntityStateType.OPEN_CLOSE,
name = name,
- value_range = '',
- units = None,
+ integration_id = integration_id,
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_movement_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Motion'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.MOVEMENT ),
- name = name,
- value_range = '',
- units = None,
- )
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
+ entity_state_type = EntityStateType.MOVEMENT,
name = name,
+ integration_id = integration_id,
)
- return sensor
@classmethod
def create_video_stream_sensor( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None ):
+ integration_id : IntegrationId = None,
+ name : str = None ):
if not name:
name = f'{entity.name} Stream'
- entity_state = EntityState.objects.create(
+ return cls.create_sensor(
entity = entity,
- entity_state_type_str = str( EntityStateType.VIDEO_STREAM ),
+ entity_state_type = EntityStateType.VIDEO_STREAM,
name = name,
- value_range = '',
- units = None,
+ integration_id = integration_id,
)
- sensor = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- return sensor
@classmethod
def create_on_off_controller( cls,
entity : Entity,
- integration_id : str = None,
- name : str = None,
- is_sensed : bool = True ):
+ integration_id : IntegrationId = None,
+ name : str = None,
+ is_sensed : bool = True ):
if not name:
name = f'{entity.name} Controller'
- entity_state = EntityState.objects.create(
+ return cls.create_controller(
entity = entity,
- entity_state_type_str = str( EntityStateType.ON_OFF ),
- name = name,
- value_range = '',
- units = None,
- )
- if is_sensed:
- _ = Sensor.objects.create(
- entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
- name = name,
- )
- controller = Controller.objects.create(
- entity_state = entity_state,
- controller_type_str = str( ControllerType.DEFAULT ),
+ entity_state_type = EntityStateType.ON_OFF,
name = name,
+ is_sensed = is_sensed,
+ integration_id = integration_id,
)
- return controller
@classmethod
def create_discrete_controller( cls,
entity : Entity,
value_list : List[ str ],
- integration_id : str = None,
- name : str = None,
- is_sensed : bool = True ):
+ integration_id : IntegrationId = None,
+ name : str = None,
+ is_sensed : bool = True ):
if not name:
name = f'{entity.name} Controller'
- entity_state = EntityState.objects.create(
+ return cls.create_controller(
entity = entity,
- entity_state_type_str = str( EntityStateType.DICRETE ),
+ entity_state_type = EntityStateType.DISCRETE,
name = name,
+ is_sensed = is_sensed,
+ integration_id = integration_id,
value_range = json.dumps( value_list ),
- units = None,
)
+
+ @classmethod
+ def create_sensor( cls,
+ entity : Entity,
+ entity_state_type : EntityStateType,
+ name : str = None,
+ sensor_type : SensorType = SensorType.DEFAULT,
+ integration_id : IntegrationId = None,
+ value_range : str = '',
+ units : str = None ):
+ if not name:
+ name = f'{entity.name}'
+
+ entity_state = EntityState.objects.create(
+ entity = entity,
+ entity_state_type_str = str( entity_state_type ),
+ name = name,
+ value_range = value_range,
+ units = units,
+ )
+ sensor = Sensor(
+ entity_state = entity_state,
+ sensor_type_str = str( sensor_type ),
+ name = name,
+ )
+ sensor.integration_id = integration_id
+ sensor.save()
+ return sensor
+
+ @classmethod
+ def create_controller( cls,
+ entity : Entity,
+ entity_state_type : EntityStateType,
+ name : str = None,
+ controller_type : ControllerType = ControllerType.DEFAULT,
+ is_sensed : bool = True,
+ integration_id : IntegrationId = None,
+ value_range : str = '',
+ units : str = None ):
+ if not name:
+ name = f'{entity.name}'
+
+ entity_state = EntityState.objects.create(
+ entity = entity,
+ entity_state_type_str = str( entity_state_type ),
+ name = name,
+ value_range = value_range,
+ units = units,
+ )
+
if is_sensed:
- _ = Sensor.objects.create(
+ sensor = Sensor(
entity_state = entity_state,
- sensor_type_str = str( SensorType.DEFAULT ),
name = name,
+ sensor_type_str = str( SensorType.DEFAULT ),
)
- controller = Controller.objects.create(
+ sensor.integration_id = integration_id
+ sensor.save()
+
+ controller = Controller(
entity_state = entity_state,
- controller_type_str = str( ControllerType.DEFAULT ),
+ controller_type_str = str( controller_type ),
name = name,
)
+ controller.integration_id = integration_id
+ controller.save()
return controller
diff --git a/src/hi/apps/sense/admin.py b/src/hi/apps/sense/admin.py
index 44a87324..0c700c04 100644
--- a/src/hi/apps/sense/admin.py
+++ b/src/hi/apps/sense/admin.py
@@ -13,7 +13,7 @@ class SensorAdmin(admin.ModelAdmin):
'entity_state',
'sensor_type_str',
'integration_type_str',
- 'integration_id',
+ 'integration_key',
)
search_fields = ['name']
diff --git a/src/hi/apps/sense/migrations/0003_rename_integration_id_sensor_integration_key.py b/src/hi/apps/sense/migrations/0003_rename_integration_id_sensor_integration_key.py
new file mode 100644
index 00000000..a505b53c
--- /dev/null
+++ b/src/hi/apps/sense/migrations/0003_rename_integration_id_sensor_integration_key.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.2.15 on 2024-08-25 23:55
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("sense", "0002_sensor_integration_id_sensor_integration_type_str"),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name="sensor",
+ old_name="integration_id",
+ new_name="integration_key",
+ ),
+ ]
diff --git a/src/hi/apps/sense/migrations/0004_alter_sensor_integration_key.py b/src/hi/apps/sense/migrations/0004_alter_sensor_integration_key.py
new file mode 100644
index 00000000..b63663d4
--- /dev/null
+++ b/src/hi/apps/sense/migrations/0004_alter_sensor_integration_key.py
@@ -0,0 +1,20 @@
+# Generated by Django 4.2.15 on 2024-08-25 23:56
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("sense", "0003_rename_integration_id_sensor_integration_key"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="sensor",
+ name="integration_key",
+ field=models.CharField(
+ blank=True, max_length=128, null=True, verbose_name="Integration Key"
+ ),
+ ),
+ ]
diff --git a/src/hi/apps/sense/models.py b/src/hi/apps/sense/models.py
index 7a34b9bf..27a288b6 100644
--- a/src/hi/apps/sense/models.py
+++ b/src/hi/apps/sense/models.py
@@ -1,12 +1,12 @@
from django.db import models
from hi.apps.entity.models import Entity, EntityState
-from hi.integrations.core.enums import IntegrationType
+from hi.integrations.core.models import IntegrationIdModel
from .enums import SensorType
-class Sensor( models.Model ):
+class Sensor( IntegrationIdModel ):
"""
- Represents an observed state of an entity.
- Will sense exactly one EntityState
@@ -28,16 +28,6 @@ class Sensor( models.Model ):
max_length = 32,
null = False, blank = False,
)
- integration_type_str = models.CharField(
- 'Integration Type',
- max_length = 32,
- null = True, blank = True,
- )
- integration_id = models.CharField(
- 'Integration Id',
- max_length = 128,
- null = True, blank = True,
- )
class Meta:
verbose_name = 'Sensor'
@@ -52,15 +42,6 @@ class Sensor( models.Model ):
self.sensor_type_str = str(sensor_type)
return
- @property
- def integration_type(self):
- return IntegrationType.from_name_safe( self.integration_type_str )
-
- @integration_type.setter
- def integration_type( self, integration_type : IntegrationType ):
- self.integration_type_str = str(integration_type)
- return
-
class SensedEntity(models.Model):
"""The Entity associated with a Sensors's state is implicitly a
diff --git a/src/hi/integrations/core/models.py b/src/hi/integrations/core/models.py
index fed9cf29..20b3fd10 100644
--- a/src/hi/integrations/core/models.py
+++ b/src/hi/integrations/core/models.py
@@ -1,3 +1,4 @@
+from dataclasses import dataclass
from typing import Dict
from django.db import models
@@ -103,3 +104,67 @@ class IntegrationProperty(models.Model):
def value_type( self, value_type : PropertyValueType ):
self.value_type_str = str(value_type)
return
+
+
+@dataclass
+class IntegrationId:
+ """ Internal data corresponding to/from IntegrationIdModel """
+
+ integration_type : IntegrationType
+ key : str
+
+ @property
+ def integration_type_str(self):
+ if self.integration_type:
+ return str(self.integration_type)
+ return None
+
+
+class IntegrationIdModel( models.Model ):
+ """
+ For use in DB objects that need to be associated with an integration
+ device/sensor/control.
+ """
+
+ class Meta:
+ abstract = True
+
+ integration_type_str = models.CharField(
+ 'Integration Type',
+ max_length = 32,
+ null = True, blank = True,
+ )
+ integration_key = models.CharField(
+ 'Integration Key',
+ max_length = 128,
+ null = True, blank = True,
+ )
+
+ @property
+ def integration_type(self):
+ return IntegrationType.from_name_safe( self.integration_type_str )
+
+ @integration_type.setter
+ def integration_type( self, integration_type : IntegrationType ):
+ self.integration_type_str = str(integration_type)
+ return
+
+ @property
+ def integration_id(self):
+ return IntegrationId(
+ integration_type = self.integration_type,
+ key = self.integration_key,
+ )
+
+ @integration_id.setter
+ def integration_id( self, integration_id : IntegrationId ):
+ if not integration_id:
+ self.integration_type_str = None
+ self.integration_key = None
+ return
+ self.integration_type_str = integration_id.integration_type_str
+ self.integration_key = integration_id.key
+ return
+
+
+
diff --git a/src/hi/integrations/hass/hass_converter.py b/src/hi/integrations/hass/hass_converter.py
index 6f7ef420..1e610116 100644
--- a/src/hi/integrations/hass/hass_converter.py
+++ b/src/hi/integrations/hass/hass_converter.py
@@ -16,6 +16,7 @@ from hi.apps.entity.models import Entity, Attribute
from hi.apps.model_helper import HiModelHelper
from hi.integrations.core.enums import IntegrationType
+from hi.integrations.core.models import IntegrationId
from .hass_models import HassApi, HassState, HassDevice
@@ -192,17 +193,23 @@ class HassConverter:
@classmethod
def create_models_for_hass_device( cls, hass_device : HassDevice ) -> Entity:
+ integration_id = IntegrationId(
+ integration_type = IntegrationType.HASS,
+ key = hass_device.device_id,
+
+ )
entity_name = cls.hass_device_to_entity_name( hass_device )
entity_type = cls.hass_device_to_entity_type( hass_device )
with transaction.atomic():
- entity = Entity.objects.create(
+ entity = Entity(
name = entity_name,
entity_type_str = str(entity_type),
- integration_type_str = str(IntegrationType.HASS),
- integration_id = str( hass_device.device_id )
)
+ entity.integration_id = integration_id
+ entity.save()
+
insteon_address = cls.hass_device_to_insteon_address( hass_device )
if insteon_address:
Attribute.objects.create(
@@ -224,6 +231,7 @@ class HassConverter:
hass_device = hass_device,
hass_state = hass_state,
entity = entity,
+ integration_id = integration_id,
)
continue
@@ -231,9 +239,10 @@ class HassConverter:
@classmethod
def _create_hass_state_sensor_or_controller( cls,
- hass_device : HassDevice,
- hass_state : HassState,
- entity : Entity ):
+ hass_device : HassDevice,
+ hass_state : HassState,
+ entity : Entity,
+ integration_id : IntegrationId ):
# Observations:
#
# - Some light switches have both a 'switch' and 'light' HAss state.
@@ -251,7 +260,7 @@ class HassConverter:
if hass_state.entity_id_prefix in cls.SWITCH_PREFIXES:
HiModelHelper.create_on_off_controller(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
@@ -263,7 +272,7 @@ class HassConverter:
if hass_state.entity_id_prefix == HassApi.SUN_ID_PREFIX:
HiModelHelper.create_multivalued_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
@@ -271,7 +280,7 @@ class HassConverter:
if hass_state.entity_id_prefix == HassApi.WEATHER_ID_PREFIX:
HiModelHelper.create_multivalued_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
@@ -281,6 +290,7 @@ class HassConverter:
hass_device = hass_device,
hass_state = hass_state,
entity = entity,
+ integration_id = integration_id,
)
return
@@ -292,7 +302,7 @@ class HassConverter:
HiModelHelper.create_temperature_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
temperature_unit = temperature_unit,
)
@@ -308,7 +318,7 @@ class HassConverter:
HiModelHelper.create_humidity_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
humidity_unit = humidity_unit,
)
@@ -317,7 +327,7 @@ class HassConverter:
if device_class == HassApi.TIMESTAMP_DEVICE_CLASS:
HiModelHelper.create_datetime_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
@@ -326,14 +336,14 @@ class HassConverter:
HiModelHelper.create_discrete_sensor(
entity = entity,
values = hass_state.options,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
HiModelHelper.create_blob_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
@@ -342,7 +352,8 @@ class HassConverter:
def _create_hass_state_binary_sensor( cls,
hass_device : HassDevice,
hass_state : HassState,
- entity : Entity ):
+ entity : Entity,
+ integration_id : IntegrationId ):
name = hass_state.friendly_name
device_class = hass_state.device_class
if not name and device_class:
@@ -353,37 +364,37 @@ class HassConverter:
if hass_state.device_class == HassApi.CONNECTIVITY_DEVICE_CLASS:
HiModelHelper.create_connectivity_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
elif hass_state.device_class in HassApi.DOOR_DEVICE_CLASS_SET:
HiModelHelper.create_open_close_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
elif hass_state.device_class == HassApi.MOTION_DEVICE_CLASS:
HiModelHelper.create_movement_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
elif hass_state.device_class == HassApi.LIGHT_DEVICE_CLASS:
HiModelHelper.create_on_off_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
elif hass_state.device_class == HassApi.BATTERY_DEVICE_CLASS:
HiModelHelper.create_high_low_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
else:
HiModelHelper.create_on_off_sensor(
entity = entity,
- integration_id = hass_state.entity_id,
+ integration_id = integration_id,
name = name,
)
return
diff --git a/src/hi/integrations/hass/hass_manager.py b/src/hi/integrations/hass/hass_manager.py
index 45f04be6..149fe70f 100644
--- a/src/hi/integrations/hass/hass_manager.py
+++ b/src/hi/integrations/hass/hass_manager.py
@@ -131,7 +131,8 @@ class HassManager( Singleton ):
entity_queryset = Entity.objects.filter( integration_type_str = str(IntegrationType.HASS) )
for entity in entity_queryset:
- hass_device_id = entity.integration_id
+ integration_id = entity.integration_id
+ hass_device_id = integration_id.key
if not hass_device_id:
result.error_list.append( f'Entity found without valid HAss Id: {entity}' )
hass_device_id = 1000000 + entity.id # We need a (unique) placeholder for removals
diff --git a/src/hi/integrations/zoneminder/zm_manager.py b/src/hi/integrations/zoneminder/zm_manager.py
index ef26ec83..74b57c05 100644
--- a/src/hi/integrations/zoneminder/zm_manager.py
+++ b/src/hi/integrations/zoneminder/zm_manager.py
@@ -8,19 +8,14 @@ from django.db import transaction
from hi.apps.common.database_lock import ExclusionLockContext
from hi.apps.common.processing_result import ProcessingResult
from hi.apps.common.singleton import Singleton
-from hi.apps.entity.enums import (
- AttributeName,
- EntityType,
- AttributeValueType,
- AttributeType,
-)
-from hi.apps.entity.models import Entity, Attribute
+from hi.apps.entity.enums import EntityType
+from hi.apps.entity.models import Entity
from hi.apps.model_helper import HiModelHelper
from hi.integrations.core.enums import IntegrationType
from hi.integrations.core.exceptions import IntegrationPropertyError
-from hi.integrations.core.models import Integration
+from hi.integrations.core.models import Integration, IntegrationId
from .enums import (
ZmPropertyName,
@@ -142,8 +137,9 @@ class ZoneMinderManager( Singleton ):
entity_queryset = Entity.objects.filter( integration_type_str = str(IntegrationType.ZONEMINDER) )
for entity in entity_queryset:
+ integration_id = entity.integration_id
try:
- zm_id = int( entity.integration_id )
+ zm_id = int( integration_id.key )
except ( TypeError, ValueError ):
result.error_list.append( f'ZM entity found without valid ZM Id: {entity}' )
zm_id = 1000000 + entity.id # We need a (unique) placeholder (will remove this later)
@@ -178,23 +174,31 @@ class ZoneMinderManager( Singleton ):
def _create_entity( self,
monitor : ZmMonitor,
result : ProcessingResult ):
-
+
+ integration_id = IntegrationId(
+ integration_type = IntegrationType.ZONEMINDER,
+ key = str( monitor.id() ),
+
+ )
with transaction.atomic():
- entity = Entity.objects.create(
+ entity = Entity(
name = monitor.name(),
entity_type_str = str(EntityType.CAMERA),
- integration_type_str = str(IntegrationType.ZONEMINDER),
- integration_id = str( monitor.id() ),
)
+ entity.integration_id = integration_id
+ entity.save()
HiModelHelper.create_video_stream_sensor(
entity = entity,
+ integration_id = integration_id,
)
HiModelHelper.create_movement_sensor(
entity = entity,
+ integration_id = integration_id,
)
HiModelHelper.create_discrete_controller(
entity = entity,
+ integration_id = integration_id,
name = f'{entity.name} State',
value_list = [ str(x) for x in ZmMonitorState ],
)