Make device.battery_size NOT NULL in schema and tests

This commit is contained in:
2026-04-19 19:00:33 -05:00
parent 3e75bb3ab4
commit 52d1105997
3 changed files with 14 additions and 12 deletions
+3 -1
View File
@@ -792,7 +792,9 @@ def create_app(config_object="config"):
device.battery_slots = slots device.battery_slots = slots
device.notes = notes device.notes = notes
device.device_type = device_type 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.location = request.form.get("location", "").strip() or None
device.ha_entity_id = request.form.get("ha_entity_id", "").strip() or None device.ha_entity_id = request.form.get("ha_entity_id", "").strip() or None
db.commit() db.commit()
+1 -1
View File
@@ -13,7 +13,7 @@ class Device(Base):
name = Column(String(100), nullable=False, unique=True) name = Column(String(100), nullable=False, unique=True)
battery_slots = Column(Integer, nullable=False, default=1) battery_slots = Column(Integer, nullable=False, default=1)
device_type = Column(String(50), nullable=True) 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) location = Column(String(100), nullable=True)
notes = Column(Text, nullable=True) notes = Column(Text, nullable=True)
ha_entity_id = Column(String(100), nullable=True) # e.g. "sensor.tv_remote_battery" ha_entity_id = Column(String(100), nullable=True) # e.g. "sensor.tv_remote_battery"
+10 -10
View File
@@ -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"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"})
# Set ha_entity_id on device # Set ha_entity_id on device
ha_client_f.post("/device/1/edit", data={ 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 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"}) ha_client_f.post("/battery/add", data={"brand": "X", "count": "1"})
# Set entity on device but do NOT install the battery # Set entity on device but do NOT install the battery
ha_client_f.post("/device/1/edit", data={ 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 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/add", data={"brand": "X", "count": "1"})
ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"})
ha_client_f.post("/device/1/edit", data={ 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 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): 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/add", data={"name": "Dev F", "battery_slots": "1", "battery_size": "AA"})
ha_client_f.post("/device/1/edit", data={ 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") resp = ha_client_f.get("/device/1")
assert b"sensor.my_remote" in resp.data 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): 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/add", data={"name": "Dev G", "battery_slots": "1", "battery_size": "AA"})
ha_client_f.post("/device/1/edit", data={ 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 # Now clear it
ha_client_f.post("/device/1/edit", data={ 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") resp = ha_client_f.get("/device/1")
assert b"sensor.foo" not in resp.data 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/add", data={"brand": "X", "count": "1"})
ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"})
ha_client_f.post("/device/1/edit", data={ 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 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/add", data={"brand": "X", "count": "1"})
ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"})
ha_client_f.post("/device/1/edit", data={ 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 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.""" """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/add", data={"name": "Dev Live", "battery_slots": "1", "battery_size": "AA"})
ha_client_f.post("/device/1/edit", data={ 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() 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/add", data={"brand": "X", "count": "1"})
ha_client_f.post("/battery/1/assign", data={"device_id": "1"}) ha_client_f.post("/battery/1/assign", data={"device_id": "1"})
ha_client_f.post("/device/1/edit", data={ 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() mock_resp = MagicMock()