Only load camera prefs once (#112064)

This commit is contained in:
Paulus Schoutsen 2024-03-02 17:18:34 -05:00 committed by GitHub
parent ec4331fc19
commit dd1ad71166
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -391,6 +391,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
)
prefs = CameraPreferences(hass)
await prefs.async_load()
hass.data[DATA_CAMERA_PREFS] = prefs
hass.http.register_view(CameraImageView(component))

View File

@ -29,6 +29,8 @@ class DynamicStreamSettings:
class CameraPreferences:
"""Handle camera preferences."""
_preload_prefs: dict[str, dict[str, bool | Orientation]]
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize camera prefs."""
self._hass = hass
@ -41,6 +43,10 @@ class CameraPreferences:
str, DynamicStreamSettings
] = {}
async def async_load(self) -> None:
"""Initialize the camera preferences."""
self._preload_prefs = await self._store.async_load() or {}
async def async_update(
self,
entity_id: str,
@ -63,9 +69,8 @@ class CameraPreferences:
if preload_stream is not UNDEFINED:
if dynamic_stream_settings:
dynamic_stream_settings.preload_stream = preload_stream
preload_prefs = await self._store.async_load() or {}
preload_prefs[entity_id] = {PREF_PRELOAD_STREAM: preload_stream}
await self._store.async_save(preload_prefs)
self._preload_prefs[entity_id] = {PREF_PRELOAD_STREAM: preload_stream}
await self._store.async_save(self._preload_prefs)
if orientation is not UNDEFINED:
if (registry := er.async_get(self._hass)).async_get(entity_id):
@ -91,10 +96,10 @@ class CameraPreferences:
# Get orientation setting from entity registry
reg_entry = er.async_get(self._hass).async_get(entity_id)
er_prefs: Mapping = reg_entry.options.get(DOMAIN, {}) if reg_entry else {}
preload_prefs = await self._store.async_load() or {}
settings = DynamicStreamSettings(
preload_stream=cast(
bool, preload_prefs.get(entity_id, {}).get(PREF_PRELOAD_STREAM, False)
bool,
self._preload_prefs.get(entity_id, {}).get(PREF_PRELOAD_STREAM, False),
),
orientation=er_prefs.get(PREF_ORIENTATION, Orientation.NO_TRANSFORM),
)