From 4863df00a1d207f49c53a68cbf7005166d0d67e3 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 25 Feb 2026 21:36:53 -0600 Subject: [PATCH 1/5] Avoid invalid cache future state (#164081) --- homeassistant/components/tts/__init__.py | 4 +++ tests/components/tts/test_init.py | 40 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 3645afedd6d77..fb9dfcac13cc4 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -527,6 +527,8 @@ def async_set_message(self, message: str) -> None: This method will leverage a disk cache to speed up generation. """ + if self._result_cache.done(): + return self._result_cache.set_result( self._manager.async_cache_message_in_memory( engine=self.engine, @@ -543,6 +545,8 @@ def async_set_message_stream(self, message_stream: AsyncGenerator[str]) -> None: This method can result in faster first byte when generating long responses. """ + if self._result_cache.done(): + return self._result_cache.set_result( self._manager.async_cache_message_stream_in_memory( engine=self.engine, diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index 5a6e988c82d14..ee7878e603a11 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -1954,6 +1954,46 @@ async def stream_message(): assert result_data == data +async def test_result_stream_message_set_idempotent( + hass: HomeAssistant, mock_tts_entity: MockTTSEntity +) -> None: + """Test setting a result stream message more than once.""" + await mock_config_entry_setup(hass, mock_tts_entity) + + stream = tts.async_create_stream(hass, mock_tts_entity.entity_id) + stream.async_set_message("hello") + cache_first = stream._result_cache.result() + stream.async_set_message("world") + assert stream._result_cache.result() is cache_first + + async def async_stream_tts_audio( + request: tts.TTSAudioRequest, + ) -> tts.TTSAudioResponse: + """Mock stream TTS audio.""" + + async def gen_data(): + async for msg in request.message_gen: + yield msg.encode() + + return tts.TTSAudioResponse( + extension="mp3", + data_gen=gen_data(), + ) + + mock_tts_entity.async_stream_tts_audio = async_stream_tts_audio + mock_tts_entity.async_supports_streaming_input = Mock(return_value=True) + + async def stream_message(): + """Mock stream message.""" + yield "h" + + stream2 = tts.async_create_stream(hass, mock_tts_entity.entity_id) + stream2.async_set_message_stream(stream_message()) + cache_first = stream2._result_cache.result() + stream2.async_set_message_stream(stream_message()) + assert stream2._result_cache.result() is cache_first + + async def test_tts_cache() -> None: """Test TTSCache.""" From f5c996e243fe4ae02010a0f11f4b91a041b2c434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Bregu=C5=82a?= Date: Thu, 26 Feb 2026 07:39:50 +0100 Subject: [PATCH 2/5] Add support for S3 prefix in AWS S3 integration (#162836) Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com> Co-authored-by: Claude Haiku 4.5 --- homeassistant/components/aws_s3/backup.py | 39 ++-- .../components/aws_s3/config_flow.py | 34 +++- homeassistant/components/aws_s3/const.py | 1 + .../components/aws_s3/coordinator.py | 7 +- homeassistant/components/aws_s3/helpers.py | 8 +- .../components/aws_s3/quality_scale.yaml | 4 +- homeassistant/components/aws_s3/strings.json | 2 + tests/components/aws_s3/conftest.py | 12 +- tests/components/aws_s3/const.py | 10 +- tests/components/aws_s3/test_backup.py | 184 +++++++++++++++++- tests/components/aws_s3/test_config_flow.py | 107 +++++++++- tests/components/aws_s3/test_sensor.py | 20 +- 12 files changed, 387 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/aws_s3/backup.py b/homeassistant/components/aws_s3/backup.py index 0d03afa6ac51e..784e267edab60 100644 --- a/homeassistant/components/aws_s3/backup.py +++ b/homeassistant/components/aws_s3/backup.py @@ -19,7 +19,7 @@ from homeassistant.core import HomeAssistant, callback from . import S3ConfigEntry -from .const import CONF_BUCKET, DATA_BACKUP_AGENT_LISTENERS, DOMAIN +from .const import CONF_BUCKET, CONF_PREFIX, DATA_BACKUP_AGENT_LISTENERS, DOMAIN from .helpers import async_list_backups_from_s3 _LOGGER = logging.getLogger(__name__) @@ -100,6 +100,13 @@ def __init__(self, hass: HomeAssistant, entry: S3ConfigEntry) -> None: self.unique_id = entry.entry_id self._backup_cache: dict[str, AgentBackup] = {} self._cache_expiration = time() + self._prefix: str = entry.data.get(CONF_PREFIX, "") + + def _with_prefix(self, key: str) -> str: + """Add prefix to a key if configured.""" + if not self._prefix: + return key + return f"{self._prefix}/{key}" @handle_boto_errors async def async_download_backup( @@ -115,7 +122,9 @@ async def async_download_backup( backup = await self._find_backup_by_id(backup_id) tar_filename, _ = suggested_filenames(backup) - response = await self._client.get_object(Bucket=self._bucket, Key=tar_filename) + response = await self._client.get_object( + Bucket=self._bucket, Key=self._with_prefix(tar_filename) + ) return response["Body"].iter_chunks() async def async_upload_backup( @@ -142,7 +151,7 @@ async def async_upload_backup( metadata_content = json.dumps(backup.as_dict()) await self._client.put_object( Bucket=self._bucket, - Key=metadata_filename, + Key=self._with_prefix(metadata_filename), Body=metadata_content, ) except BotoCoreError as err: @@ -169,7 +178,7 @@ async def _upload_simple( await self._client.put_object( Bucket=self._bucket, - Key=tar_filename, + Key=self._with_prefix(tar_filename), Body=bytes(file_data), ) @@ -186,7 +195,7 @@ async def _upload_multipart( _LOGGER.debug("Starting multipart upload for %s", tar_filename) multipart_upload = await self._client.create_multipart_upload( Bucket=self._bucket, - Key=tar_filename, + Key=self._with_prefix(tar_filename), ) upload_id = multipart_upload["UploadId"] try: @@ -216,7 +225,7 @@ async def _upload_multipart( ) part = await cast(Any, self._client).upload_part( Bucket=self._bucket, - Key=tar_filename, + Key=self._with_prefix(tar_filename), PartNumber=part_number, UploadId=upload_id, Body=part_data.tobytes(), @@ -244,7 +253,7 @@ async def _upload_multipart( ) part = await cast(Any, self._client).upload_part( Bucket=self._bucket, - Key=tar_filename, + Key=self._with_prefix(tar_filename), PartNumber=part_number, UploadId=upload_id, Body=remaining_data.tobytes(), @@ -253,7 +262,7 @@ async def _upload_multipart( await cast(Any, self._client).complete_multipart_upload( Bucket=self._bucket, - Key=tar_filename, + Key=self._with_prefix(tar_filename), UploadId=upload_id, MultipartUpload={"Parts": parts}, ) @@ -262,7 +271,7 @@ async def _upload_multipart( try: await self._client.abort_multipart_upload( Bucket=self._bucket, - Key=tar_filename, + Key=self._with_prefix(tar_filename), UploadId=upload_id, ) except BotoCoreError: @@ -283,8 +292,12 @@ async def async_delete_backup( tar_filename, metadata_filename = suggested_filenames(backup) # Delete both the backup file and its metadata file - await self._client.delete_object(Bucket=self._bucket, Key=tar_filename) - await self._client.delete_object(Bucket=self._bucket, Key=metadata_filename) + await self._client.delete_object( + Bucket=self._bucket, Key=self._with_prefix(tar_filename) + ) + await self._client.delete_object( + Bucket=self._bucket, Key=self._with_prefix(metadata_filename) + ) # Reset cache after successful deletion self._cache_expiration = time() @@ -317,7 +330,9 @@ async def _list_backups(self) -> dict[str, AgentBackup]: if time() <= self._cache_expiration: return self._backup_cache - backups_list = await async_list_backups_from_s3(self._client, self._bucket) + backups_list = await async_list_backups_from_s3( + self._client, self._bucket, self._prefix + ) self._backup_cache = {b.backup_id: b for b in backups_list} self._cache_expiration = time() + CACHE_TTL diff --git a/homeassistant/components/aws_s3/config_flow.py b/homeassistant/components/aws_s3/config_flow.py index 3d8f3479aa328..cb9d363172a3b 100644 --- a/homeassistant/components/aws_s3/config_flow.py +++ b/homeassistant/components/aws_s3/config_flow.py @@ -22,6 +22,7 @@ CONF_ACCESS_KEY_ID, CONF_BUCKET, CONF_ENDPOINT_URL, + CONF_PREFIX, CONF_SECRET_ACCESS_KEY, DEFAULT_ENDPOINT_URL, DESCRIPTION_AWS_S3_DOCS_URL, @@ -39,6 +40,7 @@ vol.Required(CONF_ENDPOINT_URL, default=DEFAULT_ENDPOINT_URL): TextSelector( config=TextSelectorConfig(type=TextSelectorType.URL) ), + vol.Optional(CONF_PREFIX, default=""): cv.string, } ) @@ -53,12 +55,17 @@ async def async_step_user( errors: dict[str, str] = {} if user_input is not None: - self._async_abort_entries_match( - { - CONF_BUCKET: user_input[CONF_BUCKET], - CONF_ENDPOINT_URL: user_input[CONF_ENDPOINT_URL], - } - ) + normalized_prefix = user_input.get(CONF_PREFIX, "").strip("/") + # Check for existing entries, treating missing prefix as empty + for entry in self._async_current_entries(include_ignore=False): + entry_prefix = (entry.data.get(CONF_PREFIX) or "").strip("/") + if ( + entry.data.get(CONF_BUCKET) == user_input[CONF_BUCKET] + and entry.data.get(CONF_ENDPOINT_URL) + == user_input[CONF_ENDPOINT_URL] + and entry_prefix == normalized_prefix + ): + return self.async_abort(reason="already_configured") hostname = urlparse(user_input[CONF_ENDPOINT_URL]).hostname if not hostname or not hostname.endswith(AWS_DOMAIN): @@ -83,9 +90,18 @@ async def async_step_user( except ConnectionError: errors[CONF_ENDPOINT_URL] = "cannot_connect" else: - return self.async_create_entry( - title=user_input[CONF_BUCKET], data=user_input - ) + data = dict(user_input) + if not normalized_prefix: + # Do not persist empty optional values + data.pop(CONF_PREFIX, None) + else: + data[CONF_PREFIX] = normalized_prefix + + title = user_input[CONF_BUCKET] + if normalized_prefix: + title = f"{title} - {normalized_prefix}" + + return self.async_create_entry(title=title, data=data) return self.async_show_form( step_id="user", diff --git a/homeassistant/components/aws_s3/const.py b/homeassistant/components/aws_s3/const.py index a6863e6c38a74..b4eed69c4a960 100644 --- a/homeassistant/components/aws_s3/const.py +++ b/homeassistant/components/aws_s3/const.py @@ -11,6 +11,7 @@ CONF_SECRET_ACCESS_KEY = "secret_access_key" CONF_ENDPOINT_URL = "endpoint_url" CONF_BUCKET = "bucket" +CONF_PREFIX = "prefix" AWS_DOMAIN = "amazonaws.com" DEFAULT_ENDPOINT_URL = f"https://s3.eu-central-1.{AWS_DOMAIN}/" diff --git a/homeassistant/components/aws_s3/coordinator.py b/homeassistant/components/aws_s3/coordinator.py index 52735ce364fc8..08df1dd4520b7 100644 --- a/homeassistant/components/aws_s3/coordinator.py +++ b/homeassistant/components/aws_s3/coordinator.py @@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import CONF_BUCKET, DOMAIN +from .const import CONF_BUCKET, CONF_PREFIX, DOMAIN from .helpers import async_list_backups_from_s3 SCAN_INTERVAL = timedelta(hours=6) @@ -53,11 +53,14 @@ def __init__( ) self.client = client self._bucket: str = entry.data[CONF_BUCKET] + self._prefix: str = entry.data.get(CONF_PREFIX, "") async def _async_update_data(self) -> SensorData: """Fetch data from AWS S3.""" try: - backups = await async_list_backups_from_s3(self.client, self._bucket) + backups = await async_list_backups_from_s3( + self.client, self._bucket, self._prefix + ) except BotoCoreError as error: raise UpdateFailed( translation_domain=DOMAIN, diff --git a/homeassistant/components/aws_s3/helpers.py b/homeassistant/components/aws_s3/helpers.py index 0eea233c797c6..4a5af12a4c033 100644 --- a/homeassistant/components/aws_s3/helpers.py +++ b/homeassistant/components/aws_s3/helpers.py @@ -17,11 +17,17 @@ async def async_list_backups_from_s3( client: S3Client, bucket: str, + prefix: str, ) -> list[AgentBackup]: """List backups from an S3 bucket by reading metadata files.""" paginator = client.get_paginator("list_objects_v2") metadata_files: list[dict[str, Any]] = [] - async for page in paginator.paginate(Bucket=bucket): + + list_kwargs: dict[str, Any] = {"Bucket": bucket} + if prefix: + list_kwargs["Prefix"] = prefix + "/" + + async for page in paginator.paginate(**list_kwargs): metadata_files.extend( obj for obj in page.get("Contents", []) diff --git a/homeassistant/components/aws_s3/quality_scale.yaml b/homeassistant/components/aws_s3/quality_scale.yaml index 963bf7a05f7fd..230a13678c0d8 100644 --- a/homeassistant/components/aws_s3/quality_scale.yaml +++ b/homeassistant/components/aws_s3/quality_scale.yaml @@ -23,7 +23,9 @@ rules: runtime-data: done test-before-configure: done test-before-setup: done - unique-config-entry: done + unique-config-entry: + status: exempt + comment: Hassfest does not recognize the duplicate prevention logic. Duplicate entries are prevented by checking bucket, endpoint URL, and prefix in the config flow. # Silver action-exceptions: diff --git a/homeassistant/components/aws_s3/strings.json b/homeassistant/components/aws_s3/strings.json index 13d8cc2203b5c..1030ed6702517 100644 --- a/homeassistant/components/aws_s3/strings.json +++ b/homeassistant/components/aws_s3/strings.json @@ -15,12 +15,14 @@ "access_key_id": "Access key ID", "bucket": "Bucket name", "endpoint_url": "Endpoint URL", + "prefix": "Prefix", "secret_access_key": "Secret access key" }, "data_description": { "access_key_id": "Access key ID to connect to AWS S3 API", "bucket": "Bucket must already exist and be writable by the provided credentials.", "endpoint_url": "Endpoint URL provided to [Boto3 Session]({boto3_docs_url}). Region-specific [AWS S3 endpoints]({aws_s3_docs_url}) are available in their docs.", + "prefix": "Folder or prefix to store backups in, for example `backups`", "secret_access_key": "Secret access key to connect to AWS S3 API" }, "title": "Add AWS S3 bucket" diff --git a/tests/components/aws_s3/conftest.py b/tests/components/aws_s3/conftest.py index 423b64023e34f..637fdbd54989d 100644 --- a/tests/components/aws_s3/conftest.py +++ b/tests/components/aws_s3/conftest.py @@ -13,7 +13,7 @@ from homeassistant.components.aws_s3.const import DOMAIN from homeassistant.components.backup import AgentBackup -from .const import USER_INPUT +from .const import CONFIG_ENTRY_DATA from tests.common import MockConfigEntry @@ -76,11 +76,17 @@ async def read(self) -> bytes: @pytest.fixture -def mock_config_entry() -> MockConfigEntry: +def config_entry_extra_data() -> dict: + """Extra config entry data, override in tests to change defaults.""" + return {} + + +@pytest.fixture +def mock_config_entry(config_entry_extra_data: dict) -> MockConfigEntry: """Return the default mocked config entry.""" return MockConfigEntry( entry_id="test", title="test", domain=DOMAIN, - data=USER_INPUT, + data=CONFIG_ENTRY_DATA | config_entry_extra_data, ) diff --git a/tests/components/aws_s3/const.py b/tests/components/aws_s3/const.py index ebffa11d95651..6497457aa1abb 100644 --- a/tests/components/aws_s3/const.py +++ b/tests/components/aws_s3/const.py @@ -4,12 +4,20 @@ CONF_ACCESS_KEY_ID, CONF_BUCKET, CONF_ENDPOINT_URL, + CONF_PREFIX, CONF_SECRET_ACCESS_KEY, ) -USER_INPUT = { +# What gets persisted in the config entry (empty prefix is not stored) +CONFIG_ENTRY_DATA = { CONF_ACCESS_KEY_ID: "TestTestTestTestTest", CONF_SECRET_ACCESS_KEY: "TestTestTestTestTestTestTestTestTestTest", CONF_ENDPOINT_URL: "https://s3.eu-south-1.amazonaws.com", CONF_BUCKET: "test", } + +# What users submit to the flow (can include empty prefix) +USER_INPUT = { + **CONFIG_ENTRY_DATA, + CONF_PREFIX: "", +} diff --git a/tests/components/aws_s3/test_backup.py b/tests/components/aws_s3/test_backup.py index e599a546c0254..4d932772bb910 100644 --- a/tests/components/aws_s3/test_backup.py +++ b/tests/components/aws_s3/test_backup.py @@ -4,7 +4,7 @@ from io import StringIO import json from time import time -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import ANY, AsyncMock, Mock, patch from botocore.exceptions import ConnectTimeoutError import pytest @@ -572,3 +572,185 @@ async def mock_get_object(**kwargs): assert len(backups) == 2 backup_ids = {backup.backup_id for backup in backups} assert backup_ids == {"backup1", "backup2"} + + +@pytest.mark.parametrize( + ("config_entry_extra_data", "expected_paginate_extra_kwargs"), + [ + ({"prefix": "backups/home"}, {"Prefix": "backups/home/"}), + ({}, {}), + ], + ids=["with_prefix", "no_prefix"], +) +async def test_agent_list_backups_parametrized( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + mock_config_entry: MockConfigEntry, + mock_client: MagicMock, + test_backup: AgentBackup, + config_entry_extra_data: dict, + expected_paginate_extra_kwargs: dict, +) -> None: + """Test agent list backups with and without prefix.""" + client = await hass_ws_client(hass) + await client.send_json_auto_id({"type": "backup/info"}) + response = await client.receive_json() + + assert response["success"] + assert response["result"]["agent_errors"] == {} + + # Verify pagination call with expected parameters + mock_client.get_paginator.return_value.paginate.assert_called_with( + **{"Bucket": "test"} | expected_paginate_extra_kwargs + ) + + +@pytest.mark.parametrize( + ("config_entry_extra_data", "expected_key_prefix"), + [ + ({"prefix": "backups/home"}, "backups/home/"), + ({}, ""), + ], + ids=["with_prefix", "no_prefix"], +) +async def test_agent_delete_backup_parametrized( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + mock_client: MagicMock, + mock_config_entry: MockConfigEntry, + test_backup: AgentBackup, + expected_key_prefix: str, +) -> None: + """Test agent delete backup with and without prefix.""" + client = await hass_ws_client(hass) + + await client.send_json_auto_id( + { + "type": "backup/delete", + "backup_id": "23e64aec", + } + ) + response = await client.receive_json() + + assert response["success"] + assert response["result"] == {"agent_errors": {}} + + tar_filename, metadata_filename = suggested_filenames(test_backup) + + expected_tar_key = f"{expected_key_prefix}{tar_filename}" + expected_metadata_key = f"{expected_key_prefix}{metadata_filename}" + + mock_client.delete_object.assert_any_call(Bucket="test", Key=expected_tar_key) + mock_client.delete_object.assert_any_call(Bucket="test", Key=expected_metadata_key) + + +@pytest.mark.parametrize( + ("config_entry_extra_data", "expected_key_prefix"), + [ + ({"prefix": "backups/home"}, "backups/home/"), + ({}, ""), + ], + ids=["with_prefix", "no_prefix"], +) +async def test_agent_upload_backup_parametrized( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_client: MagicMock, + mock_config_entry: MockConfigEntry, + test_backup: AgentBackup, + expected_key_prefix: str, +) -> None: + """Test agent upload backup with and without prefix.""" + client = await hass_client() + with ( + patch( + "homeassistant.components.backup.manager.BackupManager.async_get_backup", + return_value=test_backup, + ), + patch( + "homeassistant.components.backup.manager.read_backup", + return_value=test_backup, + ), + patch("pathlib.Path.open") as mocked_open, + ): + # we must emit at least two chunks + # the "appendix" chunk triggers the upload of the final buffer part + mocked_open.return_value.read = Mock( + side_effect=[ + b"a" * test_backup.size, + b"appendix", + b"", + ] + ) + resp = await client.post( + f"/api/backup/upload?agent_id={DOMAIN}.{mock_config_entry.entry_id}", + data={"file": StringIO("test")}, + ) + + assert resp.status == 201 + + tar_filename, metadata_filename = suggested_filenames(test_backup) + + expected_tar_key = f"{expected_key_prefix}{tar_filename}" + expected_metadata_key = f"{expected_key_prefix}{metadata_filename}" + + if test_backup.size < MULTIPART_MIN_PART_SIZE_BYTES: + mock_client.put_object.assert_any_call( + Bucket="test", Key=expected_tar_key, Body=ANY + ) + mock_client.put_object.assert_any_call( + Bucket="test", Key=expected_metadata_key, Body=ANY + ) + else: + mock_client.create_multipart_upload.assert_called_with( + Bucket="test", Key=expected_tar_key + ) + mock_client.upload_part.assert_any_call( + Bucket="test", + Key=expected_tar_key, + PartNumber=1, + UploadId="upload_id", + Body=ANY, + ) + mock_client.complete_multipart_upload.assert_called_with( + Bucket="test", + Key=expected_tar_key, + UploadId="upload_id", + MultipartUpload=ANY, + ) + mock_client.put_object.assert_called_with( + Bucket="test", Key=expected_metadata_key, Body=ANY + ) + + +@pytest.mark.parametrize( + ("config_entry_extra_data", "expected_key_prefix"), + [ + ({"prefix": "backups/home"}, "backups/home/"), + ({}, ""), + ], + ids=["with_prefix", "no_prefix"], +) +async def test_agent_download_backup_parametrized( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_client: MagicMock, + mock_config_entry: MockConfigEntry, + test_backup: AgentBackup, + expected_key_prefix: str, +) -> None: + """Test agent download backup with and without prefix.""" + client = await hass_client() + backup_id = "23e64aec" + + resp = await client.get( + f"/api/backup/download/{backup_id}?agent_id={DOMAIN}.{mock_config_entry.entry_id}" + ) + assert resp.status == 200 + assert await resp.content.read() == b"backup data" + + tar_filename, _ = suggested_filenames(test_backup) + + expected_tar_key = f"{expected_key_prefix}{tar_filename}" + + mock_client.get_object.assert_any_call(Bucket="test", Key=expected_tar_key) diff --git a/tests/components/aws_s3/test_config_flow.py b/tests/components/aws_s3/test_config_flow.py index 58c634f691788..3ada3b38b7bf5 100644 --- a/tests/components/aws_s3/test_config_flow.py +++ b/tests/components/aws_s3/test_config_flow.py @@ -10,11 +10,16 @@ import pytest from homeassistant import config_entries -from homeassistant.components.aws_s3.const import CONF_BUCKET, CONF_ENDPOINT_URL, DOMAIN +from homeassistant.components.aws_s3.const import ( + CONF_BUCKET, + CONF_ENDPOINT_URL, + CONF_PREFIX, + DOMAIN, +) from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .const import USER_INPUT +from .const import CONFIG_ENTRY_DATA, USER_INPUT from tests.common import MockConfigEntry @@ -38,12 +43,50 @@ async def _async_start_flow( ) -async def test_flow(hass: HomeAssistant) -> None: - """Test config flow.""" - result = await _async_start_flow(hass) +@pytest.mark.parametrize( + ("user_input", "expected_title", "expected_data"), + [ + (USER_INPUT, "test", CONFIG_ENTRY_DATA), + ( + USER_INPUT | {CONF_PREFIX: "my-prefix"}, + "test - my-prefix", + USER_INPUT | {CONF_PREFIX: "my-prefix"}, + ), + ( + USER_INPUT | {CONF_PREFIX: "/backups/"}, + "test - backups", + CONFIG_ENTRY_DATA | {CONF_PREFIX: "backups"}, + ), + ( + USER_INPUT | {CONF_PREFIX: "/"}, + "test", + CONFIG_ENTRY_DATA, + ), + ( + USER_INPUT | {CONF_PREFIX: "my-prefix/"}, + "test - my-prefix", + CONFIG_ENTRY_DATA | {CONF_PREFIX: "my-prefix"}, + ), + ], + ids=[ + "no_prefix", + "with_prefix", + "with_leading_and_trailing_slash", + "only_slash", + "with_trailing_slash", + ], +) +async def test_flow( + hass: HomeAssistant, + user_input: dict, + expected_title: str, + expected_data: dict, +) -> None: + """Test config flow with and without prefix, including prefix normalization.""" + result = await _async_start_flow(hass, user_input) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "test" - assert result["data"] == USER_INPUT + assert result["title"] == expected_title + assert result["data"] == expected_data @pytest.mark.parametrize( @@ -83,7 +126,7 @@ async def test_flow_create_client_errors( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "test" - assert result["data"] == USER_INPUT + assert result["data"] == CONFIG_ENTRY_DATA async def test_flow_head_bucket_error( @@ -108,7 +151,7 @@ async def test_flow_head_bucket_error( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "test" - assert result["data"] == USER_INPUT + assert result["data"] == CONFIG_ENTRY_DATA async def test_abort_if_already_configured( @@ -147,4 +190,48 @@ async def test_flow_create_not_aws_endpoint( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "test" - assert result["data"] == USER_INPUT + assert result["data"] == CONFIG_ENTRY_DATA + + +async def test_abort_if_already_configured_with_same_prefix( + hass: HomeAssistant, + mock_client: AsyncMock, +) -> None: + """Test we abort if same bucket, endpoint, and prefix are already configured.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG_ENTRY_DATA | {CONF_PREFIX: "my-prefix"}, + ) + entry.add_to_hass(hass) + result = await _async_start_flow(hass, USER_INPUT | {CONF_PREFIX: "my-prefix"}) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_abort_if_entry_without_prefix( + hass: HomeAssistant, + mock_client: AsyncMock, +) -> None: + """Test we abort if an entry without prefix matches bucket and endpoint.""" + # Entry without CONF_PREFIX in data (empty prefix is not persisted) + entry = MockConfigEntry(domain=DOMAIN, data=CONFIG_ENTRY_DATA) + entry.add_to_hass(hass) + # Try to configure the same bucket/endpoint with an empty prefix + result = await _async_start_flow(hass, USER_INPUT) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_no_abort_if_different_prefix( + hass: HomeAssistant, + mock_client: AsyncMock, +) -> None: + """Test we do not abort when same bucket+endpoint but a different prefix is used.""" + entry = MockConfigEntry( + domain=DOMAIN, + data=CONFIG_ENTRY_DATA | {CONF_PREFIX: "prefix-a"}, + ) + entry.add_to_hass(hass) + result = await _async_start_flow(hass, USER_INPUT | {CONF_PREFIX: "prefix-b"}) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_PREFIX] == "prefix-b" diff --git a/tests/components/aws_s3/test_sensor.py b/tests/components/aws_s3/test_sensor.py index 0af9192ba6c0e..a17e48428ca62 100644 --- a/tests/components/aws_s3/test_sensor.py +++ b/tests/components/aws_s3/test_sensor.py @@ -7,6 +7,7 @@ from botocore.exceptions import BotoCoreError from freezegun.api import FrozenDateTimeFactory +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.aws_s3.coordinator import SCAN_INTERVAL @@ -74,14 +75,26 @@ async def test_sensor_availability( assert state.state != STATE_UNAVAILABLE +@pytest.mark.parametrize( + ("config_entry_extra_data", "expected_pagination_call"), + [ + ({}, {"Bucket": "test"}), + ( + {"prefix": "backups/home"}, + {"Bucket": "test", "Prefix": "backups/home/"}, + ), + ], +) async def test_calculate_backups_size( hass: HomeAssistant, mock_client: AsyncMock, mock_config_entry: MockConfigEntry, freezer: FrozenDateTimeFactory, test_backup: AgentBackup, + config_entry_extra_data: dict, + expected_pagination_call: dict, ) -> None: - """Test the total size of backups calculation.""" + """Test the total size of backups calculation with and without prefix.""" mock_client.get_paginator.return_value.paginate.return_value.__aiter__.return_value = [ {"Contents": []} ] @@ -111,3 +124,8 @@ async def test_calculate_backups_size( assert (state := hass.states.get("sensor.bucket_test_total_size_of_backups")) assert float(state.state) > 0 + + # Verify prefix was used in API call if expected + mock_client.get_paginator.return_value.paginate.assert_called_with( + **expected_pagination_call, + ) From 88b276f3a447fbd060510717999cbfe6284b7e93 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 26 Feb 2026 01:43:44 -0500 Subject: [PATCH 3/5] Simplify Anthropic integration name (#164124) Co-authored-by: Claude --- homeassistant/components/anthropic/manifest.json | 2 +- homeassistant/generated/integrations.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/anthropic/manifest.json b/homeassistant/components/anthropic/manifest.json index 8b4aaa3087f6f..7ed34c517d124 100644 --- a/homeassistant/components/anthropic/manifest.json +++ b/homeassistant/components/anthropic/manifest.json @@ -1,6 +1,6 @@ { "domain": "anthropic", - "name": "Anthropic Conversation", + "name": "Anthropic", "after_dependencies": ["assist_pipeline", "intent"], "codeowners": ["@Shulyaka"], "config_flow": true, diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 71ef0d7be6fcd..e3890c5187747 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -381,7 +381,7 @@ "iot_class": "local_push" }, "anthropic": { - "name": "Anthropic Conversation", + "name": "Anthropic", "integration_type": "service", "config_flow": true, "iot_class": "cloud_polling" From eaae64fa122e7b0f69d49554e9c32e38ed3b37d0 Mon Sep 17 00:00:00 2001 From: mettolen <1007649+mettolen@users.noreply.github.com> Date: Thu, 26 Feb 2026 08:44:19 +0200 Subject: [PATCH 4/5] Remove error translation placeholders from Saunum (#164121) --- homeassistant/components/saunum/climate.py | 1 - homeassistant/components/saunum/coordinator.py | 1 - homeassistant/components/saunum/strings.json | 4 ++-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/saunum/climate.py b/homeassistant/components/saunum/climate.py index a6e55ccfe34ce..411d456c3c7b2 100644 --- a/homeassistant/components/saunum/climate.py +++ b/homeassistant/components/saunum/climate.py @@ -269,7 +269,6 @@ async def async_start_session( raise HomeAssistantError( translation_domain=DOMAIN, translation_key="start_session_failed", - translation_placeholders={"error": str(err)}, ) from err await self.coordinator.async_request_refresh() diff --git a/homeassistant/components/saunum/coordinator.py b/homeassistant/components/saunum/coordinator.py index f4d90c24a7c53..540da3b55f4ba 100644 --- a/homeassistant/components/saunum/coordinator.py +++ b/homeassistant/components/saunum/coordinator.py @@ -47,5 +47,4 @@ async def _async_update_data(self) -> SaunumData: raise UpdateFailed( translation_domain=DOMAIN, translation_key="communication_error", - translation_placeholders={"error": str(err)}, ) from err diff --git a/homeassistant/components/saunum/strings.json b/homeassistant/components/saunum/strings.json index ca0631337b35e..4e3645b66991b 100644 --- a/homeassistant/components/saunum/strings.json +++ b/homeassistant/components/saunum/strings.json @@ -88,7 +88,7 @@ }, "exceptions": { "communication_error": { - "message": "Communication error: {error}" + "message": "Communication error with sauna control unit" }, "door_open": { "message": "Cannot start sauna session when sauna door is open" @@ -130,7 +130,7 @@ "message": "Failed to set temperature to {temperature}" }, "start_session_failed": { - "message": "Failed to start sauna session: {error}" + "message": "Failed to start sauna session" } }, "options": { From 31f796143761cf42b43f76f97a8de52dddebcfd0 Mon Sep 17 00:00:00 2001 From: Amit Finkelstein Date: Thu, 26 Feb 2026 09:04:58 +0200 Subject: [PATCH 5/5] Add HassOS "mount_reload" action (#155996) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Shay Levy Co-authored-by: AbĂ­lio Costa Co-authored-by: Erik Montnemery --- homeassistant/components/hassio/__init__.py | 54 ++++++ homeassistant/components/hassio/icons.json | 3 + homeassistant/components/hassio/services.yaml | 10 ++ homeassistant/components/hassio/strings.json | 21 +++ tests/components/hassio/test_init.py | 160 +++++++++++++++++- 5 files changed, 247 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 9f164a3d8f1eb..ed7478422c912 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -21,6 +21,7 @@ from homeassistant.components.http import StaticPathConfig from homeassistant.config_entries import SOURCE_SYSTEM, ConfigEntry from homeassistant.const import ( + ATTR_DEVICE_ID, ATTR_NAME, EVENT_CORE_CONFIG_UPDATE, HASSIO_USER_NAME, @@ -34,11 +35,13 @@ async_get_hass_or_none, callback, ) +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import ( config_validation as cv, device_registry as dr, discovery_flow, issue_registry as ir, + selector, ) from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.event import async_call_later @@ -92,6 +95,7 @@ DATA_SUPERVISOR_INFO, DOMAIN, HASSIO_UPDATE_INTERVAL, + SupervisorEntityModel, ) from .coordinator import ( HassioDataUpdateCoordinator, @@ -147,6 +151,7 @@ SERVICE_BACKUP_PARTIAL = "backup_partial" SERVICE_RESTORE_FULL = "restore_full" SERVICE_RESTORE_PARTIAL = "restore_partial" +SERVICE_MOUNT_RELOAD = "mount_reload" VALID_ADDON_SLUG = vol.Match(re.compile(r"^[-_.A-Za-z0-9]+$")) @@ -229,6 +234,19 @@ def valid_addon(value: Any) -> str: } ) +SCHEMA_MOUNT_RELOAD = vol.Schema( + { + vol.Required(ATTR_DEVICE_ID): selector.DeviceSelector( + selector.DeviceSelectorConfig( + filter=selector.DeviceFilterSelectorConfig( + integration=DOMAIN, + model=SupervisorEntityModel.MOUNT, + ) + ) + ) + } +) + def _is_32_bit() -> bool: size = struct.calcsize("P") @@ -444,6 +462,42 @@ async def async_service_handler(service: ServiceCall) -> None: DOMAIN, service, async_service_handler, schema=settings.schema ) + dev_reg = dr.async_get(hass) + + async def async_mount_reload(service: ServiceCall) -> None: + """Handle service calls for Hass.io.""" + coordinator: HassioDataUpdateCoordinator | None = None + + if (device := dev_reg.async_get(service.data[ATTR_DEVICE_ID])) is None: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="mount_reload_unknown_device_id", + ) + + if ( + device.name is None + or device.model != SupervisorEntityModel.MOUNT + or (coordinator := hass.data.get(ADDONS_COORDINATOR)) is None + or coordinator.entry_id not in device.config_entries + ): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="mount_reload_invalid_device", + ) + + try: + await supervisor_client.mounts.reload_mount(device.name) + except SupervisorError as error: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="mount_reload_error", + translation_placeholders={"name": device.name, "error": str(error)}, + ) from error + + hass.services.async_register( + DOMAIN, SERVICE_MOUNT_RELOAD, async_mount_reload, SCHEMA_MOUNT_RELOAD + ) + async def update_info_data(_: datetime | None = None) -> None: """Update last available supervisor information.""" supervisor_client = get_supervisor_client(hass) diff --git a/homeassistant/components/hassio/icons.json b/homeassistant/components/hassio/icons.json index 49111914c81dc..0037409c6d3a9 100644 --- a/homeassistant/components/hassio/icons.json +++ b/homeassistant/components/hassio/icons.json @@ -46,6 +46,9 @@ "host_shutdown": { "service": "mdi:power" }, + "mount_reload": { + "service": "mdi:reload" + }, "restore_full": { "service": "mdi:backup-restore" }, diff --git a/homeassistant/components/hassio/services.yaml b/homeassistant/components/hassio/services.yaml index 0d00255264e75..6aa279f9a42a7 100644 --- a/homeassistant/components/hassio/services.yaml +++ b/homeassistant/components/hassio/services.yaml @@ -165,3 +165,13 @@ restore_partial: example: "password" selector: text: + +mount_reload: + fields: + device_id: + required: true + selector: + device: + filter: + integration: hassio + model: Home Assistant Mount diff --git a/homeassistant/components/hassio/strings.json b/homeassistant/components/hassio/strings.json index e480fb794f4c7..b9a4ec0fa2de3 100644 --- a/homeassistant/components/hassio/strings.json +++ b/homeassistant/components/hassio/strings.json @@ -43,6 +43,17 @@ } } }, + "exceptions": { + "mount_reload_error": { + "message": "Failed to reload mount {name}: {error}" + }, + "mount_reload_invalid_device": { + "message": "Device is not a supervisor mount point" + }, + "mount_reload_unknown_device_id": { + "message": "Device ID not found" + } + }, "issues": { "issue_addon_boot_fail": { "fix_flow": { @@ -456,6 +467,16 @@ "description": "Powers off the host system.", "name": "Power off the host system" }, + "mount_reload": { + "description": "Reloads a network storage mount.", + "fields": { + "device_id": { + "description": "The device ID of the network storage mount to reload.", + "name": "Device ID" + } + }, + "name": "Reload network storage mount" + }, "restore_full": { "description": "Restores from full backup.", "fields": { diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index b6295feda10db..0262fd73ae773 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -2,17 +2,25 @@ from datetime import timedelta import os +from pathlib import PurePath from typing import Any from unittest.mock import AsyncMock, patch from aiohasupervisor import SupervisorError from aiohasupervisor.models import AddonsStats +from aiohasupervisor.models.mounts import ( + CIFSMountResponse, + MountsInfo, + MountState, + MountType, + MountUsage, +) from freezegun.api import FrozenDateTimeFactory import pytest from voluptuous import Invalid from homeassistant.auth.const import GROUP_ID_ADMIN -from homeassistant.components import frontend +from homeassistant.components import frontend, hassio from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN from homeassistant.components.hassio import ( ADDONS_COORDINATOR, @@ -31,10 +39,12 @@ ) from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import device_registry as dr, issue_registry as ir from homeassistant.helpers.hassio import is_hassio from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util +from homeassistant.util.yaml import load_yaml_dict from tests.common import MockConfigEntry, async_fire_time_changed from tests.test_util.aiohttp import AiohttpClientMocker @@ -514,6 +524,7 @@ async def test_service_register(hass: HomeAssistant) -> None: assert hass.services.has_service("hassio", "backup_partial") assert hass.services.has_service("hassio", "restore_full") assert hass.services.has_service("hassio", "restore_partial") + assert hass.services.has_service("hassio", "mount_reload") @pytest.mark.parametrize( @@ -1484,3 +1495,150 @@ async def test_deprecated_installation_issue_supported_board( await hass.async_block_till_done() assert len(issue_registry.issues) == 0 + + +async def mount_reload_test_setup( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> dr.DeviceEntry: + """Set up mount reload test and return the device entry.""" + supervisor_client.mounts.info = AsyncMock( + return_value=MountsInfo( + default_backup_mount=None, + mounts=[ + CIFSMountResponse( + share="files", + server="1.2.3.4", + name="NAS", + type=MountType.CIFS, + usage=MountUsage.SHARE, + read_only=False, + state=MountState.ACTIVE, + user_path=PurePath("/share/nas"), + ) + ], + ) + ) + + with patch.dict(os.environ, MOCK_ENVIRON): + config_entry = MockConfigEntry(domain=DOMAIN, data={}, unique_id=DOMAIN) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + device = device_registry.async_get_device(identifiers={(DOMAIN, "mount_NAS")}) + assert device is not None + return device + + +async def test_mount_reload_action( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test reload_mount service call.""" + device = await mount_reload_test_setup(hass, device_registry, supervisor_client) + await hass.services.async_call( + "hassio", "mount_reload", {"device_id": device.id}, blocking=True + ) + supervisor_client.mounts.reload_mount.assert_awaited_once_with("NAS") + + +async def test_mount_reload_action_failure( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test reload_mount service call failure.""" + device = await mount_reload_test_setup(hass, device_registry, supervisor_client) + supervisor_client.mounts.reload_mount = AsyncMock( + side_effect=SupervisorError("test failure") + ) + with pytest.raises(HomeAssistantError) as exc: + await hass.services.async_call( + "hassio", "mount_reload", {"device_id": device.id}, blocking=True + ) + assert str(exc.value) == "Failed to reload mount NAS: test failure" + + +async def test_mount_reload_unknown_device_id( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test reload_mount with unknown device ID.""" + await mount_reload_test_setup(hass, device_registry, supervisor_client) + with pytest.raises(ServiceValidationError) as exc: + await hass.services.async_call( + "hassio", "mount_reload", {"device_id": "1234"}, blocking=True + ) + assert str(exc.value) == "Device ID not found" + + +async def test_mount_reload_no_name( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test reload_mount with an unnamed device.""" + device = await mount_reload_test_setup(hass, device_registry, supervisor_client) + device_registry.async_update_device(device.id, name=None) + with pytest.raises(ServiceValidationError) as exc: + await hass.services.async_call( + "hassio", "mount_reload", {"device_id": device.id}, blocking=True + ) + assert str(exc.value) == "Device is not a supervisor mount point" + + +async def test_mount_reload_invalid_model( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test reload_mount with an invalid model.""" + device = await mount_reload_test_setup(hass, device_registry, supervisor_client) + device_registry.async_update_device(device.id, model=None) + with pytest.raises(ServiceValidationError) as exc: + await hass.services.async_call( + "hassio", "mount_reload", {"device_id": device.id}, blocking=True + ) + assert str(exc.value) == "Device is not a supervisor mount point" + + +async def test_mount_reload_not_supervisor_device( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test reload_mount with a device not belonging to the supervisor.""" + device = await mount_reload_test_setup(hass, device_registry, supervisor_client) + config_entry = MockConfigEntry() + config_entry.add_to_hass(hass) + device2 = device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + identifiers={("test", "test")}, + name=device.name, + model=device.model, + ) + with pytest.raises(ServiceValidationError) as exc: + await hass.services.async_call( + "hassio", "mount_reload", {"device_id": device2.id}, blocking=True + ) + assert str(exc.value) == "Device is not a supervisor mount point" + + +async def test_mount_reload_selector_matches_device_name( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + supervisor_client: AsyncMock, +) -> None: + """Test that the model name in the selector of mount reload is valid.""" + device = await mount_reload_test_setup(hass, device_registry, supervisor_client) + services = load_yaml_dict(f"{hassio.__path__[0]}/services.yaml") + assert ( + services["mount_reload"]["fields"]["device_id"]["selector"]["device"]["filter"][ + "model" + ] + == device.model + )