diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index c6cada2e3c9..14cd64df920 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -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 diff --git a/homeassistant/components/demo/camera.py b/homeassistant/components/demo/camera.py index b3f9b505aee..572a5bf331e 100644 --- a/homeassistant/components/demo/camera.py +++ b/homeassistant/components/demo/camera.py @@ -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) diff --git a/homeassistant/components/demo/demo_0.png b/homeassistant/components/demo/demo_0.png new file mode 100644 index 00000000000..f45852e3b20 Binary files /dev/null and b/homeassistant/components/demo/demo_0.png differ diff --git a/homeassistant/components/demo/demo_1.png b/homeassistant/components/demo/demo_1.png new file mode 100644 index 00000000000..0a2131a773e Binary files /dev/null and b/homeassistant/components/demo/demo_1.png differ diff --git a/homeassistant/components/demo/demo_2.png b/homeassistant/components/demo/demo_2.png new file mode 100644 index 00000000000..97a8e49025d Binary files /dev/null and b/homeassistant/components/demo/demo_2.png differ diff --git a/homeassistant/components/demo/demo_3.png b/homeassistant/components/demo/demo_3.png new file mode 100644 index 00000000000..0a2131a773e Binary files /dev/null and b/homeassistant/components/demo/demo_3.png differ diff --git a/tests/components/camera/test_init.py b/tests/components/camera/test_init.py index f4267de234c..bb3f76e0d1b 100644 --- a/tests/components/camera/test_init.py +++ b/tests/components/camera/test_init.py @@ -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")