Add optional Home Assistant integration for battery percentage tracking
This commit is contained in:
@@ -21,6 +21,25 @@ def create_app(config_object="config"):
|
||||
def remove_session(exc=None):
|
||||
db.remove()
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Home Assistant integration (optional)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
from ha_client import HomeAssistantClient
|
||||
from ha_poller import HaPoller
|
||||
|
||||
ha_client = HomeAssistantClient(
|
||||
url=app.config.get("HOMEASSISTANT_URL"),
|
||||
api_key=app.config.get("HOMEASSISTANT_API_KEY"),
|
||||
)
|
||||
if ha_client.enabled:
|
||||
poller = HaPoller(
|
||||
ha_client=ha_client,
|
||||
session_factory=sessionmaker(bind=engine),
|
||||
interval=app.config.get("HOMEASSISTANT_POLL_INTERVAL", 300),
|
||||
)
|
||||
poller.start()
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Dashboard
|
||||
# ------------------------------------------------------------------ #
|
||||
@@ -42,7 +61,8 @@ def create_app(config_object="config"):
|
||||
devices_with_slots = [d for d in devices if d.installed_count() < d.battery_slots]
|
||||
return render_template("dashboard.html", batteries=batteries,
|
||||
storage_locations=storage_locations, devices=devices,
|
||||
devices_with_slots=devices_with_slots)
|
||||
devices_with_slots=devices_with_slots,
|
||||
ha_enabled=ha_client.enabled)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Battery — add
|
||||
@@ -156,6 +176,7 @@ def create_app(config_object="config"):
|
||||
battery.charge_cycles = _int("charge_cycles")
|
||||
battery.purchase_date = f.get("purchase_date", "").strip() or None
|
||||
battery.storage_location = f.get("storage_location", "").strip() or None
|
||||
battery.battery_percentage = _int("battery_percentage")
|
||||
|
||||
db.commit()
|
||||
flash("Details updated.", "success")
|
||||
@@ -229,6 +250,7 @@ def create_app(config_object="config"):
|
||||
notes = request.form.get("notes", "").strip() or None
|
||||
if increment:
|
||||
battery.charge_cycles = (battery.charge_cycles or 0) + 1
|
||||
battery.battery_percentage = 100
|
||||
db.add(ChargeLog(battery_id=battery_id, charged_date=date_val,
|
||||
increment_cycles=increment, notes=notes))
|
||||
db.commit()
|
||||
@@ -572,7 +594,8 @@ def create_app(config_object="config"):
|
||||
device_types = sorted({d.device_type for d in db.query(Device).all() if d.device_type})
|
||||
return render_template("device_detail.html", device=device, brands=brands,
|
||||
available_batteries=available_batteries,
|
||||
device_types=device_types)
|
||||
device_types=device_types,
|
||||
ha_enabled=ha_client.enabled)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Devices — edit
|
||||
@@ -607,6 +630,7 @@ def create_app(config_object="config"):
|
||||
device.battery_slots = slots
|
||||
device.notes = notes
|
||||
device.device_type = device_type
|
||||
device.ha_entity_id = request.form.get("ha_entity_id", "").strip() or None
|
||||
db.commit()
|
||||
flash("Device updated.", "success")
|
||||
return redirect(url_for("device_detail", device_id=device_id))
|
||||
|
||||
Reference in New Issue
Block a user