Use the common read error helper in collection_image (#185748)

This commit is contained in:
karwosts
2026-10-11 08:35:47 +02:00
committed by GitHub
parent 27eeeb6fde
commit cdfdaeab28
3 changed files with 25 additions and 25 deletions
@@ -26,6 +26,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.os_error import os_read_error
from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.typing import UNDEFINED
from homeassistant.util import dt as dt_util
@@ -277,13 +278,6 @@ class CollectionImageImageEntity(ImageEntity):
try:
return self.path.read_bytes()
except OSError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="image_read_error",
translation_placeholders={
"path": str(self.path),
"error": str(err),
},
) from err
raise os_read_error(err, str(self.path)) from err
return None
@@ -28,9 +28,6 @@
}
},
"exceptions": {
"image_read_error": {
"message": "Error reading image from {path}: {error}"
},
"invalid_media_type": {
"message": "Selected media {id} has type {mime_type}, which is not an image, and cannot be shown by {entity}."
},
+23 -14
View File
@@ -1,6 +1,8 @@
"""The tests for the Collection Image image platform."""
import errno
from http import HTTPStatus
import os
from pathlib import Path
from unittest.mock import AsyncMock, patch
@@ -338,21 +340,24 @@ async def test_unresolvable(
await _verify_path_image(hass, hass_client)
@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"),
],
)
@pytest.mark.usefixtures("mock_media_source")
async def test_image_file_read_error(
hass: HomeAssistant,
config_entry: MockConfigEntry,
media_source_state: MediaSourceState,
hass_client: ClientSessionGenerator,
error_no: int,
translation_key: str,
) -> None:
"""Test that a file read error is surfaced when serving the image."""
missing_path = Path(__file__).parent / "does_not_exist.png"
media_source_state.resolve_results[MOCK_MEDIA_IMAGE_URI_1] = PlayMedia(
url="",
mime_type="image/png",
path=missing_path,
)
with (
freeze_time(TEST_TIME),
):
@@ -364,13 +369,17 @@ async def test_image_file_read_error(
state = hass.states.get(DEFAULT_ENTITY_ID)
assert state and state.state == TEST_TIME
with pytest.raises(HomeAssistantError) as exc_info:
await async_get_image(hass, DEFAULT_ENTITY_ID)
assert exc_info.value.translation_key == "image_read_error"
assert exc_info.value.translation_placeholders["path"] == str(missing_path)
client = await hass_client()
resp = await client.get(f"/api/image_proxy/{DEFAULT_ENTITY_ID}")
with patch.object(
Path, "read_bytes", side_effect=OSError(error_no, os.strerror(error_no))
):
with pytest.raises(HomeAssistantError) as exc_info:
await async_get_image(hass, DEFAULT_ENTITY_ID)
resp = await client.get(f"/api/image_proxy/{DEFAULT_ENTITY_ID}")
assert exc_info.value.translation_domain == "homeassistant"
assert exc_info.value.translation_key == translation_key
assert exc_info.value.translation_placeholders == {"path": str(TEST_IMAGE)}
assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR