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