diff --git a/homeassistant/components/homekit/img_util.py b/homeassistant/components/homekit/img_util.py index 835b04558e6aac..88217bf776d52a 100644 --- a/homeassistant/components/homekit/img_util.py +++ b/homeassistant/components/homekit/img_util.py @@ -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 @@ def instance(): 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( diff --git a/tests/components/homekit/test_img_util.py b/tests/components/homekit/test_img_util.py index 4ada89b3acd818..728bb8847fffd0 100644 --- a/tests/components/homekit/test_img_util.py +++ b/tests/components/homekit/test_img_util.py @@ -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() diff --git a/tests/components/homekit/test_type_cameras.py b/tests/components/homekit/test_type_cameras.py index e3444ca23e4286..0c002fa7213b78 100644 --- a/tests/components/homekit/test_type_cameras.py +++ b/tests/components/homekit/test_type_cameras.py @@ -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}