Remove legacy compatiblity for camera platforms that do not support width/height (#68039)

- async_camera_image or camera_image must accept the width and
  height arguments
This commit is contained in:
J. Nick Koston 2022-03-12 08:47:14 -10:00 committed by GitHub
parent 6124081ddc
commit 8e3454e46a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 55 deletions

View File

@ -10,7 +10,6 @@ from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import partial from functools import partial
import hashlib import hashlib
import inspect
import logging import logging
import os import os
from random import SystemRandom from random import SystemRandom
@ -161,18 +160,9 @@ async def _async_get_image(
""" """
with suppress(asyncio.CancelledError, asyncio.TimeoutError): with suppress(asyncio.CancelledError, asyncio.TimeoutError):
async with async_timeout.timeout(timeout): async with async_timeout.timeout(timeout):
# Calling inspect will be removed in 2022.1 after all if image_bytes := await camera.async_camera_image(
# custom components have had a chance to change their signature width=width, height=height
sig = inspect.signature(camera.async_camera_image) ):
if "height" in sig.parameters and "width" in sig.parameters:
image_bytes = await camera.async_camera_image(
width=width, height=height
)
else:
camera.async_warn_old_async_camera_image_signature()
image_bytes = await camera.async_camera_image()
if image_bytes:
content_type = camera.content_type content_type = camera.content_type
image = Image(content_type, image_bytes) image = Image(content_type, image_bytes)
if ( if (
@ -576,27 +566,9 @@ class Camera(Entity):
self, width: int | None = None, height: int | None = None self, width: int | None = None, height: int | None = None
) -> bytes | None: ) -> bytes | None:
"""Return bytes of camera image.""" """Return bytes of camera image."""
sig = inspect.signature(self.camera_image) return await self.hass.async_add_executor_job(
# Calling inspect will be removed in 2022.1 after all partial(self.camera_image, width=width, height=height)
# custom components have had a chance to change their signature
if "height" in sig.parameters and "width" in sig.parameters:
return await self.hass.async_add_executor_job(
partial(self.camera_image, width=width, height=height)
)
self.async_warn_old_async_camera_image_signature()
return await self.hass.async_add_executor_job(self.camera_image)
# Remove in 2022.1 after all custom components have had a chance to change their signature
@callback
def async_warn_old_async_camera_image_signature(self) -> None:
"""Warn once when calling async_camera_image with the function old signature."""
if self._warned_old_signature:
return
_LOGGER.warning(
"The camera entity %s does not support requesting width and height, please open an issue with the integration author",
self.entity_id,
) )
self._warned_old_signature = True
async def handle_async_still_stream( async def handle_async_still_stream(
self, request: web.Request, interval: float self, request: web.Request, interval: float

View File

@ -108,28 +108,6 @@ async def test_get_image_from_camera(hass, image_mock_url):
assert image.content == b"Test" assert image.content == b"Test"
async def test_legacy_async_get_image_signature_warns_only_once(
hass, image_mock_url, caplog
):
"""Test that we only warn once when we encounter a legacy async_get_image function signature."""
async def _legacy_async_camera_image(self):
return b"Image"
with patch(
"homeassistant.components.demo.camera.DemoCamera.async_camera_image",
new=_legacy_async_camera_image,
):
image = await camera.async_get_image(hass, "camera.demo_camera")
assert image.content == b"Image"
assert "does not support requesting width and height" in caplog.text
caplog.clear()
image = await camera.async_get_image(hass, "camera.demo_camera")
assert image.content == b"Image"
assert "does not support requesting width and height" not in caplog.text
async def test_get_image_from_camera_with_width_height(hass, image_mock_url): async def test_get_image_from_camera_with_width_height(hass, image_mock_url):
"""Grab an image from camera entity with width and height.""" """Grab an image from camera entity with width and height."""