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.
This commit is contained in:
Phil Bruckner 2019-03-06 21:42:59 -06:00 committed by Paulus Schoutsen
parent 0e36b26770
commit 5616505032
3 changed files with 9 additions and 7 deletions

View File

@ -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__)

View File

@ -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)

View File

@ -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