Raise HomeAssistantError from camera snapshot service (#137051)
* Raise HomeAssistantError from camera snapshot service * Improve error message --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>pull/137390/head
parent
829a6271af
commit
a4eab35e01
|
@ -1175,12 +1175,17 @@ async def async_handle_snapshot_service(
|
||||||
f"Cannot write `{snapshot_file}`, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`"
|
f"Cannot write `{snapshot_file}`, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`"
|
||||||
)
|
)
|
||||||
|
|
||||||
async with asyncio.timeout(CAMERA_IMAGE_TIMEOUT):
|
try:
|
||||||
image = (
|
async with asyncio.timeout(CAMERA_IMAGE_TIMEOUT):
|
||||||
await _async_get_stream_image(camera, wait_for_next_keyframe=True)
|
image = (
|
||||||
if camera.use_stream_for_stills
|
await _async_get_stream_image(camera, wait_for_next_keyframe=True)
|
||||||
else await camera.async_camera_image()
|
if camera.use_stream_for_stills
|
||||||
)
|
else await camera.async_camera_image()
|
||||||
|
)
|
||||||
|
except TimeoutError as err:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
f"Unable to get snapshot: Timed out after {CAMERA_IMAGE_TIMEOUT} seconds"
|
||||||
|
) from err
|
||||||
|
|
||||||
if image is None:
|
if image is None:
|
||||||
return
|
return
|
||||||
|
@ -1194,7 +1199,7 @@ async def async_handle_snapshot_service(
|
||||||
try:
|
try:
|
||||||
await hass.async_add_executor_job(_write_image, snapshot_file, image)
|
await hass.async_add_executor_job(_write_image, snapshot_file, image)
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
_LOGGER.error("Can't write image to file: %s", err)
|
raise HomeAssistantError(f"Can't write image to file: {err}") from err
|
||||||
|
|
||||||
|
|
||||||
async def async_handle_play_stream_service(
|
async def async_handle_play_stream_service(
|
||||||
|
|
|
@ -301,13 +301,24 @@ async def test_snapshot_service_not_allowed_path(hass: HomeAssistant) -> None:
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_camera")
|
@pytest.mark.usefixtures("mock_camera")
|
||||||
async def test_snapshot_service_os_error(
|
@pytest.mark.parametrize(
|
||||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
("target", "side_effect"),
|
||||||
|
[
|
||||||
|
("homeassistant.components.camera.os.makedirs", OSError),
|
||||||
|
(
|
||||||
|
"homeassistant.components.demo.camera.DemoCamera.async_camera_image",
|
||||||
|
TimeoutError,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_snapshot_service_error(
|
||||||
|
hass: HomeAssistant, target: str, side_effect: Exception
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test snapshot service with os error."""
|
"""Test snapshot service with error."""
|
||||||
with (
|
with (
|
||||||
patch.object(hass.config, "is_allowed_path", return_value=True),
|
patch.object(hass.config, "is_allowed_path", return_value=True),
|
||||||
patch("homeassistant.components.camera.os.makedirs", side_effect=OSError),
|
patch(target, side_effect=side_effect),
|
||||||
|
pytest.raises(HomeAssistantError),
|
||||||
):
|
):
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
camera.DOMAIN,
|
camera.DOMAIN,
|
||||||
|
@ -319,8 +330,6 @@ async def test_snapshot_service_os_error(
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert "Can't write image to file:" in caplog.text
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_camera", "mock_stream")
|
@pytest.mark.usefixtures("mock_camera", "mock_stream")
|
||||||
async def test_websocket_stream_no_source(
|
async def test_websocket_stream_no_source(
|
||||||
|
|
Loading…
Reference in New Issue