From 3f2fad1a2714c9feba045ae46f4780127686eff3 Mon Sep 17 00:00:00 2001 From: prwood80 <22550665+prwood80@users.noreply.github.com> Date: Fri, 27 Aug 2021 11:22:49 -0500 Subject: [PATCH] Improve performance of ring camera still images (#53803) Co-authored-by: Pat Wood Co-authored-by: Paulus Schoutsen Co-authored-by: Paulus Schoutsen --- homeassistant/components/ring/camera.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/ring/camera.py b/homeassistant/components/ring/camera.py index 509877ae5ff..6a4ef692c1e 100644 --- a/homeassistant/components/ring/camera.py +++ b/homeassistant/components/ring/camera.py @@ -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,12 +108,18 @@ 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 + if self._image is None and self._video_url: + image = await ffmpeg.async_get_image( + self.hass, + self._video_url, + width=width, + height=height, + ) - return 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.""" @@ -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"]