Scope device uniqueness per parent, null type/location for sub-components, hide sub-components from list, flat battery summary for parent devices

This commit is contained in:
2026-06-09 19:26:31 -05:00
parent 2a54cd8297
commit 23eeeafff7
7 changed files with 340 additions and 42 deletions
+17 -2
View File
@@ -1,6 +1,6 @@
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, ForeignKey, Table
from sqlalchemy import Column, Integer, String, Text, ForeignKey, Table, UniqueConstraint
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
@@ -25,7 +25,7 @@ class Device(Base):
__tablename__ = "device"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(100), nullable=False, unique=True)
name = Column(String(100), nullable=False)
battery_slots = Column(Integer, nullable=False, default=1)
device_type = Column(String(50), nullable=True)
battery_size = Column(String(20), nullable=True) # AA, AAA, 9V, CR2032 …; null for parent-only devices
@@ -33,6 +33,11 @@ class Device(Base):
notes = Column(Text, nullable=True)
ha_entity_id = Column(String(100), nullable=True) # e.g. "sensor.tv_remote_battery"
parent_id = Column(Integer, ForeignKey("device.id", ondelete="SET NULL"), nullable=True)
parent_key = Column(Integer, nullable=False, default=-1) # -1 for top-level, parent_id for sub-components
__table_args__ = (
UniqueConstraint("parent_key", "name", name="uq_device_parent_key_name"),
)
batteries = relationship("Battery", back_populates="device")
logbook_entries = relationship(
@@ -71,6 +76,16 @@ class Device(Base):
def is_subcomponent(self):
return self.parent_id is not None
def effective_installed_count(self):
if self.has_children():
return sum(c.installed_count() for c in self.children)
return self.installed_count()
def effective_slots(self):
if self.has_children():
return sum(c.battery_slots for c in self.children)
return self.battery_slots
def __repr__(self):
return f"<Device {self.name}>"