Cache Store instances per key to preserve debounce state#5294
Open
ludeeus wants to merge 4 commits into
Open
Conversation
get_store_for_key() built a new HACSStore on every call. Store.async_delay_save debounces via instance state — it cancels the previous _unsub_delayed_write and keeps a single EVENT_HOMEASSISTANT_FINAL_WRITE listener per Store. Returning a fresh instance each call defeats the debounce and leaks a final-write listener on every scheduled save. Cache the HACSStore on hass.data so callers share one instance per key.
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request introduces per-key caching for Store instances created by get_store_for_key() so repeated calls for the same key return the same Store object, preserving Store.async_delay_save() debounce behavior.
Changes:
- Added a
STORE_CACHE_KEYconstant and ahass.data-backed cache forStoreinstances inget_store_for_key(). - Updated
get_store_for_key()to reuse cachedStoreinstances per key instead of always creating new ones. - Added a unit test verifying same-key identity reuse and different-key separation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
custom_components/hacs/utils/store.py |
Caches Store instances in hass.data to preserve per-instance debounce state across repeated calls. |
tests/utils/test_store.py |
Adds coverage to ensure get_store_for_key() returns stable instances per key. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Caching on hass.data leaked the cache across unload/reload — stale HACSStore objects (with their final-write listeners) survived until HA restarted. runtime_data is reset by async_setup_entry on each setup, so moving the cache there ties its lifecycle to the integration. Adds a typed HacsRuntimeData dataclass and HacsConfigEntry alias per the HA quality scale runtime-data rule, and replaces the hass.data sentinel in get_store_for_key with a lookup of the (single-instance) HACS config entry's runtime_data.store_cache. Falls back to an uncached Store when no entry is loaded (defensive, used by existing tests that don't set up an entry).
Revert the runtime_data scaffolding and keep the simpler hass.data cache, but pop STORE_CACHE_KEY in async_unload_entry so cached HACSStore objects (and their EVENT_HOMEASSISTANT_FINAL_WRITE listeners) don't survive an unload/reload cycle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change implements caching of Store instances in
get_store_for_key()to ensure that repeated calls for the same key return the same Store object.This is required for #5293 to work.