From 5ce4763fd7a74123f26fd461a338662cc0469df3 Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Fri, 6 Nov 2026 20:22:54 +0000 Subject: [PATCH] Use the common read error helper in file read_file (#190975) --- homeassistant/components/file/services.py | 13 ++------ homeassistant/components/file/strings.json | 6 ---- tests/components/file/test_services.py | 35 +++++++++++++++++----- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/file/services.py b/homeassistant/components/file/services.py index 67c1a896ed67..5f22f726f1a3 100644 --- a/homeassistant/components/file/services.py +++ b/homeassistant/components/file/services.py @@ -10,6 +10,7 @@ import yaml from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.os_error import os_read_error from homeassistant.helpers.service import async_register_admin_service from .const import DOMAIN @@ -78,18 +79,8 @@ def read_file(call: ServiceCall) -> dict: try: with open(file_name, encoding="utf-8") as file: file_content = file.read() - except FileNotFoundError as err: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="file_not_found", - translation_placeholders={"filename": file_name}, - ) from err except OSError as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="file_read_error", - translation_placeholders={"filename": file_name}, - ) from err + raise os_read_error(err, file_name) from err loader, error_type = ENCODING_LOADERS[file_encoding] try: diff --git a/homeassistant/components/file/strings.json b/homeassistant/components/file/strings.json index fea623aa6a71..c3f7760b92fd 100644 --- a/homeassistant/components/file/strings.json +++ b/homeassistant/components/file/strings.json @@ -49,12 +49,6 @@ "file_decoding": { "message": "Cannot read file {filename} as {encoding}." }, - "file_not_found": { - "message": "File {filename} not found." - }, - "file_read_error": { - "message": "Error reading {filename}." - }, "no_access_to_path": { "message": "Cannot read {filename}, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`" }, diff --git a/tests/components/file/test_services.py b/tests/components/file/test_services.py index 640f527948e1..4f1413adc099 100644 --- a/tests/components/file/test_services.py +++ b/tests/components/file/test_services.py @@ -1,6 +1,8 @@ """The tests for the notify file platform.""" -from unittest.mock import MagicMock +import errno +import os +from unittest.mock import MagicMock, patch import pytest from syrupy.assertion import SnapshotAssertion @@ -121,16 +123,33 @@ async def test_read_file_decoding_error( assert hae.value.translation_domain == DOMAIN -async def test_read_file_dne( +@pytest.mark.parametrize( + ("error_no", "translation_key"), + [ + pytest.param(errno.ENOENT, "os_read_not_found", id="not_found"), + pytest.param(errno.EACCES, "os_read_permission_denied", id="permission"), + pytest.param(errno.EISDIR, "os_read_is_directory", id="is_directory"), + pytest.param(errno.EIO, "os_read_error", id="fallback"), + ], +) +async def test_read_file_os_errors( hass: HomeAssistant, mock_is_allowed_path: MagicMock, setup_ha_file_integration, + error_no: int, + translation_key: str, ) -> None: - """Test handling error if file does not exist.""" - file_name = "tests/components/file/fixtures/file_dne.yaml" + """Test handling the errors of reading the file.""" + file_name = "tests/components/file/fixtures/file_read.yaml" - with pytest.raises(HomeAssistantError) as hae: - _ = await hass.services.async_call( + with ( + patch( + "homeassistant.components.file.services.open", + side_effect=OSError(error_no, os.strerror(error_no)), + ), + pytest.raises(HomeAssistantError) as hae, + ): + await hass.services.async_call( DOMAIN, FileService.READ_FILE, { @@ -140,4 +159,6 @@ async def test_read_file_dne( blocking=True, return_response=True, ) - assert file_name in str(hae.value) + assert hae.value.translation_domain == "homeassistant" + assert hae.value.translation_key == translation_key + assert hae.value.translation_placeholders == {"path": file_name}