Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Permissions for Home Assistant."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 
7 import voluptuous as vol
8 
9 from .const import CAT_ENTITIES
10 from .entities import ENTITY_POLICY_SCHEMA, compile_entities
11 from .merge import merge_policies
12 from .models import PermissionLookup
13 from .types import PolicyType
14 from .util import test_all
15 
16 POLICY_SCHEMA = vol.Schema({vol.Optional(CAT_ENTITIES): ENTITY_POLICY_SCHEMA})
17 
18 __all__ = [
19  "POLICY_SCHEMA",
20  "merge_policies",
21  "PermissionLookup",
22  "PolicyType",
23  "AbstractPermissions",
24  "PolicyPermissions",
25  "OwnerPermissions",
26 ]
27 
28 
30  """Default permissions class."""
31 
32  _cached_entity_func: Callable[[str, str], bool] | None = None
33 
34  def _entity_func(self) -> Callable[[str, str], bool]:
35  """Return a function that can test entity access."""
36  raise NotImplementedError
37 
38  def access_all_entities(self, key: str) -> bool:
39  """Check if we have a certain access to all entities."""
40  raise NotImplementedError
41 
42  def check_entity(self, entity_id: str, key: str) -> bool:
43  """Check if we can access entity."""
44  if (entity_func := self._cached_entity_func_cached_entity_func) is None:
45  entity_func = self._cached_entity_func_cached_entity_func = self._entity_func_entity_func()
46 
47  return entity_func(entity_id, key)
48 
49 
51  """Handle permissions."""
52 
53  def __init__(self, policy: PolicyType, perm_lookup: PermissionLookup) -> None:
54  """Initialize the permission class."""
55  self._policy_policy = policy
56  self._perm_lookup_perm_lookup = perm_lookup
57 
58  def access_all_entities(self, key: str) -> bool:
59  """Check if we have a certain access to all entities."""
60  return test_all(self._policy_policy.get(CAT_ENTITIES), key)
61 
62  def _entity_func(self) -> Callable[[str, str], bool]:
63  """Return a function that can test entity access."""
64  return compile_entities(self._policy_policy.get(CAT_ENTITIES), self._perm_lookup_perm_lookup)
65 
66  def __eq__(self, other: object) -> bool:
67  """Equals check."""
68  return isinstance(other, PolicyPermissions) and other._policy == self._policy_policy
69 
70 
72  """Owner permissions."""
73 
74  def access_all_entities(self, key: str) -> bool:
75  """Check if we have a certain access to all entities."""
76  return True
77 
78  def _entity_func(self) -> Callable[[str, str], bool]:
79  """Return a function that can test entity access."""
80  return lambda entity_id, key: True
81 
82 
83 OwnerPermissions = _OwnerPermissions()
Callable[[str, str], bool] _entity_func(self)
Definition: __init__.py:34
bool check_entity(self, str entity_id, str key)
Definition: __init__.py:42
Callable[[str, str], bool] _entity_func(self)
Definition: __init__.py:62
None __init__(self, PolicyType policy, PermissionLookup perm_lookup)
Definition: __init__.py:53
Callable[[str, str], bool] _entity_func(self)
Definition: __init__.py:78
Callable[[str, str], bool] compile_entities(CategoryType policy, PermissionLookup perm_lookup)
Definition: entities.py:92
bool test_all(CategoryType policy, str key)
Definition: util.py:103
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88