Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for August entity."""
2 
3 from abc import abstractmethod
4 
5 from yalexs.activity import Activity, ActivityType
6 from yalexs.doorbell import Doorbell, DoorbellDetail
7 from yalexs.keypad import KeypadDetail
8 from yalexs.lock import Lock, LockDetail
9 from yalexs.util import get_configuration_url
10 
11 from homeassistant.const import ATTR_CONNECTIONS
12 from homeassistant.core import callback
13 from homeassistant.helpers import device_registry as dr
14 from homeassistant.helpers.device_registry import DeviceInfo
15 from homeassistant.helpers.entity import Entity, EntityDescription
16 
17 from . import DOMAIN, AugustData
18 from .const import MANUFACTURER
19 
20 DEVICE_TYPES = ["keypad", "lock", "camera", "doorbell", "door", "bell"]
21 
22 
24  """Base implementation for August device."""
25 
26  _attr_should_poll = False
27  _attr_has_entity_name = True
28 
29  def __init__(
30  self, data: AugustData, device: Doorbell | Lock | KeypadDetail, unique_id: str
31  ) -> None:
32  """Initialize an August device."""
33  super().__init__()
34  self._data_data = data
35  self._stream_stream = data.activity_stream
36  self._device_device = device
37  detail = self._detail_detail
38  self._device_id_device_id = device.device_id
39  self._attr_unique_id_attr_unique_id = f"{device.device_id}_{unique_id}"
40  self._attr_device_info_attr_device_info = DeviceInfo(
41  identifiers={(DOMAIN, self._device_id_device_id)},
42  manufacturer=MANUFACTURER,
43  model=detail.model,
44  name=device.device_name,
45  sw_version=detail.firmware_version,
46  suggested_area=_remove_device_types(device.device_name, DEVICE_TYPES),
47  configuration_url=get_configuration_url(data.brand),
48  )
49  if isinstance(detail, LockDetail) and (mac := detail.mac_address):
50  self._attr_device_info_attr_device_info[ATTR_CONNECTIONS] = {(dr.CONNECTION_BLUETOOTH, mac)}
51 
52  @property
53  def _detail(self) -> DoorbellDetail | LockDetail:
54  return self._data_data.get_device_detail(self._device_device.device_id)
55 
56  @property
57  def _hyper_bridge(self) -> bool:
58  """Check if the lock has a paired hyper bridge."""
59  return bool(self._detail_detail.bridge and self._detail_detail.bridge.hyper_bridge)
60 
61  @callback
62  def _get_latest(self, activity_types: set[ActivityType]) -> Activity | None:
63  """Get the latest activity for the device."""
64  return self._stream_stream.get_latest_device_activity(self._device_id_device_id, activity_types)
65 
66  @callback
68  self._update_from_data_update_from_data()
69  self.async_write_ha_stateasync_write_ha_state()
70 
71  @abstractmethod
72  def _update_from_data(self) -> None:
73  """Update the entity state from the data object."""
74 
75  async def async_added_to_hass(self) -> None:
76  """Subscribe to updates."""
77  self.async_on_remove(
78  self._data.async_subscribe_device_id(
79  self._device_id, self._update_from_data_and_write_state
80  )
81  )
82  self.async_on_remove(
83  self._stream.async_subscribe_device_id(
84  self._device_id, self._update_from_data_and_write_state
85  )
86  )
87  self._update_from_data()
88 
89 
91  """An August entity with a description."""
92 
93  def __init__(
94  self,
95  data: AugustData,
96  device: Doorbell | Lock | KeypadDetail,
97  description: EntityDescription,
98  ) -> None:
99  """Initialize an August entity with a description."""
100  super().__init__(data, device, description.key)
101  self.entity_descriptionentity_description = description
102 
103 
104 def _remove_device_types(name: str, device_types: list[str]) -> str:
105  """Strip device types from a string.
106 
107  August stores the name as Master Bed Lock
108  or Master Bed Door. We can come up with a
109  reasonable suggestion by removing the supported
110  device types from the string.
111  """
112  lower_name = name.lower()
113  for device_type in device_types:
114  lower_name = lower_name.removesuffix(f" {device_type}")
115  return name[: len(lower_name)]
None __init__(self, AugustData data, Doorbell|Lock|KeypadDetail device, EntityDescription description)
Definition: entity.py:98
DoorbellDetail|LockDetail _detail(self)
Definition: entity.py:53
Activity|None _get_latest(self, set[ActivityType] activity_types)
Definition: entity.py:62
None __init__(self, AugustData data, Doorbell|Lock|KeypadDetail device, str unique_id)
Definition: entity.py:31
str _remove_device_types(str name, list[str] device_types)
Definition: entity.py:104