Make amcrest integration more robust (#30843)
- Bump amcrest package to 1.5.6. Includes networking improvements, no longer communicates during Http.__init__(), and allows running snapshot command without using stream mode. - Handle login errors better, and not just at startup. - Increase network connect & read timeout to 6.05 seconds. - Increase network read timeout to 20 seconds for snapshot command. - Run snapshot command in separate task, that cannot be cancelled, to eliminate possibility of two snapshot commands running simultaneously (since AmcrestCam.async_camera_image can be cancelled.) Also makes sure any exceptions from the command are caught properly.
This commit is contained in:
1 parent
9e87a662d5
commit
d1e7ade6db
5 files changed
+95
-34
No files matched your search
@@ -1,11 +1,11 @@
|
||||
"""Support for Amcrest IP cameras."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
from functools import partial
|
||||
import logging
|
||||
|
||||
from amcrest import AmcrestError
|
||||
from haffmpeg.camera import CameraMjpeg
|
||||
from urllib3.exceptions import HTTPError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.camera import (
|
||||
@@ -26,9 +26,11 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from .const import (
|
||||
CAMERA_WEB_SESSION_TIMEOUT,
|
||||
CAMERAS,
|
||||
COMM_TIMEOUT,
|
||||
DATA_AMCREST,
|
||||
DEVICES,
|
||||
SERVICE_UPDATE,
|
||||
SNAPSHOT_TIMEOUT,
|
||||
)
|
||||
from .helpers import log_update_error, service_signal
|
||||
|
||||
@@ -90,6 +92,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
async_add_entities([AmcrestCam(name, device, hass.data[DATA_FFMPEG])], True)
|
||||
|
||||
|
||||
class CannotSnapshot(Exception):
|
||||
"""Conditions are not valid for taking a snapshot."""
|
||||
|
||||
|
||||
class AmcrestCam(Camera):
|
||||
"""An implementation of an Amcrest IP camera."""
|
||||
|
||||
@@ -112,12 +118,11 @@ class AmcrestCam(Camera):
|
||||
self._motion_recording_enabled = None
|
||||
self._color_bw = None
|
||||
self._rtsp_url = None
|
||||
self._snapshot_lock = asyncio.Lock()
|
||||
self._snapshot_task = None
|
||||
self._unsub_dispatcher = []
|
||||
self._update_succeeded = False
|
||||
|
||||
async def async_camera_image(self):
|
||||
"""Return a still image response from the camera."""
|
||||
def _check_snapshot_ok(self):
|
||||
available = self.available
|
||||
if not available or not self.is_on:
|
||||
_LOGGER.warning(
|
||||
@@ -125,15 +130,46 @@ class AmcrestCam(Camera):
|
||||
self.name,
|
||||
"offline" if not available else "off",
|
||||
)
|
||||
raise CannotSnapshot
|
||||
|
||||
async def _async_get_image(self):
|
||||
try:
|
||||
# Send the request to snap a picture and return raw jpg data
|
||||
# Snapshot command needs a much longer read timeout than other commands.
|
||||
return await self.hass.async_add_executor_job(
|
||||
partial(
|
||||
self._api.snapshot,
|
||||
timeout=(COMM_TIMEOUT, SNAPSHOT_TIMEOUT),
|
||||
stream=False,
|
||||
)
|
||||
)
|
||||
except AmcrestError as error:
|
||||
log_update_error(_LOGGER, "get image from", self.name, "camera", error)
|
||||
return None
|
||||
finally:
|
||||
self._snapshot_task = None
|
||||
|
||||
async def async_camera_image(self):
|
||||
"""Return a still image response from the camera."""
|
||||
_LOGGER.debug("Take snapshot from %s", self._name)
|
||||
try:
|
||||
# Amcrest cameras only support one snapshot command at a time.
|
||||
# Hence need to wait if a previous snapshot has not yet finished.
|
||||
# Also need to check that camera is online and turned on before each wait
|
||||
# and before initiating shapshot.
|
||||
while self._snapshot_task:
|
||||
self._check_snapshot_ok()
|
||||
_LOGGER.debug("Waiting for previous snapshot from %s ...", self._name)
|
||||
await self._snapshot_task
|
||||
self._check_snapshot_ok()
|
||||
# Run snapshot command in separate Task that can't be cancelled so
|
||||
# 1) it's not possible to send another snapshot command while camera is
|
||||
# still working on a previous one, and
|
||||
# 2) someone will be around to catch any exceptions.
|
||||
self._snapshot_task = self.hass.async_create_task(self._async_get_image())
|
||||
return await asyncio.shield(self._snapshot_task)
|
||||
except CannotSnapshot:
|
||||
return None
|
||||
async with self._snapshot_lock:
|
||||
try:
|
||||
# Send the request to snap a picture and return raw jpg data
|
||||
response = await self.hass.async_add_executor_job(self._api.snapshot)
|
||||
return response.data
|
||||
except (AmcrestError, HTTPError) as error:
|
||||
log_update_error(_LOGGER, "get image from", self.name, "camera", error)
|
||||
return None
|
||||
|
||||
async def handle_async_mjpeg_stream(self, request):
|
||||
"""Return an MJPEG stream."""
|
||||
|
||||
Reference in new issue
Block a user