1 """Preference management for camera component."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from dataclasses
import asdict, dataclass
7 from typing
import Final, cast
16 from .const
import DOMAIN, PREF_ORIENTATION, PREF_PRELOAD_STREAM
18 STORAGE_KEY: Final = DOMAIN
19 STORAGE_VERSION: Final = 1
24 """Stream settings which are managed and updated by the camera entity."""
26 preload_stream: bool =
False
27 orientation: Orientation = Orientation.NO_TRANSFORM
31 """Handle camera preferences."""
33 _preload_prefs: dict[str, dict[str, bool | Orientation]]
35 def __init__(self, hass: HomeAssistant) ->
None:
36 """Initialize camera prefs."""
40 self.
_store_store = Store[dict[str, dict[str, bool | Orientation]]](
41 hass, STORAGE_VERSION, STORAGE_KEY
43 self._dynamic_stream_settings_by_entity_id: dict[
44 str, DynamicStreamSettings
48 """Initialize the camera preferences."""
55 preload_stream: bool | UndefinedType = UNDEFINED,
56 orientation: Orientation | UndefinedType = UNDEFINED,
57 ) -> dict[str, bool | Orientation]:
58 """Update camera preferences.
60 Also update the DynamicStreamSettings if they exist.
61 preload_stream is stored in a Store
62 orientation is stored in the Entity Registry
64 Returns a dict with the preferences on success.
65 Raises HomeAssistantError on failure.
67 dynamic_stream_settings = self._dynamic_stream_settings_by_entity_id.
get(
70 if preload_stream
is not UNDEFINED:
71 if dynamic_stream_settings:
72 dynamic_stream_settings.preload_stream = preload_stream
73 self.
_preload_prefs_preload_prefs[entity_id] = {PREF_PRELOAD_STREAM: preload_stream}
76 if orientation
is not UNDEFINED:
77 if (registry := er.async_get(self.
_hass_hass)).
async_get(entity_id):
78 registry.async_update_entity_options(
79 entity_id, DOMAIN, {PREF_ORIENTATION: orientation}
83 "Orientation is only supported on entities set up through config"
86 if dynamic_stream_settings:
87 dynamic_stream_settings.orientation = orientation
92 ) -> DynamicStreamSettings:
93 """Get the DynamicStreamSettings for the entity."""
94 if settings := self._dynamic_stream_settings_by_entity_id.
get(entity_id):
99 er_prefs: Mapping = reg_entry.options.get(DOMAIN, {})
if reg_entry
else {}
105 orientation=er_prefs.get(PREF_ORIENTATION, Orientation.NO_TRANSFORM),
107 self._dynamic_stream_settings_by_entity_id[entity_id] = settings
DynamicStreamSettings get_dynamic_stream_settings(self, str entity_id)
None __init__(self, HomeAssistant hass)
dict[str, bool|Orientation] async_update(self, str entity_id, *bool|UndefinedType preload_stream=UNDEFINED, Orientation|UndefinedType orientation=UNDEFINED)
web.Response get(self, web.Request request, str config_key)
AreaRegistry async_get(HomeAssistant hass)
None async_save(self, _T data)