cloudflare_r2: warmup loader caches to remove blocking listdir calls (#180398)

This commit is contained in:
Patrick Vorgers
2026-08-27 16:32:23 +00:00
committed by Franck Nijhof
parent 8c58cb13e6
commit 9f6fc980ab
4 changed files with 35 additions and 1 deletions
@@ -4,6 +4,7 @@ import logging
from typing import cast
from aiobotocore.client import AioBaseClient as S3Client
from aiobotocore.config import AioConfig
from aiobotocore.session import AioSession
from botocore.exceptions import (
ClientError,
@@ -43,6 +44,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: R2ConfigEntry) -> bool:
endpoint_url=data.get(CONF_ENDPOINT_URL),
aws_secret_access_key=data[CONF_SECRET_ACCESS_KEY],
aws_access_key_id=data[CONF_ACCESS_KEY_ID],
config=AioConfig(warm_up_loader_caches=True),
).__aenter__()
await client.head_bucket(Bucket=data[CONF_BUCKET])
except ClientError as err:
@@ -3,6 +3,7 @@
from typing import Any, override
from urllib.parse import urlparse
from aiobotocore.config import AioConfig
from aiobotocore.session import AioSession
from botocore.exceptions import (
ClientError,
@@ -78,6 +79,7 @@ class R2ConfigFlow(ConfigFlow, domain=DOMAIN):
endpoint_url=user_input.get(CONF_ENDPOINT_URL),
aws_secret_access_key=user_input[CONF_SECRET_ACCESS_KEY],
aws_access_key_id=user_input[CONF_ACCESS_KEY_ID],
config=AioConfig(warm_up_loader_caches=True),
) as client:
await client.head_bucket(Bucket=user_input[CONF_BUCKET])
except ClientError:
@@ -44,7 +44,17 @@ async def _async_start_flow(
async def test_flow(hass: HomeAssistant) -> None:
"""Test config flow."""
result = await _async_start_flow(hass)
create_client = AsyncMock(name="create_client")
create_client.__aenter__.return_value.head_bucket.return_value = {}
with patch(
"homeassistant.components.cloudflare_r2.config_flow.AioSession.create_client",
return_value=create_client,
) as patched_create_client:
result = await _async_start_flow(hass)
assert patched_create_client.call_args.kwargs["config"].warm_up_loader_caches
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "test"
assert result["data"] == USER_INPUT
@@ -73,3 +73,23 @@ async def test_setup_entry_head_bucket_error(
)
await setup_integration(hass, mock_config_entry)
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
async def test_setup_entry_warms_loader_caches(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that create_client passes warm_up_loader_caches config."""
with patch(
"homeassistant.components.cloudflare_r2.AioSession.create_client"
) as create_client:
client_ctx = AsyncMock()
client = AsyncMock()
client_ctx.__aenter__.return_value = client
create_client.return_value = client_ctx
await setup_integration(hass, mock_config_entry)
create_client.assert_called_once()
_, kwargs = create_client.call_args
assert kwargs["config"].warm_up_loader_caches is True