Skip to content

Home Information - Your home, your data, organized.

Python

Improve Entity deletion during integration sync operation (Issue #56) (#58)

Tony Cc466681a50d4
diff --git a/src/hi/integrations/sync_mixins.py b/src/hi/integrations/sync_mixins.py
new file mode 100644
index 00000000..ef88102d
--- /dev/null
+++ b/src/hi/integrations/sync_mixins.py
@@ -0,0 +1,119 @@
+"""
+Mixins for integration synchronization operations.
+
+This module provides common functionality for integration sync operations,
+particularly around intelligent entity deletion that preserves user data.
+"""
+
+import logging
+
+from django.db import transaction
+
+from hi.apps.common.processing_result import ProcessingResult
+from hi.apps.entity.models import Entity, EntityState
+from hi.apps.sense.models import Sensor
+from hi.apps.control.models import Controller
+
+from .user_data_detector import EntityUserDataDetector
+
+logger = logging.getLogger(__name__)
+
+
+class IntegrationSyncMixin:
+ """
+ Mixin providing common functionality for integration synchronization operations.
+
+ This mixin provides intelligent entity deletion that preserves user-created
+ data while removing integration-specific components.
+ """
+
+ def _remove_entity_intelligently(self,
+ entity: Entity,
+ result: ProcessingResult,
+ integration_name: str):
+ """
+ Remove an entity that no longer exists in the integration.
+
+ If the entity has user-created attributes, preserve the entity but disconnect
+ it from the integration and remove only integration-related components.
+ Otherwise, perform complete deletion.
+
+ Args:
+ entity: The Entity to remove or preserve
+ result: ProcessingResult to update with messages
+ integration_name: Name of the integration (for logging)
+ """
+ if EntityUserDataDetector.has_user_created_attributes(entity):
+ self._preserve_entity_with_user_data(entity, result, integration_name)
+ else:
+ # No user data, safe to delete completely
+ entity.delete() # Deletion cascades to all related data
+ result.message_list.append(f'Removed stale {integration_name} entity: {entity}')
+
+ def _preserve_entity_with_user_data(self,
+ entity: Entity,
+ result: ProcessingResult,
+ integration_name: str):
+ """
+ Preserve an entity with user-created data by disconnecting it from integration
+ and removing only integration-related components.
+
+ Args:
+ entity: The Entity to preserve
+ result: ProcessingResult to update with messages
+ integration_name: Name of the integration (for logging)
+ """
+ original_name = entity.name
+
+ # Get integration-related components to remove
+ sensor_ids_to_remove = EntityUserDataDetector.get_integration_related_sensors(entity)
+ controller_ids_to_remove = EntityUserDataDetector.get_integration_related_controllers(entity)
+
+ # Get entity states that will become orphaned
+ orphaned_state_ids = EntityUserDataDetector.get_orphaned_entity_states(
+ entity, sensor_ids_to_remove, controller_ids_to_remove
+ )
+
+ with transaction.atomic():
+ # Remove integration-related sensors
+ if sensor_ids_to_remove:
+ removed_sensor_count = Sensor.objects.filter(
+ id__in=sensor_ids_to_remove
+ ).delete()[0]
+ logger.debug(f'Removed {removed_sensor_count} integration sensors for {entity}')
+
+ # Remove integration-related controllers
+ if controller_ids_to_remove:
+ removed_controller_count = Controller.objects.filter(
+ id__in=controller_ids_to_remove
+ ).delete()[0]
+ logger.debug(f'Removed {removed_controller_count} integration controllers for {entity}')
+
+ # Remove orphaned entity states
+ if orphaned_state_ids:
+ removed_state_count = EntityState.objects.filter(
+ id__in=orphaned_state_ids
+ ).delete()[0]
+ logger.debug(f'Removed {removed_state_count} orphaned entity states for {entity}')
+
+ # Remove integration-related attributes (keep user-created ones)
+ removed_attr_count = entity.attributes.filter(
+ integration_key_str__isnull=False
+ ).delete()[0]
+ if removed_attr_count:
+ logger.debug(f'Removed {removed_attr_count} integration attributes for {entity}')
+
+ # Disconnect entity from integration
+ entity.integration_id = None
+ entity.integration_name = None
+
+ # Update name to indicate disconnected status
+ if not entity.name.startswith('[Disconnected]'):
+ entity.name = f'[Disconnected] {entity.name}'
+
+ entity.save()
+
+ result.message_list.append(
+ f'Preserved {integration_name} entity "{original_name}" with user data, '
+ f'disconnected from integration and renamed to "{entity.name}"'
+ )
diff --git a/src/hi/integrations/tests/__init__.py b/src/hi/integrations/tests/__init__.py
new file mode 100644
index 00000000..0ca287e9
--- /dev/null
+++ b/src/hi/integrations/tests/__init__.py
@@ -0,0 +1 @@
+# Integration tests
diff --git a/src/hi/integrations/tests/test_sync_mixins.py b/src/hi/integrations/tests/test_sync_mixins.py
new file mode 100644
index 00000000..97901c08
--- /dev/null
+++ b/src/hi/integrations/tests/test_sync_mixins.py
@@ -0,0 +1,289 @@
+"""
+Unit tests for IntegrationSyncMixin.
+"""
+
+from django.test import TestCase
+
+from hi.apps.entity.models import Entity, EntityAttribute, EntityState
+from hi.apps.sense.models import Sensor
+from hi.apps.control.models import Controller
+from hi.apps.common.processing_result import ProcessingResult
+
+from hi.integrations.sync_mixins import IntegrationSyncMixin
+
+
+class TestSynchronizer(IntegrationSyncMixin):
+ """Test class that uses the IntegrationSyncMixin."""
+ pass
+
+
+class IntegrationSyncMixinTestCase(TestCase):
+ """Test cases for IntegrationSyncMixin functionality."""
+
+ def setUp(self):
+ """Set up test data."""
+ self.synchronizer = TestSynchronizer()
+ self.result = ProcessingResult(title='Test Sync Result')
+
+ # Create a test entity
+ self.entity = Entity.objects.create(
+ name='Test Entity',
+ entity_type_str='LIGHT',
+ integration_id='test_integration',
+ integration_name='test_device_1'
+ )
+
+ # Create entity state
+ self.entity_state = EntityState.objects.create(
+ entity=self.entity,
+ entity_state_type_str='DISCRETE',
+ name='Test State'
+ )
+
+ def test_remove_entity_intelligently_no_user_data(self):
+ """Test complete deletion when entity has no user data."""
+ # Create only integration data
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='Integration Data',
+ value='Integration-specific data',
+ value_type_str='TEXT',
+ attribute_type_str='CONFIGURATION',
+ integration_key_str='test_integration:test_device_1'
+ )
+
+ Sensor.objects.create(
+ name='Integration Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ entity_id = self.entity.id
+
+ # Call the method
+ self.synchronizer._remove_entity_intelligently(
+ self.entity, self.result, 'TestIntegration'
+ )
+
+ # Entity should be completely deleted
+ self.assertFalse(Entity.objects.filter(id=entity_id).exists())
+
+ # Check result message
+ self.assertEqual(len(self.result.message_list), 1)
+ self.assertIn('Removed stale TestIntegration entity', self.result.message_list[0])
+
+ def test_remove_entity_intelligently_with_user_data(self):
+ """Test preservation when entity has user data."""
+ # Create user-created attribute
+ user_attr = EntityAttribute.objects.create(
+ entity=self.entity,
+ name='User Note',
+ value='This is a user note',
+ value_type_str='TEXT',
+ attribute_type_str='DOCUMENTATION'
+ # integration_key_str is None (user-created)
+ )
+
+ # Create integration attribute
+ integration_attr = EntityAttribute.objects.create(
+ entity=self.entity,
+ name='Integration Data',
+ value='Integration data',
+ value_type_str='TEXT',
+ attribute_type_str='CONFIGURATION',
+ integration_key_str='test_integration:test_device_1'
+ )
+
+ # Create integration sensor
+ integration_sensor = Sensor.objects.create(
+ name='Integration Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ # Create integration controller
+ integration_controller = Controller.objects.create(
+ name='Integration Controller',
+ entity_state=self.entity_state,
+ controller_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='controller_1'
+ )
+
+ original_name = self.entity.name
+ entity_id = self.entity.id
+ state_id = self.entity_state.id
+
+ # Call the method
+ self.synchronizer._remove_entity_intelligently(
+ self.entity, self.result, 'TestIntegration'
+ )
+
+ # Entity should still exist but be disconnected
+ self.assertTrue(Entity.objects.filter(id=entity_id).exists())
+
+ # Reload entity
+ self.entity.refresh_from_db()
+
+ # Check entity is disconnected
+ self.assertIsNone(self.entity.integration_id)
+ self.assertIsNone(self.entity.integration_name)
+ self.assertEqual(self.entity.name, f'[Disconnected] {original_name}')
+
+ # User attribute should still exist
+ self.assertTrue(EntityAttribute.objects.filter(id=user_attr.id).exists())
+
+ # Integration attribute should be deleted
+ self.assertFalse(EntityAttribute.objects.filter(id=integration_attr.id).exists())
+
+ # Integration sensor should be deleted
+ self.assertFalse(Sensor.objects.filter(id=integration_sensor.id).exists())
+
+ # Integration controller should be deleted
+ self.assertFalse(Controller.objects.filter(id=integration_controller.id).exists())
+
+ # Entity state should be deleted (orphaned)
+ self.assertFalse(EntityState.objects.filter(id=state_id).exists())
+
+ # Check result message
+ self.assertEqual(len(self.result.message_list), 1)
+ self.assertIn('Preserved TestIntegration entity', self.result.message_list[0])
+ self.assertIn('disconnected from integration', self.result.message_list[0])
+
+ def test_preserve_entity_state_with_remaining_user_components(self):
+ """Test that entity states are preserved when user components remain."""
+ # Create user-created attribute
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='User Note',
+ value='User note',
+ value_type_str='TEXT',
+ attribute_type_str='DOCUMENTATION'
+ )
+
+ # Create integration sensor
+ integration_sensor = Sensor.objects.create(
+ name='Integration Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ # Create user sensor (should preserve the state)
+ user_sensor = Sensor.objects.create(
+ name='User Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE'
+ )
+
+ state_id = self.entity_state.id
+ user_sensor_id = user_sensor.id
+
+ # Call the method
+ self.synchronizer._remove_entity_intelligently(
+ self.entity, self.result, 'TestIntegration'
+ )
+
+ # Entity state should still exist (has user sensor)
+ self.assertTrue(EntityState.objects.filter(id=state_id).exists())
+
+ # User sensor should still exist
+ self.assertTrue(Sensor.objects.filter(id=user_sensor_id).exists())
+
+ # Integration sensor should be deleted
+ self.assertFalse(Sensor.objects.filter(id=integration_sensor.id).exists())
+
+ def test_disconnected_name_not_duplicated(self):
+ """Test that [Disconnected] prefix is not added if already present."""
+ # Set entity name to already have disconnected prefix
+ self.entity.name = '[Disconnected] Already Disconnected'
+ self.entity.save()
+
+ # Create user data
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='User Note',
+ value='User note',
+ value_type_str='TEXT',
+ attribute_type_str='DOCUMENTATION'
+ )
+
+ # Call the method
+ self.synchronizer._remove_entity_intelligently(
+ self.entity, self.result, 'TestIntegration'
+ )
+
+ # Reload entity
+ self.entity.refresh_from_db()
+
+ # Name should not have duplicate prefix
+ self.assertEqual(self.entity.name, '[Disconnected] Already Disconnected')
+ self.assertNotIn('[Disconnected] [Disconnected]', self.entity.name)
+
+ def test_multiple_entity_states_mixed_preservation(self):
+ """Test handling of multiple entity states with different preservation needs."""
+ # Create user data to trigger preservation
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='User Note',
+ value='User note',
+ value_type_str='TEXT',
+ attribute_type_str='DOCUMENTATION'
+ )
+
+ # Create second entity state
+ entity_state2 = EntityState.objects.create(
+ entity=self.entity,
+ entity_state_type_str='CONTINUOUS',
+ name='Test State 2'
+ )
+
+ # First state: only integration components (will be orphaned)
+ integration_sensor1 = Sensor.objects.create(
+ name='Integration Sensor 1',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ # Second state: mixed components (will be preserved)
+ integration_sensor2 = Sensor.objects.create(
+ name='Integration Sensor 2',
+ entity_state=entity_state2,
+ sensor_type_str='CONTINUOUS',
+ integration_id='test_integration',
+ integration_name='sensor_2'
+ )
+
+ user_sensor2 = Sensor.objects.create(
+ name='User Sensor 2',
+ entity_state=entity_state2,
+ sensor_type_str='CONTINUOUS'
+ )
+
+ state1_id = self.entity_state.id
+ state2_id = entity_state2.id
+
+ # Call the method
+ self.synchronizer._remove_entity_intelligently(
+ self.entity, self.result, 'TestIntegration'
+ )
+
+ # First state should be deleted (orphaned)
+ self.assertFalse(EntityState.objects.filter(id=state1_id).exists())
+
+ # Second state should be preserved (has user sensor)
+ self.assertTrue(EntityState.objects.filter(id=state2_id).exists())
+
+ # Integration sensors should be deleted
+ self.assertFalse(Sensor.objects.filter(id=integration_sensor1.id).exists())
+ self.assertFalse(Sensor.objects.filter(id=integration_sensor2.id).exists())
+
+ # User sensor should be preserved
+ self.assertTrue(Sensor.objects.filter(id=user_sensor2.id).exists())
diff --git a/src/hi/integrations/tests/test_user_data_detector.py b/src/hi/integrations/tests/test_user_data_detector.py
new file mode 100644
index 00000000..cbacee6f
--- /dev/null
+++ b/src/hi/integrations/tests/test_user_data_detector.py
@@ -0,0 +1,244 @@
+"""
+Unit tests for EntityUserDataDetector utility.
+"""
+
+from django.test import TestCase
+
+from hi.apps.entity.models import Entity, EntityAttribute, EntityState
+from hi.apps.sense.models import Sensor
+from hi.apps.control.models import Controller
+
+from hi.integrations.user_data_detector import EntityUserDataDetector
+
+
+class EntityUserDataDetectorTestCase(TestCase):
+ """Test cases for EntityUserDataDetector functionality."""
+
+ def setUp(self):
+ """Set up test data."""
+ # Create a test entity
+ self.entity = Entity.objects.create(
+ name='Test Entity',
+ entity_type_str='LIGHT',
+ integration_id='test_integration',
+ integration_name='test_device_1'
+ )
+
+ # Create entity state for testing sensors/controllers
+ self.entity_state = EntityState.objects.create(
+ entity=self.entity,
+ entity_state_type_str='DISCRETE',
+ name='Test State'
+ )
+
+ def test_has_user_created_attributes_with_user_data(self):
+ """Test detection of user-created attributes."""
+ # Create a user-created attribute (no integration_key_str)
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='User Note',
+ value='This is a user note',
+ value_type_str='TEXT',
+ attribute_type_str='DOCUMENTATION'
+ # integration_key_str is None (user-created)
+ )
+
+ result = EntityUserDataDetector.has_user_created_attributes(self.entity)
+ self.assertTrue(result)
+
+ def test_has_user_created_attributes_with_integration_data_only(self):
+ """Test that integration-created attributes don't trigger preservation."""
+ # Create an integration-created attribute
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='Integration Data',
+ value='Integration-specific data',
+ value_type_str='TEXT',
+ attribute_type_str='CONFIGURATION',
+ integration_key_str='test_integration:test_device_1'
+ )
+
+ result = EntityUserDataDetector.has_user_created_attributes(self.entity)
+ self.assertFalse(result)
+
+ def test_has_user_created_attributes_no_attributes(self):
+ """Test entity with no attributes."""
+ result = EntityUserDataDetector.has_user_created_attributes(self.entity)
+ self.assertFalse(result)
+
+ def test_has_user_created_attributes_mixed_attributes(self):
+ """Test entity with both user and integration attributes."""
+ # Create both types
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='User Note',
+ value='User data',
+ value_type_str='TEXT',
+ attribute_type_str='DOCUMENTATION'
+ # integration_key_str is None (user-created)
+ )
+ EntityAttribute.objects.create(
+ entity=self.entity,
+ name='Integration Data',
+ value='Integration data',
+ value_type_str='TEXT',
+ attribute_type_str='CONFIGURATION',
+ integration_key_str='test_integration:test_device_1'
+ )
+
+ result = EntityUserDataDetector.has_user_created_attributes(self.entity)
+ self.assertTrue(result) # Should preserve due to user attribute
+
+ def test_get_integration_related_sensors(self):
+ """Test identification of integration-related sensors."""
+ # Create integration sensor
+ integration_sensor = Sensor.objects.create(
+ name='Integration Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ # Create user sensor (no integration)
+ user_sensor = Sensor.objects.create(
+ name='User Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE'
+ # integration_id is None
+ )
+
+ sensor_ids = EntityUserDataDetector.get_integration_related_sensors(self.entity)
+
+ self.assertEqual(len(sensor_ids), 1)
+ self.assertIn(integration_sensor.id, sensor_ids)
+ self.assertNotIn(user_sensor.id, sensor_ids)
+
+ def test_get_integration_related_controllers(self):
+ """Test identification of integration-related controllers."""
+ # Create integration controller
+ integration_controller = Controller.objects.create(
+ name='Integration Controller',
+ entity_state=self.entity_state,
+ controller_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='controller_1'
+ )
+
+ # Create user controller (no integration)
+ user_controller = Controller.objects.create(
+ name='User Controller',
+ entity_state=self.entity_state,
+ controller_type_str='DISCRETE'
+ # integration_id is None
+ )
+
+ controller_ids = EntityUserDataDetector.get_integration_related_controllers(self.entity)
+
+ self.assertEqual(len(controller_ids), 1)
+ self.assertIn(integration_controller.id, controller_ids)
+ self.assertNotIn(user_controller.id, controller_ids)
+
+ def test_get_orphaned_entity_states_all_integration(self):
+ """Test detection of entity states that become orphaned when all sensors/controllers are integration-related."""
+ # Create integration sensor and controller
+ integration_sensor = Sensor.objects.create(
+ name='Integration Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ integration_controller = Controller.objects.create(
+ name='Integration Controller',
+ entity_state=self.entity_state,
+ controller_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='controller_1'
+ )
+
+ # Get IDs to remove
+ sensor_ids = {integration_sensor.id}
+ controller_ids = {integration_controller.id}
+
+ orphaned_ids = EntityUserDataDetector.get_orphaned_entity_states(
+ self.entity, sensor_ids, controller_ids
+ )
+
+ self.assertEqual(len(orphaned_ids), 1)
+ self.assertIn(self.entity_state.id, orphaned_ids)
+
+ def test_get_orphaned_entity_states_with_remaining_user_components(self):
+ """Test that entity states with remaining user sensors/controllers are not orphaned."""
+ # Create integration sensor
+ integration_sensor = Sensor.objects.create(
+ name='Integration Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ # Create user sensor (should keep the state)
+ Sensor.objects.create(
+ name='User Sensor',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE'
+ # integration_id is None
+ )
+
+ # Only remove integration sensor
+ sensor_ids = {integration_sensor.id}
+ controller_ids = set()
+
+ orphaned_ids = EntityUserDataDetector.get_orphaned_entity_states(
+ self.entity, sensor_ids, controller_ids
+ )
+
+ self.assertEqual(len(orphaned_ids), 0) # State should not be orphaned
+
+ def test_multiple_entity_states(self):
+ """Test handling multiple entity states with different orphan status."""
+ # Create second entity state
+ entity_state2 = EntityState.objects.create(
+ entity=self.entity,
+ entity_state_type_str='CONTINUOUS',
+ name='Test State 2'
+ )
+
+ # First state: only integration components (will be orphaned)
+ integration_sensor1 = Sensor.objects.create(
+ name='Integration Sensor 1',
+ entity_state=self.entity_state,
+ sensor_type_str='DISCRETE',
+ integration_id='test_integration',
+ integration_name='sensor_1'
+ )
+
+ # Second state: mixed components (will not be orphaned)
+ integration_sensor2 = Sensor.objects.create(
+ name='Integration Sensor 2',
+ entity_state=entity_state2,
+ sensor_type_str='CONTINUOUS',
+ integration_id='test_integration',
+ integration_name='sensor_2'
+ )
+
+ Sensor.objects.create(
+ name='User Sensor 2',
+ entity_state=entity_state2,
+ sensor_type_str='CONTINUOUS'
+ )
+
+ # Remove all integration sensors
+ sensor_ids = {integration_sensor1.id, integration_sensor2.id}
+ controller_ids = set()
+
+ orphaned_ids = EntityUserDataDetector.get_orphaned_entity_states(
+ self.entity, sensor_ids, controller_ids
+ )
+
+ self.assertEqual(len(orphaned_ids), 1)
+ self.assertIn(self.entity_state.id, orphaned_ids)
+ self.assertNotIn(entity_state2.id, orphaned_ids)
diff --git a/src/hi/integrations/user_data_detector.py b/src/hi/integrations/user_data_detector.py
new file mode 100644
index 00000000..00168761
--- /dev/null
+++ b/src/hi/integrations/user_data_detector.py
@@ -0,0 +1,128 @@
+"""
+Utility for detecting user-created data associated with entities.
+
+This module provides functionality to determine whether an entity has
+user-created data that should be preserved during integration sync operations.
+All methods are purely analytical and perform no database operations.
+"""
+
+import logging
+from typing import Set
+
+from hi.apps.entity.models import Entity
+
+logger = logging.getLogger(__name__)
+
+
+class EntityUserDataDetector:
+ """
+ Detects user-created data associated with entities to determine
+ whether an entity should be preserved during integration deletion.
+
+ This class provides analysis methods only - no database operations
+ are performed. The calling code is responsible for acting on the
+ information provided by these methods.
+ """
+
+ @staticmethod
+ def has_user_created_attributes(entity: Entity) -> bool:
+ """
+ Check if an entity has user-created attributes that justify preservation.
+
+ This is the primary decision criteria for whether to preserve an entity
+ during integration sync deletion. Only user-created attributes (those
+ without integration_key_str) are considered significant enough to preserve
+ the entire entity.
+
+ Args:
+ entity: The Entity to check for user-created attributes
+
+ Returns:
+ True if the entity has user-created attributes, False otherwise
+ """
+ user_attributes = entity.attributes.filter(
+ integration_key_str__isnull=True
+ ).exists()
+
+ if user_attributes:
+ logger.debug(f'Entity {entity} has user-created attributes - should be preserved')
+ return True
+
+ logger.debug(f'Entity {entity} has no user-created attributes - can be deleted')
+ return False
+
+ @staticmethod
+ def get_integration_related_sensors(entity: Entity) -> Set[int]:
+ """
+ Get sensor IDs that are integration-related for this entity.
+
+ Args:
+ entity: The Entity to check
+
+ Returns:
+ Set of sensor IDs that have integration keys and should be removed
+ """
+ sensor_ids = set()
+ for state in entity.states.all():
+ integration_sensors = state.sensors.filter(
+ integration_id__isnull=False
+ )
+ sensor_ids.update(integration_sensors.values_list('id', flat=True))
+ return sensor_ids
+
+ @staticmethod
+ def get_integration_related_controllers(entity: Entity) -> Set[int]:
+ """
+ Get controller IDs that are integration-related for this entity.
+
+ Args:
+ entity: The Entity to check
+
+ Returns:
+ Set of controller IDs that have integration keys and should be removed
+ """
+ controller_ids = set()
+ for state in entity.states.all():
+ integration_controllers = state.controllers.filter(
+ integration_id__isnull=False
+ )
+ controller_ids.update(integration_controllers.values_list('id', flat=True))
+ return controller_ids
+
+ @staticmethod
+ def get_orphaned_entity_states(entity: Entity,
+ removed_sensor_ids: Set[int],
+ removed_controller_ids: Set[int]) -> Set[int]:
+ """
+ Get entity state IDs that would become orphaned after removing integration components.
+
+ Args:
+ entity: The Entity to check
+ removed_sensor_ids: Set of sensor IDs being removed
+ removed_controller_ids: Set of controller IDs being removed
+
+ Returns:
+ Set of entity state IDs that would have no remaining sensors or controllers
+ and should be removed
+ """
+ orphaned_state_ids = set()
+
+ for state in entity.states.all():
+ # Check if state will have any remaining sensors
+ remaining_sensors = state.sensors.exclude(
+ id__in=removed_sensor_ids
+ ).exists()
+
+ # Check if state will have any remaining controllers
+ remaining_controllers = state.controllers.exclude(
+ id__in=removed_controller_ids
+ ).exists()
+
+ # If no remaining sensors or controllers, this state can be deleted
+ if not remaining_sensors and not remaining_controllers:
+ orphaned_state_ids.add(state.id)
+ logger.debug(f'EntityState {state} will be orphaned and should be removed')
+ else:
+ logger.debug(f'EntityState {state} will retain sensors/controllers, should keep')
+
+ return orphaned_state_ids
diff --git a/src/hi/services/hass/hass_sync.py b/src/hi/services/hass/hass_sync.py
index 9f9f78f1..eced020c 100644
--- a/src/hi/services/hass/hass_sync.py
+++ b/src/hi/services/hass/hass_sync.py
@@ -8,6 +8,7 @@ from hi.apps.common.processing_result import ProcessingResult
from hi.apps.entity.models import Entity
from hi.integrations.transient_models import IntegrationKey
+from hi.integrations.sync_mixins import IntegrationSyncMixin
from .hass_converter import HassConverter
from .hass_models import HassDevice
@@ -17,7 +18,7 @@ from .hass_metadata import HassMetaData
logger = logging.getLogger(__name__)
-class HassSynchronizer( HassMixin ):
+class HassSynchronizer( HassMixin, IntegrationSyncMixin ):
SYNCHRONIZATION_LOCK_NAME = 'hass_integration_sync'
@@ -130,8 +131,12 @@ class HassSynchronizer( HassMixin ):
def _remove_entity( self,
entity : Entity,
result : ProcessingResult ):
- entity.delete() # Deletion cascades to attributes, positions, sensors, controllers, etc.
- result.message_list.append( f'Removed stale HAss entity: {entity}' )
+ """
+ Remove an entity that no longer exists in the HASS integration.
+
+ Uses intelligent deletion that preserves user-created data.
+ """
+ self._remove_entity_intelligently(entity, result, 'HASS')
return
diff --git a/src/hi/services/zoneminder/zm_sync.py b/src/hi/services/zoneminder/zm_sync.py
index 835f90f7..f5141075 100644
--- a/src/hi/services/zoneminder/zm_sync.py
+++ b/src/hi/services/zoneminder/zm_sync.py
@@ -13,6 +13,7 @@ from hi.apps.sense.models import Sensor
from hi.apps.model_helper import HiModelHelper
from hi.integrations.transient_models import IntegrationKey
+from hi.integrations.sync_mixins import IntegrationSyncMixin
from .zm_metadata import ZmMetaData
from .zm_mixins import ZoneMinderMixin
@@ -20,7 +21,7 @@ from .zm_mixins import ZoneMinderMixin
logger = logging.getLogger(__name__)
-class ZoneMinderSynchronizer( ZoneMinderMixin ):
+class ZoneMinderSynchronizer( ZoneMinderMixin, IntegrationSyncMixin ):
SYNCHRONIZATION_LOCK_NAME = 'zm_integration_sync'
@@ -256,9 +257,12 @@ class ZoneMinderSynchronizer( ZoneMinderMixin ):
def _remove_entity( self,
entity : Entity,
result : ProcessingResult ):
-
- # TODO: Should we remove the EventDefinitions that were auto-created (with integration key)?
+ """
+ Remove an entity that no longer exists in the ZoneMinder integration.
+
+ Uses intelligent deletion that preserves user-created data.
- entity.delete() # Deletion cascades to attributes, positions, sensors, controllers, etc.
- result.message_list.append( f'Removed stale ZM entity: {entity}' )
+ TODO: Should we remove the EventDefinitions that were auto-created (with integration key)?
+ """
+ self._remove_entity_intelligently(entity, result, 'ZoneMinder')
return