HA improvements: entity overflow fix, live % fetch on device page, searchable entity dropdown
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from flask import Flask, render_template, redirect, url_for, request, flash, abort, send_from_directory
|
||||
from flask import Flask, render_template, redirect, url_for, request, flash, abort, send_from_directory, jsonify
|
||||
from sqlalchemy import create_engine, func
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
@@ -547,6 +547,12 @@ def create_app(config_object="config"):
|
||||
# Devices — list
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
@app.route("/ha/entities")
|
||||
def ha_entities():
|
||||
if not ha_client.enabled:
|
||||
return jsonify([])
|
||||
return jsonify(ha_client.list_battery_entities())
|
||||
|
||||
@app.route("/device/")
|
||||
def device_list():
|
||||
devices = db.query(Device).order_by(Device.name).all()
|
||||
@@ -614,10 +620,28 @@ def create_app(config_object="config"):
|
||||
.filter_by(status="available")
|
||||
.order_by(Battery.label).all())
|
||||
device_types = sorted({d.device_type for d in db.query(Device).all() if d.device_type})
|
||||
ha_live_pct = None
|
||||
if ha_client.enabled and device.ha_entity_id:
|
||||
ha_live_pct = ha_client.get_state(device.ha_entity_id, timeout=4)
|
||||
if ha_live_pct is not None:
|
||||
changed = False
|
||||
for battery in device.batteries:
|
||||
if battery.status == "installed" and battery.battery_percentage != ha_live_pct:
|
||||
battery.battery_percentage = ha_live_pct
|
||||
db.add(BatteryPctLog(
|
||||
battery_id=battery.id,
|
||||
percentage=ha_live_pct,
|
||||
recorded_at=datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
source="poll",
|
||||
))
|
||||
changed = True
|
||||
if changed:
|
||||
db.commit()
|
||||
return render_template("device_detail.html", device=device, brands=brands,
|
||||
available_batteries=available_batteries,
|
||||
device_types=device_types,
|
||||
ha_enabled=ha_client.enabled)
|
||||
ha_enabled=ha_client.enabled,
|
||||
ha_live_pct=ha_live_pct)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Devices — edit
|
||||
|
||||
Reference in New Issue
Block a user