From 52d11059973087baab7398d27fab86a426f23947 Mon Sep 17 00:00:00 2001 From: Darek Date: Sun, 19 Apr 2026 19:00:33 -0500 Subject: [PATCH] Make device.battery_size NOT NULL in schema and tests --- app.py | 4 +++- models.py | 2 +- tests/test_ha_integration.py | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app.py b/app.py index 9de1d5e..b11c697 100644 --- a/app.py +++ b/app.py @@ -792,7 +792,9 @@ def create_app(config_object="config"): device.battery_slots = slots device.notes = notes device.device_type = device_type - device.battery_size = request.form.get("battery_size", "").strip() or None + new_battery_size = request.form.get("battery_size", "").strip() or None + if new_battery_size is not None: + device.battery_size = new_battery_size device.location = request.form.get("location", "").strip() or None device.ha_entity_id = request.form.get("ha_entity_id", "").strip() or None db.commit() diff --git a/models.py b/models.py index b45f68f..1ad6d25 100644 --- a/models.py +++ b/models.py @@ -13,7 +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) - battery_size = Column(String(20), nullable=True) # AA, AAA, 9V, CR2032 … + battery_size = Column(String(20), nullable=False) # AA, AAA, 9V, CR2032 … 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/tests/test_ha_integration.py b/tests/test_ha_integration.py index 530e234..e5b7d6e 100644 --- a/tests/test_ha_integration.py +++ b/tests/test_ha_integration.py @@ -159,7 +159,7 @@ def test_poll_updates_installed_batteries(ha_app, ha_client_f): ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) # Set ha_entity_id on device ha_client_f.post("/device/1/edit", data={ - "name": "Dev A", "battery_slots": "2", "ha_entity_id": "sensor.dev_a_battery" + "name": "Dev A", "battery_slots": "2", "battery_size": "AA", "ha_entity_id": "sensor.dev_a_battery" }) from ha_client import HomeAssistantClient @@ -188,7 +188,7 @@ def test_poll_skips_uninstalled_batteries(ha_app, ha_client_f): ha_client_f.post("/battery/add", data={"brand": "X", "count": "1"}) # Set entity on device but do NOT install the battery ha_client_f.post("/device/1/edit", data={ - "name": "Dev B", "battery_slots": "1", "ha_entity_id": "sensor.dev_b_battery" + "name": "Dev B", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.dev_b_battery" }) from ha_client import HomeAssistantClient @@ -232,7 +232,7 @@ def test_poll_handles_api_error_gracefully(ha_app, ha_client_f): ha_client_f.post("/battery/add", data={"brand": "X", "count": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev D", "battery_slots": "1", "ha_entity_id": "sensor.dev_d" + "name": "Dev D", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.dev_d" }) from ha_client import HomeAssistantClient @@ -352,7 +352,7 @@ def test_device_detail_shows_ha_field(ha_client_f): def test_edit_device_ha_entity_id_saves(ha_client_f): ha_client_f.post("/device/add", data={"name": "Dev F", "battery_slots": "1", "battery_size": "AA"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev F", "battery_slots": "1", "ha_entity_id": "sensor.my_remote" + "name": "Dev F", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.my_remote" }) resp = ha_client_f.get("/device/1") assert b"sensor.my_remote" in resp.data @@ -361,11 +361,11 @@ def test_edit_device_ha_entity_id_saves(ha_client_f): def test_edit_device_ha_entity_id_clear(ha_client_f): ha_client_f.post("/device/add", data={"name": "Dev G", "battery_slots": "1", "battery_size": "AA"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev G", "battery_slots": "1", "ha_entity_id": "sensor.foo" + "name": "Dev G", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.foo" }) # Now clear it ha_client_f.post("/device/1/edit", data={ - "name": "Dev G", "battery_slots": "1", "ha_entity_id": "" + "name": "Dev G", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "" }) resp = ha_client_f.get("/device/1") assert b"sensor.foo" not in resp.data @@ -419,7 +419,7 @@ def test_poll_skips_update_when_percentage_unchanged(ha_app, ha_client_f): ha_client_f.post("/battery/add", data={"brand": "X", "count": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev NoCh", "battery_slots": "1", "ha_entity_id": "sensor.noch" + "name": "Dev NoCh", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.noch" }) from sqlalchemy import create_engine @@ -450,7 +450,7 @@ def test_poll_creates_pct_log_on_change(ha_app, ha_client_f): ha_client_f.post("/battery/add", data={"brand": "X", "count": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev Chg", "battery_slots": "1", "ha_entity_id": "sensor.chg" + "name": "Dev Chg", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.chg" }) from ha_client import HomeAssistantClient @@ -521,7 +521,7 @@ def test_device_detail_shows_live_pct(ha_app, ha_client_f): """Opening a device page fetches live % from HA and displays it.""" ha_client_f.post("/device/add", data={"name": "Dev Live", "battery_slots": "1", "battery_size": "AA"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev Live", "battery_slots": "1", "ha_entity_id": "sensor.live_test" + "name": "Dev Live", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.live_test" }) mock_resp = MagicMock() @@ -541,7 +541,7 @@ def test_device_detail_updates_battery_on_load(ha_app, ha_client_f): ha_client_f.post("/battery/add", data={"brand": "X", "count": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/device/1/edit", data={ - "name": "Dev Update", "battery_slots": "1", "ha_entity_id": "sensor.update_test" + "name": "Dev Update", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.update_test" }) mock_resp = MagicMock()