Home Assistant Unofficial Reference 2024.12.1
prefs.py
Go to the documentation of this file.
1 """Preference management for camera component."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from dataclasses import asdict, dataclass
7 from typing import Final, cast
8 
9 from homeassistant.components.stream import Orientation
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import HomeAssistantError
12 from homeassistant.helpers import entity_registry as er
13 from homeassistant.helpers.storage import Store
14 from homeassistant.helpers.typing import UNDEFINED, UndefinedType
15 
16 from .const import DOMAIN, PREF_ORIENTATION, PREF_PRELOAD_STREAM
17 
18 STORAGE_KEY: Final = DOMAIN
19 STORAGE_VERSION: Final = 1
20 
21 
22 @dataclass
24  """Stream settings which are managed and updated by the camera entity."""
25 
26  preload_stream: bool = False
27  orientation: Orientation = Orientation.NO_TRANSFORM
28 
29 
31  """Handle camera preferences."""
32 
33  _preload_prefs: dict[str, dict[str, bool | Orientation]]
34 
35  def __init__(self, hass: HomeAssistant) -> None:
36  """Initialize camera prefs."""
37  self._hass_hass = hass
38  # The orientation prefs are stored in in the entity registry options
39  # The preload_stream prefs are stored in this Store
40  self._store_store = Store[dict[str, dict[str, bool | Orientation]]](
41  hass, STORAGE_VERSION, STORAGE_KEY
42  )
43  self._dynamic_stream_settings_by_entity_id: dict[
44  str, DynamicStreamSettings
45  ] = {}
46 
47  async def async_load(self) -> None:
48  """Initialize the camera preferences."""
49  self._preload_prefs_preload_prefs = await self._store_store.async_load() or {}
50 
51  async def async_update(
52  self,
53  entity_id: str,
54  *,
55  preload_stream: bool | UndefinedType = UNDEFINED,
56  orientation: Orientation | UndefinedType = UNDEFINED,
57  ) -> dict[str, bool | Orientation]:
58  """Update camera preferences.
59 
60  Also update the DynamicStreamSettings if they exist.
61  preload_stream is stored in a Store
62  orientation is stored in the Entity Registry
63 
64  Returns a dict with the preferences on success.
65  Raises HomeAssistantError on failure.
66  """
67  dynamic_stream_settings = self._dynamic_stream_settings_by_entity_id.get(
68  entity_id
69  )
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}
74  await self._store_store.async_save(self._preload_prefs_preload_prefs)
75 
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}
80  )
81  else:
82  raise HomeAssistantError(
83  "Orientation is only supported on entities set up through config"
84  " flows"
85  )
86  if dynamic_stream_settings:
87  dynamic_stream_settings.orientation = orientation
88  return asdict(await self.get_dynamic_stream_settingsget_dynamic_stream_settings(entity_id))
89 
91  self, entity_id: str
92  ) -> DynamicStreamSettings:
93  """Get the DynamicStreamSettings for the entity."""
94  if settings := self._dynamic_stream_settings_by_entity_id.get(entity_id):
95  return settings
96  # Get preload stream setting from prefs
97  # Get orientation setting from entity registry
98  reg_entry = er.async_get(self._hass_hass).async_get(entity_id)
99  er_prefs: Mapping = reg_entry.options.get(DOMAIN, {}) if reg_entry else {}
100  settings = DynamicStreamSettings(
101  preload_stream=cast(
102  bool,
103  self._preload_prefs_preload_prefs.get(entity_id, {}).get(PREF_PRELOAD_STREAM, False),
104  ),
105  orientation=er_prefs.get(PREF_ORIENTATION, Orientation.NO_TRANSFORM),
106  )
107  self._dynamic_stream_settings_by_entity_id[entity_id] = settings
108  return settings
DynamicStreamSettings get_dynamic_stream_settings(self, str entity_id)
Definition: prefs.py:92
None __init__(self, HomeAssistant hass)
Definition: prefs.py:35
dict[str, bool|Orientation] async_update(self, str entity_id, *bool|UndefinedType preload_stream=UNDEFINED, Orientation|UndefinedType orientation=UNDEFINED)
Definition: prefs.py:57
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
AreaRegistry async_get(HomeAssistant hass)
None async_save(self, _T data)
Definition: storage.py:424