Improve performance of ring camera still images (#53803)

Co-authored-by: Pat Wood <prwood80@users.noreply.github.com>
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
prwood80 2021-08-27 11:22:49 -05:00 committed by GitHub
parent 819fd811af
commit 3f2fad1a27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,6 +52,7 @@ class RingCam(RingEntityMixin, Camera):
self._last_event = None
self._last_video_id = None
self._video_url = None
self._image = None
self._expires_at = dt_util.utcnow() - FORCE_REFRESH_INTERVAL
async def async_added_to_hass(self):
@ -80,6 +81,7 @@ class RingCam(RingEntityMixin, Camera):
self._last_event = None
self._last_video_id = None
self._video_url = None
self._image = None
self._expires_at = dt_util.utcnow()
self.async_write_ha_state()
@ -106,13 +108,19 @@ class RingCam(RingEntityMixin, Camera):
self, width: int | None = None, height: int | None = None
) -> bytes | None:
"""Return a still image response from the camera."""
if self._video_url is None:
return
return await ffmpeg.async_get_image(
self.hass, self._video_url, width=width, height=height
if self._image is None and self._video_url:
image = await ffmpeg.async_get_image(
self.hass,
self._video_url,
width=width,
height=height,
)
if image:
self._image = image
return self._image
async def handle_async_mjpeg_stream(self, request):
"""Generate an HTTP MJPEG stream from the camera."""
if self._video_url is None:
@ -144,6 +152,9 @@ class RingCam(RingEntityMixin, Camera):
if self._last_video_id == self._last_event["id"] and utcnow <= self._expires_at:
return
if self._last_video_id != self._last_event["id"]:
self._image = None
try:
video_url = await self.hass.async_add_executor_job(
self._device.recording_url, self._last_event["id"]