From 9f6fc980abc70d06a017ac6bfece899de09645f1 Mon Sep 17 00:00:00 2001 From: Patrick Vorgers Date: Thu, 27 Aug 2026 15:30:26 +0200 Subject: [PATCH] cloudflare_r2: warmup loader caches to remove blocking listdir calls (#180398) --- .../components/cloudflare_r2/__init__.py | 2 ++ .../components/cloudflare_r2/config_flow.py | 2 ++ .../cloudflare_r2/test_config_flow.py | 12 ++++++++++- tests/components/cloudflare_r2/test_init.py | 20 +++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/cloudflare_r2/__init__.py b/homeassistant/components/cloudflare_r2/__init__.py index a392120b3660..695820168cc7 100644 --- a/homeassistant/components/cloudflare_r2/__init__.py +++ b/homeassistant/components/cloudflare_r2/__init__.py @@ -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: diff --git a/homeassistant/components/cloudflare_r2/config_flow.py b/homeassistant/components/cloudflare_r2/config_flow.py index c6ab6e4206b7..47b34cc5a8df 100644 --- a/homeassistant/components/cloudflare_r2/config_flow.py +++ b/homeassistant/components/cloudflare_r2/config_flow.py @@ -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: diff --git a/tests/components/cloudflare_r2/test_config_flow.py b/tests/components/cloudflare_r2/test_config_flow.py index f2d8f03d2b04..f20066532afb 100644 --- a/tests/components/cloudflare_r2/test_config_flow.py +++ b/tests/components/cloudflare_r2/test_config_flow.py @@ -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 diff --git a/tests/components/cloudflare_r2/test_init.py b/tests/components/cloudflare_r2/test_init.py index deaf5ac46ce0..2bca5684e888 100644 --- a/tests/components/cloudflare_r2/test_init.py +++ b/tests/components/cloudflare_r2/test_init.py @@ -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