Update nest sdm camera to refresh in background (#42865)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Allen Porter 2020-11-10 14:48:02 -08:00 committed by GitHub
parent 9f4480a634
commit 94db07ca8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 157 additions and 24 deletions

View file

@ -1,16 +1,19 @@
"""Support for Google Nest SDM Cameras."""
import datetime
import logging
from typing import Optional
from google_nest_sdm.camera_traits import CameraImageTrait, CameraLiveStreamTrait
from google_nest_sdm.device import Device
from haffmpeg.tools import IMAGE_JPEG
import requests
from homeassistant.components.camera import SUPPORT_STREAM, Camera
from homeassistant.components.ffmpeg import async_get_image
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util.dt import utcnow
@ -19,6 +22,9 @@ from .device_info import DeviceInfo
_LOGGER = logging.getLogger(__name__)
# Used to schedule an alarm to refresh the stream before expiration
STREAM_EXPIRATION_BUFFER = datetime.timedelta(seconds=30)
async def async_setup_sdm_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
@ -49,6 +55,7 @@ class NestCamera(Camera):
self._device = device
self._device_info = DeviceInfo(device)
self._stream = None
self._stream_refresh_unsub = None
@property
def should_poll(self) -> bool:
@ -93,21 +100,50 @@ class NestCamera(Camera):
if CameraLiveStreamTrait.NAME not in self._device.traits:
return None
trait = self._device.traits[CameraLiveStreamTrait.NAME]
now = utcnow()
if not self._stream:
logging.debug("Fetching stream url")
_LOGGER.debug("Fetching stream url")
self._stream = await trait.generate_rtsp_stream()
elif self._stream.expires_at < now:
logging.debug("Stream expired, extending stream")
new_stream = await self._stream.extend_rtsp_stream()
self._stream = new_stream
self._schedule_stream_refresh()
if self._stream.expires_at < utcnow():
_LOGGER.warning("Stream already expired")
return self._stream.rtsp_stream_url
def _schedule_stream_refresh(self):
"""Schedules an alarm to refresh the stream url before expiration."""
_LOGGER.debug("New stream url expires at %s", self._stream.expires_at)
refresh_time = self._stream.expires_at - STREAM_EXPIRATION_BUFFER
# Schedule an alarm to extend the stream
if self._stream_refresh_unsub is not None:
self._stream_refresh_unsub()
self._stream_refresh_unsub = async_track_point_in_utc_time(
self.hass,
self._handle_stream_refresh,
refresh_time,
)
async def _handle_stream_refresh(self, now):
"""Alarm that fires to check if the stream should be refreshed."""
if not self._stream:
return
_LOGGER.debug("Extending stream url")
self._stream_refresh_unsub = None
try:
self._stream = await self._stream.extend_rtsp_stream()
except requests.HTTPError as err:
_LOGGER.debug("Failed to extend stream: %s", err)
# Next attempt to catch a url will get a new one
self._stream = None
return
self._schedule_stream_refresh()
async def async_will_remove_from_hass(self):
"""Invalidates the RTSP token when unloaded."""
if self._stream:
logging.debug("Invalidating stream")
_LOGGER.debug("Invalidating stream")
await self._stream.stop_rtsp_stream()
if self._stream_refresh_unsub:
self._stream_refresh_unsub()
async def async_added_to_hass(self):
"""Run when entity is added to register update signal handler."""