Fix swallowed exceptions in prosegur action handlers (#183247)

This commit is contained in:
Vasily G.
2026-09-27 07:08:07 +02:00
committed by GitHub
parent b44de9cbde
commit 568a2544cd
3 changed files with 33 additions and 41 deletions
+11 -12
View File
@@ -9,6 +9,7 @@ from pyprosegur.installation import Camera as InstallationCamera, Installation
from homeassistant.components.camera import Camera
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import (
AddConfigEntryEntitiesCallback,
@@ -82,12 +83,12 @@ class ProsegurCamera(Camera):
_LOGGER.debug("Get image for %s", self._camera.description)
try:
return await self._installation.get_image(self._auth, self._camera.id)
# pylint: disable-next=home-assistant-action-swallowed-exception
except ProsegurException as err:
_LOGGER.error("Image %s doesn't exist: %s", self._camera.description, err)
return None
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="image_not_available",
translation_placeholders={"camera": self._camera.description},
) from err
async def async_request_image(self):
"""Request new image from the camera."""
@@ -95,11 +96,9 @@ class ProsegurCamera(Camera):
_LOGGER.debug("Request image for %s", self._camera.description)
try:
await self._installation.request_image(self._auth, self._camera.id)
# pylint: disable-next=home-assistant-action-swallowed-exception
except ProsegurException as err:
_LOGGER.error(
"Could not request image from camera %s: %s",
self._camera.description,
err,
)
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="request_image_failed",
translation_placeholders={"camera": self._camera.description},
) from err
@@ -30,6 +30,14 @@
}
}
},
"exceptions": {
"image_not_available": {
"message": "Unable to get image from camera {camera}"
},
"request_image_failed": {
"message": "Unable to request a new image from camera {camera}"
}
},
"services": {
"request_image": {
"description": "Requests a new image from a Prosegur camera.",
+14 -29
View File
@@ -1,7 +1,6 @@
"""The camera tests for the prosegur platform."""
import logging
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, MagicMock
from pyprosegur.exceptions import ProsegurException
import pytest
@@ -22,28 +21,17 @@ async def test_camera(hass: HomeAssistant, init_integration) -> None:
assert image == Image(content_type="image/jpeg", content=b"ABC")
async def test_camera_fail(
hass: HomeAssistant,
init_integration,
mock_install,
caplog: pytest.LogCaptureFixture,
) -> None:
@pytest.mark.usefixtures("init_integration")
async def test_camera_fail(hass: HomeAssistant, mock_install: MagicMock) -> None:
"""Test prosegur get_image fails."""
mock_install.get_image = AsyncMock(
return_value=b"ABC", side_effect=ProsegurException()
)
mock_install.get_image = AsyncMock(side_effect=ProsegurException())
with (
caplog.at_level(logging.ERROR, logger="homeassistant.components.prosegur"),
pytest.raises(HomeAssistantError) as exc,
with pytest.raises(
HomeAssistantError, match="Unable to get image from camera test_cam"
):
await camera.async_get_image(hass, "camera.contract_1234abcd_test_cam")
assert "Unable to get image" in str(exc.value)
assert "Image test_cam doesn't exist" in caplog.text
async def test_request_image(
hass: HomeAssistant, init_integration, mock_install
@@ -60,24 +48,21 @@ async def test_request_image(
assert mock_install.request_image.called
async def test_request_image_fail(
hass: HomeAssistant,
init_integration,
mock_install,
caplog: pytest.LogCaptureFixture,
) -> None:
@pytest.mark.usefixtures("init_integration")
async def test_request_image_fail(hass: HomeAssistant, mock_install: MagicMock) -> None:
"""Test the camera request image service fails."""
mock_install.request_image = AsyncMock(side_effect=ProsegurException())
with caplog.at_level(logging.ERROR, logger="homeassistant.components.prosegur"):
with pytest.raises(
HomeAssistantError,
match="Unable to request a new image from camera test_cam",
):
await hass.services.async_call(
DOMAIN,
"request_image",
{ATTR_ENTITY_ID: "camera.contract_1234abcd_test_cam"},
blocking=True,
)
await hass.async_block_till_done()
assert mock_install.request_image.called
assert "Could not request image from camera test_cam" in caplog.text
assert mock_install.request_image.called