Add location field to devices with dropdown selector

This commit is contained in:
2026-04-18 15:36:01 -05:00
parent 66062faac6
commit b90ab7b2b6
5 changed files with 79 additions and 3 deletions
+13 -3
View File
@@ -643,17 +643,20 @@ def create_app(config_object="config"):
def device_add():
all_devices = db.query(Device).all()
device_types = sorted({d.device_type for d in all_devices if d.device_type})
device_locations = sorted({d.location for d in all_devices if d.location})
if request.method == "POST":
name = request.form.get("name", "").strip()
slots_raw = request.form.get("battery_slots", "1").strip()
notes = request.form.get("notes", "").strip() or None
device_type = request.form.get("device_type", "").strip() or None
location = request.form.get("location", "").strip() or None
if not name:
flash("Device name is required.", "error")
return render_template("device_add.html",
device_types=device_types), 400
device_types=device_types,
device_locations=device_locations), 400
try:
slots = int(slots_raw)
@@ -663,6 +666,7 @@ def create_app(config_object="config"):
flash("Battery slots must be a positive integer.", "error")
return render_template("device_add.html",
device_types=device_types,
device_locations=device_locations,
form_name=name, form_notes=notes or "",
form_device_type=request.form.get("device_type", "")), 400
@@ -670,17 +674,20 @@ def create_app(config_object="config"):
flash(f"A device named '{name}' already exists.", "error")
return render_template("device_add.html",
device_types=device_types,
device_locations=device_locations,
form_name=name, form_slots=slots,
form_notes=notes or "",
form_device_type=request.form.get("device_type", "")), 400
device = Device(name=name, battery_slots=slots, notes=notes, device_type=device_type)
device = Device(name=name, battery_slots=slots, notes=notes,
device_type=device_type, location=location)
db.add(device)
db.commit()
flash(f"Device '{name}' added.", "success")
return redirect(url_for("device_list"))
return render_template("device_add.html", device_types=device_types)
return render_template("device_add.html", device_types=device_types,
device_locations=device_locations)
# ------------------------------------------------------------------ #
# Devices — detail
@@ -696,6 +703,7 @@ 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})
device_locations = sorted({d.location for d in db.query(Device).all() if d.location})
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=1)
@@ -716,6 +724,7 @@ def create_app(config_object="config"):
return render_template("device_detail.html", device=device, brands=brands,
available_batteries=available_batteries,
device_types=device_types,
device_locations=device_locations,
ha_enabled=ha_client.enabled,
ha_live_pct=ha_live_pct)
@@ -752,6 +761,7 @@ def create_app(config_object="config"):
device.battery_slots = slots
device.notes = notes
device.device_type = device_type
device.location = request.form.get("location", "").strip() or None
device.ha_entity_id = request.form.get("ha_entity_id", "").strip() or None
db.commit()
flash("Device updated.", "success")