Track battery percentage history; skip poll write when value unchanged

This commit is contained in:
2026-04-13 21:15:19 -05:00
parent 279a1f3f3e
commit d7ba64a2f3
6 changed files with 230 additions and 7 deletions
+22
View File
@@ -1,3 +1,5 @@
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, ForeignKey
from sqlalchemy.orm import declarative_base, relationship
@@ -61,6 +63,11 @@ class Battery(Base):
order_by="ChargeLog.charged_date",
cascade="all, delete-orphan",
)
pct_logs = relationship(
"BatteryPctLog", back_populates="battery",
order_by="BatteryPctLog.recorded_at.desc()",
cascade="all, delete-orphan",
)
def is_available(self):
return self.status == "available"
@@ -103,3 +110,18 @@ class ChargeLog(Base):
def __repr__(self):
return f"<ChargeLog {self.battery_id} {self.charged_date}>"
class BatteryPctLog(Base):
__tablename__ = "battery_pct_log"
id = Column(Integer, primary_key=True, autoincrement=True)
battery_id = Column(Integer, ForeignKey("battery.id", ondelete="CASCADE"), nullable=False)
percentage = Column(Integer, nullable=False)
recorded_at = Column(String(19), nullable=False) # "YYYY-MM-DD HH:MM:SS"
source = Column(String(10), nullable=True) # 'poll', 'manual', 'charge'
battery = relationship("Battery", back_populates="pct_logs")
def __repr__(self):
return f"<BatteryPctLog {self.battery_id} {self.recorded_at} {self.percentage}%>"