1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

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

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