Skip to content

Commit

Permalink
Change amcrest camera_image to async (#21720)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pnbruckner authored and balloob committed Mar 7, 2019
1 parent 0e36b26 commit 5616505
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/amcrest/__init__.py
Expand Up @@ -13,7 +13,7 @@
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__)
Expand Down
12 changes: 7 additions & 5 deletions 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
Expand Down Expand Up @@ -47,14 +47,16 @@ def __init__(self, hass, amcrest):
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)
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Expand Up @@ -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
Expand Down

0 comments on commit 5616505

Please sign in to comment.