Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions homeassistant/components/homekit/img_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 5 additions & 13 deletions tests/components/homekit/test_img_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
4 changes: 1 addition & 3 deletions tests/components/homekit/test_type_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down