Validate battery percentage range on manual edit; clamp HA values to 0-100

This commit is contained in:
2026-06-10 12:31:11 -05:00
parent a9835fee1e
commit 17bad3f36b
4 changed files with 50 additions and 0 deletions
+25
View File
@@ -213,6 +213,31 @@ def test_poll_updates_batteries_in_subcomponents(ha_app, ha_client_f):
s.close()
def test_poll_clamps_out_of_range_percentage(ha_app, ha_client_f):
"""HA sensors occasionally report out-of-range values; the poller clamps to 0-100."""
ha_client_f.post("/device/add", data={"name": "Dev E", "battery_slots": "1", "battery_size": "AA"})
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 E", "battery_slots": "1", "battery_size": "AA", "ha_entity_id": "sensor.dev_e"
})
from ha_client import HomeAssistantClient
from ha_poller import HaPoller
from models import Battery
mock_ha = MagicMock(spec=HomeAssistantClient)
mock_ha.enabled = True
mock_ha.get_state.return_value = 150
Session = _make_session_factory(ha_app)
HaPoller(mock_ha, Session, interval=300)._poll_once()
s = Session()
assert s.get(Battery, 1).battery_percentage == 100
s.close()
def test_poll_skips_uninstalled_batteries(ha_app, ha_client_f):
"""Batteries that are available (not installed) are not updated by the poller."""
ha_client_f.post("/device/add", data={"name": "Dev B", "battery_slots": "1", "battery_size": "AA"})