From 5fdcbbe0e1111475712da44e727de7c433a83030 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 5 Dec 2021 09:45:40 -0800 Subject: [PATCH] Fetch media for events for rendering in the nest media player (#61056) --- homeassistant/components/nest/__init__.py | 8 ++++++++ tests/components/nest/common.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index af3757d31da..fb39188710c 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -86,6 +86,11 @@ PLATFORMS = ["sensor", "camera", "climate"] WEB_AUTH_DOMAIN = DOMAIN INSTALLED_AUTH_DOMAIN = f"{DOMAIN}.installed" +# Fetch media for events with an in memory cache. The largest media items +# are mp4 clips at ~90kb each, so this totals a few MB per camera. +# Note: Media for events can only be published within 30 seconds of the event +EVENT_MEDIA_CACHE_SIZE = 64 + class WebAuth(config_entry_oauth2_flow.LocalOAuth2Implementation): """OAuth implementation using OAuth for web applications.""" @@ -206,6 +211,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: subscriber = await api.new_subscriber(hass, entry) if not subscriber: return False + # Keep media for last N events in memory + subscriber.cache_policy.event_cache_size = EVENT_MEDIA_CACHE_SIZE + subscriber.cache_policy.fetch = True callback = SignalUpdateCallback(hass) subscriber.set_update_callback(callback.async_handle_event) diff --git a/tests/components/nest/common.py b/tests/components/nest/common.py index eb44b19d540..a0ba813ab28 100644 --- a/tests/components/nest/common.py +++ b/tests/components/nest/common.py @@ -6,6 +6,7 @@ from unittest.mock import patch from google_nest_sdm.device_manager import DeviceManager from google_nest_sdm.event import EventMessage +from google_nest_sdm.event_media import CachePolicy from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber from homeassistant.components.nest import DOMAIN @@ -98,6 +99,11 @@ class FakeSubscriber(GoogleNestSubscriber): """Return the fake device manager.""" return self._device_manager + @property + def cache_policy(self) -> CachePolicy: + """Return the cache policy.""" + return self._device_manager.cache_policy + def stop_async(self): """No-op to stop the subscriber.""" return None