Fix backfill-complete notification firing on every HA restart.
Persist a one-shot cold_start_backfill_announced flag in the JSON store so the initial batch notification is shown only once; backfill still re-runs silently for batteries without replaced_on. Release v0.1.6. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+3
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
## Current State (2026-05-11, late-morning)
|
||||
|
||||
The Battery Lifetime custom Home Assistant integration is working and deployed via HACS on the user's live install (70 battery-powered devices → 554 companion entities). Current release is **v0.1.4** on GitHub via HACS.
|
||||
The Battery Lifetime custom Home Assistant integration is working and deployed via HACS on the user's live install (70 battery-powered devices → 554 companion entities). Current release is **v0.1.6** on GitHub via HACS.
|
||||
|
||||
The OpenSpec change `add-battery-lifetime` is at 56/62 tasks; the remaining six (10.2–10.7) are manual-validation gates against a real HA install. Some are implicitly being validated right now by the live install; see "What Needs Doing Next" below.
|
||||
|
||||
@@ -28,7 +28,8 @@ The OpenSpec changes `optimize-coordinator-tick` (v0.1.2), `defer-cold-start-bac
|
||||
- v0.1.2 ships three coordinator scheduling optimizations (drop redundant 10-minute timer, per-source-event O(1) update via `async_set_updated_data`, diff-gated heartbeat publish). 5 new tests in `tests/test_coordinator.py`, full suite 112 passed.
|
||||
- v0.1.3 defers cold-start backfill out of `async_setup`'s await chain via `hass.async_create_task`, removing the multi-minute config-entry freeze on installs with many batteries (the v0.1.1 dialog warning is no longer applicable and was softened). Adds a one-shot `persistent_notification` (`battery_lifetime_cold_start_complete`) when the initial backfill batch finishes. Ships `brand/logo.png` + `brand/logo@2x.png` (copies of the icon assets) for HA's Brands Proxy logo slot. 3 new tests in `tests/test_coordinator.py`, full suite 115 passed.
|
||||
- v0.1.4 closes the orphan-companion-entity gap: when `prune_removed_older_than` actually drops a `unique_id` from the JSON store (after the 30-day grace window), the coordinator now removes the per-source companion device via `device_registry.async_remove_device`. HA's device-registry removal cascades to the entity registry and clears the eight per-source companion entries automatically. Cleanup runs from both prune call sites (`_async_registry_changed action: remove` + `_handle_tick`). Idempotent on missing device. Soft-deleted entries within their grace window are NOT touched; restore semantics are unchanged. 3 new tests, full suite 118 passed.
|
||||
- Both `master` and tags `v0.1.0`/`v0.1.1`/`v0.1.2`/`v0.1.3`/`v0.1.4` pushed to gitea (origin) and GitHub (github).
|
||||
- v0.1.6 fixes the cold-start backfill completion notification firing on every HA restart. The one-shot `persistent_notification` (`battery_lifetime_cold_start_complete`) is now gated by a persisted `cold_start_backfill_announced` flag in the JSON store; backfill still re-runs silently on restart for batteries without `replaced_on` (idempotent, per spec). Upgrades with existing battery entries migrate the flag to `true` so no spurious notification on first restart after update. 1 new test, full suite 127 passed.
|
||||
- Both `master` and tags `v0.1.0`/`v0.1.1`/`v0.1.2`/`v0.1.3`/`v0.1.4`/`v0.1.5`/`v0.1.6` pushed to gitea (origin) and GitHub (github).
|
||||
- README and OpenSpec specs aligned with the implementation; `replacement-detection/spec.md` enumerates the three stale-prior services explicitly. `coordinator-scheduling/spec.md` documents the per-event vs heartbeat contracts, the non-blocking cold-start contract, AND the prune-driven companion-device purge.
|
||||
|
||||
## What Needs Doing Next (Immediate)
|
||||
|
||||
@@ -274,6 +274,8 @@ class BatteryLifetimeCoordinator(
|
||||
self._announce_backfill_complete()
|
||||
|
||||
def _announce_backfill_complete(self) -> None:
|
||||
if self._store.cold_start_backfill_announced:
|
||||
return
|
||||
persistent_notification.async_create(
|
||||
self.hass,
|
||||
(
|
||||
@@ -284,6 +286,7 @@ class BatteryLifetimeCoordinator(
|
||||
title="Battery Lifetime: backfill complete",
|
||||
notification_id="battery_lifetime_cold_start_complete",
|
||||
)
|
||||
self._store.set_cold_start_backfill_announced()
|
||||
|
||||
def _purge_companions_for_pruned(self, unique_ids: list[str]) -> None:
|
||||
"""Remove the companion device for each pruned source.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"domain": "battery_lifetime",
|
||||
"name": "Battery Lifetime",
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.6",
|
||||
"documentation": "https://github.com/Falkvinge/hassio-integration-battery-lifetime",
|
||||
"issue_tracker": "https://github.com/Falkvinge/hassio-integration-battery-lifetime/issues",
|
||||
"codeowners": ["@rickfalkvinge"],
|
||||
|
||||
@@ -46,6 +46,7 @@ def _empty_data(default_profile: str = DEFAULT_PROFILE) -> dict[str, Any]:
|
||||
return {
|
||||
"version": STORAGE_VERSION,
|
||||
"default_profile": default_profile,
|
||||
"cold_start_backfill_announced": False,
|
||||
"batteries": {},
|
||||
}
|
||||
|
||||
@@ -110,6 +111,9 @@ class BatteryLifetimeStore:
|
||||
)
|
||||
data.setdefault("default_profile", DEFAULT_PROFILE)
|
||||
data.setdefault("batteries", {})
|
||||
if "cold_start_backfill_announced" not in data:
|
||||
# Existing installs already passed the initial backfill phase.
|
||||
data["cold_start_backfill_announced"] = bool(data["batteries"])
|
||||
data["version"] = STORAGE_VERSION
|
||||
for entry in data["batteries"].values():
|
||||
for field in _PER_BATTERY_FIELDS:
|
||||
@@ -125,6 +129,18 @@ class BatteryLifetimeStore:
|
||||
self._ensure_loaded()
|
||||
return str(self._data.get("default_profile", DEFAULT_PROFILE))
|
||||
|
||||
@property
|
||||
def cold_start_backfill_announced(self) -> bool:
|
||||
"""Whether the one-time cold-start backfill notification was shown."""
|
||||
self._ensure_loaded()
|
||||
return bool(self._data.get("cold_start_backfill_announced"))
|
||||
|
||||
def set_cold_start_backfill_announced(self) -> None:
|
||||
"""Mark the initial cold-start backfill batch as announced."""
|
||||
self._ensure_loaded()
|
||||
self._data["cold_start_backfill_announced"] = True
|
||||
self.async_save_debounced()
|
||||
|
||||
def set_default_profile(self, profile: str) -> None:
|
||||
self._ensure_loaded()
|
||||
self._data["default_profile"] = profile
|
||||
|
||||
@@ -542,6 +542,45 @@ async def test_no_pending_backfill_no_notification(hass: Any) -> None:
|
||||
await coord.async_shutdown()
|
||||
|
||||
|
||||
async def test_cold_start_completion_not_repeated_on_restart(hass: Any) -> None:
|
||||
"""The backfill-complete notification fires once, not on every HA restart."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.persistent_notification import (
|
||||
_async_get_or_create_notifications,
|
||||
)
|
||||
|
||||
registry = er.async_get(hass)
|
||||
_register(registry, entity_id="sensor.foo_battery", unique_id="uid-foo")
|
||||
hass.states.async_set("sensor.foo_battery", "84", {})
|
||||
|
||||
store = BatteryLifetimeStore(hass)
|
||||
await store.async_load()
|
||||
coord = BatteryLifetimeCoordinator(hass, store=store)
|
||||
await coord.async_setup()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
notifications = _async_get_or_create_notifications(hass)
|
||||
assert "battery_lifetime_cold_start_complete" in notifications
|
||||
assert store.cold_start_backfill_announced is True
|
||||
await store.async_save()
|
||||
await coord.async_shutdown()
|
||||
|
||||
store_reload = BatteryLifetimeStore(hass)
|
||||
await store_reload.async_load()
|
||||
assert store_reload.cold_start_backfill_announced is True
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.persistent_notification.async_create"
|
||||
) as mock_create:
|
||||
coord_reload = BatteryLifetimeCoordinator(hass, store=store_reload)
|
||||
await coord_reload.async_setup()
|
||||
await hass.async_block_till_done()
|
||||
mock_create.assert_not_called()
|
||||
|
||||
await coord_reload.async_shutdown()
|
||||
|
||||
|
||||
async def test_purge_companions_for_pruned_removes_device(hass: Any) -> None:
|
||||
"""A pruned source's companion device is removed from the device registry."""
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
||||
|
||||
@@ -27,6 +27,7 @@ async def store(hass: Any) -> BatteryLifetimeStore:
|
||||
|
||||
async def test_fresh_store_loads_defaults(store: BatteryLifetimeStore) -> None:
|
||||
assert store.default_profile == DEFAULT_PROFILE
|
||||
assert store.cold_start_backfill_announced is False
|
||||
assert list(store.iter_batteries()) == []
|
||||
|
||||
|
||||
@@ -104,6 +105,7 @@ async def test_migration_fills_missing_fields(
|
||||
assert entry["profile"] == DEFAULT_PROFILE
|
||||
assert entry["tracking_enabled"] is True
|
||||
assert entry["threshold_override"] is None
|
||||
assert store.cold_start_backfill_announced is True
|
||||
|
||||
|
||||
async def test_remove_then_prune(store: BatteryLifetimeStore) -> None:
|
||||
|
||||
Reference in New Issue
Block a user