1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00

Ensure camera handles non-jpeg image sources correctly (#54474)

This commit is contained in:
J. Nick Koston 2021-08-11 10:12:46 -05:00 committed by GitHub
parent 13c34d646f
commit 1e14b3a0ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 7 deletions

View File

@ -177,8 +177,7 @@ async def _async_get_image(
if (
width is not None
and height is not None
and "jpeg" in content_type
or "jpg" in content_type
and ("jpeg" in content_type or "jpg" in content_type)
):
assert width is not None
assert height is not None

View File

@ -8,7 +8,12 @@ from homeassistant.components.camera import SUPPORT_ON_OFF, Camera
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Demo camera platform."""
async_add_entities([DemoCamera("Demo camera")])
async_add_entities(
[
DemoCamera("Demo camera", "image/jpg"),
DemoCamera("Demo camera png", "image/png"),
]
)
async def async_setup_entry(hass, config_entry, async_add_entities):
@ -19,10 +24,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class DemoCamera(Camera):
"""The representation of a Demo camera."""
def __init__(self, name):
def __init__(self, name, content_type):
"""Initialize demo camera component."""
super().__init__()
self._name = name
self.content_type = content_type
self._motion_status = False
self.is_streaming = True
self._images_index = 0
@ -32,7 +38,8 @@ class DemoCamera(Camera):
) -> bytes:
"""Return a faked still image response."""
self._images_index = (self._images_index + 1) % 4
image_path = Path(__file__).parent / f"demo_{self._images_index}.jpg"
ext = "jpg" if self.content_type == "image/jpg" else "png"
image_path = Path(__file__).parent / f"demo_{self._images_index}.{ext}"
return await self.hass.async_add_executor_job(image_path.read_bytes)

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

View File

@ -118,10 +118,33 @@ async def test_get_image_from_camera_with_width_height_scaled(hass, image_mock_u
)
assert mock_camera.called
assert image.content_type == "image/jpeg"
assert image.content_type == "image/jpg"
assert image.content == EMPTY_8_6_JPEG
async def test_get_image_from_camera_not_jpeg(hass, image_mock_url):
"""Grab an image from camera entity that we cannot scale."""
turbo_jpeg = mock_turbo_jpeg(
first_width=16, first_height=12, second_width=300, second_height=200
)
with patch(
"homeassistant.components.camera.img_util.TurboJPEGSingleton.instance",
return_value=turbo_jpeg,
), patch(
"homeassistant.components.demo.camera.Path.read_bytes",
autospec=True,
return_value=b"png",
) as mock_camera:
image = await camera.async_get_image(
hass, "camera.demo_camera_png", width=4, height=3
)
assert mock_camera.called
assert image.content_type == "image/png"
assert image.content == b"png"
async def test_get_stream_source_from_camera(hass, mock_camera):
"""Fetch stream source from camera entity."""
@ -200,7 +223,7 @@ async def test_websocket_camera_thumbnail(hass, hass_ws_client, mock_camera):
assert msg["id"] == 5
assert msg["type"] == TYPE_RESULT
assert msg["success"]
assert msg["result"]["content_type"] == "image/jpeg"
assert msg["result"]["content_type"] == "image/jpg"
assert msg["result"]["content"] == base64.b64encode(b"Test").decode("utf-8")