Ensure homekit functions if numpy is unavailable (#35931)

pull/36070/head
J. Nick Koston 2020-05-22 19:52:33 -05:00 committed by Franck Nijhof
parent d299a92cd3
commit f452e26269
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 12 additions and 18 deletions

View File

@ -2,8 +2,6 @@
import logging
from turbojpeg import TurboJPEG
SUPPORTED_SCALING_FACTORS = [(7, 8), (3, 4), (5, 8), (1, 2), (3, 8), (1, 4), (1, 8)]
_LOGGER = logging.getLogger(__name__)
@ -54,6 +52,12 @@ class TurboJPEGSingleton:
def __init__(self):
"""Try to create TurboJPEG only once."""
try:
# TurboJPEG checks for libturbojpeg
# when its created, but it imports
# numpy which may or may not work so
# we have to guard the import here.
from turbojpeg import TurboJPEG # pylint: disable=import-outside-toplevel
TurboJPEGSingleton.__instance = TurboJPEG()
except Exception: # pylint: disable=broad-except
_LOGGER.exception(

View File

@ -23,25 +23,19 @@ def test_scale_jpeg_camera_image():
camera_image = Image("image/jpeg", EMPTY_16_12_JPEG)
turbo_jpeg = mock_turbo_jpeg(first_width=16, first_height=12)
with patch(
"homeassistant.components.homekit.img_util.TurboJPEG", return_value=False
):
with patch("turbojpeg.TurboJPEG", return_value=False):
TurboJPEGSingleton()
assert scale_jpeg_camera_image(camera_image, 16, 12) == camera_image.content
turbo_jpeg = mock_turbo_jpeg(first_width=16, first_height=12)
with patch(
"homeassistant.components.homekit.img_util.TurboJPEG", return_value=turbo_jpeg
):
with patch("turbojpeg.TurboJPEG", return_value=turbo_jpeg):
TurboJPEGSingleton()
assert scale_jpeg_camera_image(camera_image, 16, 12) == EMPTY_16_12_JPEG
turbo_jpeg = mock_turbo_jpeg(
first_width=16, first_height=12, second_width=8, second_height=6
)
with patch(
"homeassistant.components.homekit.img_util.TurboJPEG", return_value=turbo_jpeg
):
with patch("turbojpeg.TurboJPEG", return_value=turbo_jpeg):
TurboJPEGSingleton()
jpeg_bytes = scale_jpeg_camera_image(camera_image, 8, 6)
@ -51,12 +45,10 @@ def test_scale_jpeg_camera_image():
def test_turbojpeg_load_failure():
"""Handle libjpegturbo not being installed."""
with patch(
"homeassistant.components.homekit.img_util.TurboJPEG", side_effect=Exception
):
with patch("turbojpeg.TurboJPEG", side_effect=Exception):
TurboJPEGSingleton()
assert TurboJPEGSingleton.instance() is False
with patch("homeassistant.components.homekit.img_util.TurboJPEG"):
with patch("turbojpeg.TurboJPEG"):
TurboJPEGSingleton()
assert TurboJPEGSingleton.instance()

View File

@ -193,9 +193,7 @@ async def test_camera_stream_source_configured(hass, run_driver, events):
turbo_jpeg = mock_turbo_jpeg(
first_width=16, first_height=12, second_width=300, second_height=200
)
with patch(
"homeassistant.components.homekit.img_util.TurboJPEG", return_value=turbo_jpeg
):
with patch("turbojpeg.TurboJPEG", return_value=turbo_jpeg):
TurboJPEGSingleton()
assert await hass.async_add_executor_job(
acc.get_snapshot, {"aid": 2, "image-width": 300, "image-height": 200}