diff --git a/app.py b/app.py index 8dd0654..2ca8e91 100644 --- a/app.py +++ b/app.py @@ -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") diff --git a/models.py b/models.py index 4cd8cb6..e7de76f 100644 --- a/models.py +++ b/models.py @@ -13,6 +13,7 @@ class Device(Base): name = Column(String(100), nullable=False, unique=True) battery_slots = Column(Integer, nullable=False, default=1) device_type = Column(String(50), nullable=True) + location = Column(String(100), nullable=True) notes = Column(Text, nullable=True) ha_entity_id = Column(String(100), nullable=True) # e.g. "sensor.tv_remote_battery" diff --git a/templates/device_add.html b/templates/device_add.html index d0e51bf..eafb04b 100644 --- a/templates/device_add.html +++ b/templates/device_add.html @@ -39,6 +39,20 @@ style="display:{% if _cur_type and _cur_type not in _preset_types %}''{% else %}none{% endif %};margin-top:0.4rem;"> +