Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions custom_components/hacs/utils/store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Storage handers."""
"""Storage handlers."""

from awesomeversion import AwesomeVersion
from homeassistant.const import __version__ as HAVERSION
from homeassistant.helpers.json import JSONEncoder
from homeassistant.helpers.storage import Store
from homeassistant.util import json as json_util
Expand All @@ -8,6 +10,10 @@
from ..exceptions import HacsException
from .logger import LOGGER

# serialize_in_event_loop=False is only available in HA 2025.12.0+
# See: https://developers.home-assistant.io/blog/2025/11/25/storage-helper-opt-in-serialize-in-executor/
_SERIALIZE_IN_EVENT_LOOP_SUPPORTED = AwesomeVersion(HAVERSION) >= "2025.12.0"

_LOGGER = LOGGER


Expand Down Expand Up @@ -39,7 +45,10 @@ def get_store_key(key):

def _get_store_for_key(hass, key, encoder):
"""Create a Store object for the key."""
return HACSStore(hass, VERSION_STORAGE, get_store_key(key), encoder=encoder, atomic_writes=True)
kwargs = {"encoder": encoder, "atomic_writes": True}
if _SERIALIZE_IN_EVENT_LOOP_SUPPORTED:
kwargs["serialize_in_event_loop"] = False
return HACSStore(hass, VERSION_STORAGE, get_store_key(key), **kwargs)


def get_store_for_key(hass, key):
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from custom_components.hacs.const import VERSION_STORAGE
from custom_components.hacs.exceptions import HacsException
from custom_components.hacs.utils.store import (
_SERIALIZE_IN_EVENT_LOOP_SUPPORTED,
async_load_from_store,
async_remove_store,
async_save_to_store,
Expand Down Expand Up @@ -64,3 +65,13 @@ async def test_store_store(hass: HomeAssistant, caplog: pytest.LogCaptureFixture

await async_save_to_store(hass, "test", {"test": "test"})
assert async_save_mock.call_count == 1


async def test_serialize_in_event_loop_version_check(hass: HomeAssistant) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

@copilot Use parameterized tests that pass in 2 versions and a boolean value with the expected result.
Use unittest.patch to override the core version.

Copy link
Author

Choose a reason for hiding this comment

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

Implemented parameterized test with 5 version/expected pairs and unittest.patch in 24ffd7f.

"""Test that serialize_in_event_loop flag is set based on HA version."""
# Test that the constant is a bool (either True or False depending on HA version)
assert isinstance(_SERIALIZE_IN_EVENT_LOOP_SUPPORTED, bool)

# Test that the store is created without error regardless of HA version
store = get_store_for_key(hass, "test")
assert store is not None