Skip to content

Cache Store instances per key to preserve debounce state#5294

Open
ludeeus wants to merge 4 commits into
mainfrom
claude/gifted-keller-8Yrud
Open

Cache Store instances per key to preserve debounce state#5294
ludeeus wants to merge 4 commits into
mainfrom
claude/gifted-keller-8Yrud

Conversation

@ludeeus
Copy link
Copy Markdown
Member

@ludeeus ludeeus commented May 30, 2026

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.

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.
Copilot AI review requested due to automatic review settings May 30, 2026 11:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_KEY constant and a hass.data-backed cache for Store instances in get_store_for_key().
  • Updated get_store_for_key() to reuse cached Store instances 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.

Comment thread custom_components/hacs/utils/store.py Outdated
claude added 3 commits May 30, 2026 12:21
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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@ludeeus ludeeus marked this pull request as ready for review May 30, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants