diff --git a/homeassistant/components/collection_image/image.py b/homeassistant/components/collection_image/image.py index 1dd60c7f4a2d..1a013034a858 100644 --- a/homeassistant/components/collection_image/image.py +++ b/homeassistant/components/collection_image/image.py @@ -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 diff --git a/homeassistant/components/collection_image/strings.json b/homeassistant/components/collection_image/strings.json index d78f3b1cb3ff..d6860a61b63f 100644 --- a/homeassistant/components/collection_image/strings.json +++ b/homeassistant/components/collection_image/strings.json @@ -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}." }, diff --git a/tests/components/collection_image/test_image.py b/tests/components/collection_image/test_image.py index 5f0ebb5dea0d..d8f7ba8b65dd 100644 --- a/tests/components/collection_image/test_image.py +++ b/tests/components/collection_image/test_image.py @@ -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