Use the common read error helper in file read_file (#190975)

This commit is contained in:
Christopher Pruijsen
2026-11-06 21:22:54 +01:00
committed by GitHub
parent e33039d165
commit 5ce4763fd7
3 changed files with 30 additions and 24 deletions
+2 -11
View File
@@ -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:
@@ -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`"
},
+28 -7
View File
@@ -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}