81 lines
4.0 KiB
Python
81 lines
4.0 KiB
Python
from django.conf import settings
|
|
from django.db import models
|
|
|
|
|
|
class HomeAssistantConnection(models.Model):
|
|
class Direction(models.TextChoices):
|
|
BOTH = 'both', 'Two-way'
|
|
FROM_HA = 'from_ha', 'Home Assistant to TasksNest'
|
|
TO_HA = 'to_ha', 'TasksNest to Home Assistant'
|
|
|
|
class ConflictWinner(models.TextChoices):
|
|
HA = 'ha', 'Home Assistant'
|
|
TASKSNEST = 'tasksnest', 'TasksNest'
|
|
|
|
class SyncInterval(models.TextChoices):
|
|
MANUAL = 'manual', 'Manual only'
|
|
FIVE = '5', 'Every 5 minutes'
|
|
FIFTEEN = '15', 'Every 15 minutes'
|
|
THIRTY = '30', 'Every 30 minutes'
|
|
|
|
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
|
|
related_name='home_assistant_connection')
|
|
base_url = models.URLField(max_length=500)
|
|
encrypted_token = models.TextField()
|
|
grocery_entity_id = models.CharField(max_length=255, blank=True)
|
|
task_entity_id = models.CharField(max_length=255, blank=True)
|
|
task_initial_sync_confirmed = models.BooleanField(default=False)
|
|
direction = models.CharField(max_length=10, choices=Direction.choices, default=Direction.BOTH)
|
|
conflict_winner = models.CharField(max_length=10, choices=ConflictWinner.choices, default=ConflictWinner.HA)
|
|
auto_sync_enabled = models.BooleanField(default=False)
|
|
sync_interval_minutes = models.CharField(max_length=6, choices=SyncInterval.choices, default=SyncInterval.FIFTEEN)
|
|
initial_sync_confirmed = models.BooleanField(default=False)
|
|
sync_lock_until = models.DateTimeField(null=True, blank=True)
|
|
last_synced_at = models.DateTimeField(null=True, blank=True)
|
|
last_sync_error = models.CharField(max_length=255, blank=True)
|
|
next_sync_at = models.DateTimeField(null=True, blank=True)
|
|
consecutive_sync_failures = models.PositiveSmallIntegerField(default=0)
|
|
entities = models.JSONField(default=list, blank=True)
|
|
version = models.CharField(max_length=100, blank=True)
|
|
last_tested_at = models.DateTimeField(null=True, blank=True)
|
|
last_error = models.CharField(max_length=255, blank=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
def __str__(self):
|
|
return f'Home Assistant connection for user {self.user_id}'
|
|
|
|
|
|
class HomeAssistantSyncRun(models.Model):
|
|
connection = models.ForeignKey(HomeAssistantConnection, on_delete=models.CASCADE, related_name='sync_runs')
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
succeeded = models.BooleanField(default=False)
|
|
automatic = models.BooleanField(default=False)
|
|
changes_applied = models.PositiveSmallIntegerField(default=0)
|
|
summary = models.CharField(max_length=255, blank=True)
|
|
error = models.CharField(max_length=255, blank=True)
|
|
|
|
class Meta:
|
|
ordering = ('-created_at', '-pk')
|
|
|
|
|
|
class HomeAssistantItemMap(models.Model):
|
|
connection = models.ForeignKey(HomeAssistantConnection, on_delete=models.CASCADE, related_name='item_maps')
|
|
entity_id = models.CharField(max_length=255)
|
|
grocery = models.ForeignKey('groceries.GroceryItem', on_delete=models.SET_NULL, null=True, blank=True)
|
|
task = models.ForeignKey('planner.Task', on_delete=models.SET_NULL, null=True, blank=True)
|
|
ha_uid = models.CharField(max_length=255, blank=True)
|
|
last_local_hash = models.CharField(max_length=64, blank=True)
|
|
last_remote_hash = models.CharField(max_length=64, blank=True)
|
|
pending_create = models.BooleanField(default=False)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.UniqueConstraint(fields=['connection', 'entity_id', 'grocery'],
|
|
name='ha_unique_grocery_mapping'),
|
|
models.UniqueConstraint(fields=['connection', 'entity_id', 'task'],
|
|
name='ha_unique_task_mapping'),
|
|
models.UniqueConstraint(fields=['connection', 'entity_id', 'ha_uid'],
|
|
condition=~models.Q(ha_uid=''), name='ha_unique_uid_mapping'),
|
|
]
|