From 56165050323be5eb181fc6ad2fd3c047c04c5fe0 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Wed, 6 Mar 2019 21:42:59 -0600 Subject: [PATCH] Change amcrest camera_image to async (#21720) Change AmcrestCam method camera_image to async so asyncio lock can be used instead of a threading lock. Bump amcrest package to 1.2.5. --- homeassistant/components/amcrest/__init__.py | 2 +- homeassistant/components/amcrest/camera.py | 12 +++++++----- requirements_all.txt | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/amcrest/__init__.py b/homeassistant/components/amcrest/__init__.py index bcb18402900..b976c1bd9d3 100644 --- a/homeassistant/components/amcrest/__init__.py +++ b/homeassistant/components/amcrest/__init__.py @@ -13,7 +13,7 @@ from homeassistant.const import ( from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv -REQUIREMENTS = ['amcrest==1.2.4'] +REQUIREMENTS = ['amcrest==1.2.5'] DEPENDENCIES = ['ffmpeg'] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/amcrest/camera.py b/homeassistant/components/amcrest/camera.py index 6acaa5fc86e..2793fbca958 100644 --- a/homeassistant/components/amcrest/camera.py +++ b/homeassistant/components/amcrest/camera.py @@ -1,6 +1,6 @@ """Support for Amcrest IP cameras.""" +import asyncio import logging -import threading from requests import RequestException from urllib3.exceptions import ReadTimeoutError @@ -47,14 +47,16 @@ class AmcrestCam(Camera): self._stream_source = amcrest.stream_source self._resolution = amcrest.resolution self._token = self._auth = amcrest.authentication - self._snapshot_lock = threading.Lock() + self._snapshot_lock = asyncio.Lock() - def camera_image(self): + async def async_camera_image(self): """Return a still image response from the camera.""" - with self._snapshot_lock: + async with self._snapshot_lock: try: # Send the request to snap a picture and return raw jpg data - return self._camera.snapshot(channel=self._resolution).data + response = await self.hass.async_add_executor_job( + self._camera.snapshot, self._resolution) + return response.data except (RequestException, ReadTimeoutError, ValueError) as error: _LOGGER.error( 'Could not get camera image due to error %s', error) diff --git a/requirements_all.txt b/requirements_all.txt index ec838ea6cc8..207097b2bdc 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -152,7 +152,7 @@ alarmdecoder==1.13.2 alpha_vantage==2.1.0 # homeassistant.components.amcrest -amcrest==1.2.4 +amcrest==1.2.5 # homeassistant.components.switch.anel_pwrctrl anel_pwrctrl-homeassistant==0.0.1.dev2