Validate battery percentage range on manual edit; clamp HA values to 0-100

This commit is contained in:
2026-06-10 12:31:11 -05:00
parent a9835fee1e
commit 17bad3f36b
4 changed files with 50 additions and 0 deletions
+4
View File
@@ -293,6 +293,9 @@ def create_app(config_object="config"):
battery.purchase_date = _parse_date(purchase_raw) if purchase_raw else None
battery.storage_location = f.get("storage_location", "").strip() or None
new_pct = _int("battery_percentage")
if new_pct is not None and not (0 <= new_pct <= 100):
flash("Battery percentage must be between 0 and 100.", "error")
return redirect(url_for("battery_detail", battery_id=battery_id))
if new_pct != battery.battery_percentage:
battery.battery_percentage = new_pct
if new_pct is not None:
@@ -803,6 +806,7 @@ def create_app(config_object="config"):
if ha_client.enabled and device.ha_entity_id:
ha_live_pct = ha_client.get_state(device.ha_entity_id, timeout=1)
if ha_live_pct is not None:
ha_live_pct = max(0, min(100, ha_live_pct))
changed = False
for battery in device.installed_batteries():
if battery.battery_percentage != ha_live_pct: