Track battery percentage history; skip poll write when value unchanged
This commit is contained in:
+9
-2
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import threading
|
||||
from datetime import datetime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -36,7 +37,7 @@ class HaPoller:
|
||||
self._poll_once()
|
||||
|
||||
def _poll_once(self):
|
||||
from models import Battery, Device # local import avoids circular-import risk
|
||||
from models import Battery, BatteryPctLog, Device # local import avoids circular-import risk
|
||||
|
||||
session = self._Session()
|
||||
try:
|
||||
@@ -49,8 +50,14 @@ class HaPoller:
|
||||
pct = self._client.get_state(device.ha_entity_id)
|
||||
if pct is not None:
|
||||
for battery in device.batteries:
|
||||
if battery.status == "installed":
|
||||
if battery.status == "installed" and battery.battery_percentage != pct:
|
||||
battery.battery_percentage = pct
|
||||
session.add(BatteryPctLog(
|
||||
battery_id=battery.id,
|
||||
percentage=pct,
|
||||
recorded_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
source="poll",
|
||||
))
|
||||
session.commit()
|
||||
logger.debug("HA poll complete (%d devices checked)", len(devices))
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user