Update sub-component batteries from parent HA entity in poller and live sync

This commit is contained in:
2026-06-10 12:28:27 -05:00
parent d41fe7f4ab
commit 2a103f52a5
4 changed files with 43 additions and 13 deletions
+31
View File
@@ -182,6 +182,37 @@ def test_poll_updates_installed_batteries(ha_app, ha_client_f):
mock_ha.get_state.assert_called_once_with("sensor.dev_a_battery")
def test_poll_updates_batteries_in_subcomponents(ha_app, ha_client_f):
"""Batteries installed in sub-components are updated via the parent's HA entity."""
ha_client_f.post("/device/add", data={"name": "RC Set", "battery_slots": "0", "battery_size": ""})
ha_client_f.post("/device/add", data={"name": "Remote", "battery_slots": "2",
"battery_size": "AA", "parent_id": "1"})
ha_client_f.post("/battery/add", data={"brand": "X", "count": "1", "size": "AA"})
ha_client_f.post("/battery/1/assign", data={"device_id": "2"})
ha_client_f.post("/device/1/edit", data={
"name": "RC Set", "battery_slots": "0", "battery_size": "",
"ha_entity_id": "sensor.rc_set_battery"
})
from ha_client import HomeAssistantClient
from ha_poller import HaPoller
from models import Battery, BatteryPctLog
mock_ha = MagicMock(spec=HomeAssistantClient)
mock_ha.enabled = True
mock_ha.get_state.return_value = 37
Session = _make_session_factory(ha_app)
HaPoller(mock_ha, Session, interval=300)._poll_once()
s = Session()
b = s.get(Battery, 1)
assert b.battery_percentage == 37
logs = s.query(BatteryPctLog).filter_by(battery_id=1, source="poll").all()
assert len(logs) == 1 and logs[0].percentage == 37
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"})