Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Bosch Smart Home Controller base entity."""
2 
3 from __future__ import annotations
4 
5 from boschshcpy import SHCDevice, SHCIntrusionSystem
6 
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import device_registry as dr
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.entity import Entity
11 
12 from .const import DOMAIN
13 
14 
16  hass: HomeAssistant, entity: SHCBaseEntity, entry_id: str
17 ) -> None:
18  """Get item that is removed from session."""
19  dev_registry = dr.async_get(hass)
20  device = dev_registry.async_get_device(identifiers={(DOMAIN, entity.device_id)})
21  if device is not None:
22  dev_registry.async_update_device(device.id, remove_config_entry_id=entry_id)
23 
24 
26  """Base representation of a SHC entity."""
27 
28  _attr_should_poll = False
29  _attr_has_entity_name = True
30 
31  def __init__(
32  self, device: SHCDevice | SHCIntrusionSystem, parent_id: str, entry_id: str
33  ) -> None:
34  """Initialize the generic SHC device."""
35  self._device_device = device
36  self._entry_id_entry_id = entry_id
37 
38  async def async_added_to_hass(self) -> None:
39  """Subscribe to SHC events."""
40  await super().async_added_to_hass()
41 
42  def on_state_changed() -> None:
43  if self._device_device.deleted:
44  self.hasshass.add_job(async_remove_devices(self.hasshass, self, self._entry_id_entry_id))
45  else:
46  self.schedule_update_ha_stateschedule_update_ha_state()
47 
48  self._device_device.subscribe_callback(self.entity_identity_id, on_state_changed)
49 
50  async def async_will_remove_from_hass(self) -> None:
51  """Unsubscribe from SHC events."""
52  await super().async_will_remove_from_hass()
53  self._device_device.unsubscribe_callback(self.entity_identity_id)
54 
55  @property
56  def device_id(self) -> str:
57  """Return the device id."""
58  return self._device_device.id
59 
60 
62  """Representation of a SHC device entity."""
63 
64  def __init__(self, device: SHCDevice, parent_id: str, entry_id: str) -> None:
65  """Initialize generic SHC device."""
66  self._attr_unique_id_attr_unique_id = device.serial
67  self._attr_device_info_attr_device_info = DeviceInfo(
68  identifiers={(DOMAIN, device.id)},
69  manufacturer=device.manufacturer,
70  model=device.device_model,
71  name=device.name,
72  via_device=(
73  DOMAIN,
74  device.parent_device_id
75  if device.parent_device_id is not None
76  else parent_id,
77  ),
78  )
79  super().__init__(device=device, parent_id=parent_id, entry_id=entry_id)
80 
81  async def async_added_to_hass(self) -> None:
82  """Subscribe to SHC events."""
83  await super().async_added_to_hass()
84 
85  def on_state_changed() -> None:
86  self.schedule_update_ha_stateschedule_update_ha_state()
87 
88  for service in self._device_device.device_services:
89  service.subscribe_callback(self.entity_identity_id, on_state_changed)
90 
91  async def async_will_remove_from_hass(self) -> None:
92  """Unsubscribe from SHC events."""
93  await super().async_will_remove_from_hass()
94  for service in self._device_device.device_services:
95  service.unsubscribe_callback(self.entity_identity_id)
96 
97  @property
98  def available(self) -> bool:
99  """Return false if status is unavailable."""
100  return self._device_device.status == "AVAILABLE"
101 
102 
104  """Representation of a SHC domain service entity."""
105 
106  def __init__(
107  self, domain: SHCIntrusionSystem, parent_id: str, entry_id: str
108  ) -> None:
109  """Initialize the generic SHC device."""
110  self._attr_unique_id_attr_unique_id = domain.id
111  self._attr_device_info_attr_device_info = DeviceInfo(
112  identifiers={(DOMAIN, domain.id)},
113  manufacturer=domain.manufacturer,
114  model=domain.device_model,
115  name=domain.name,
116  via_device=(
117  DOMAIN,
118  parent_id,
119  ),
120  )
121  super().__init__(device=domain, parent_id=parent_id, entry_id=entry_id)
122 
123  @property
124  def available(self) -> bool:
125  """Return false if status is unavailable."""
126  return self._device_device.system_availability
None __init__(self, SHCDevice|SHCIntrusionSystem device, str parent_id, str entry_id)
Definition: entity.py:33
None __init__(self, SHCIntrusionSystem domain, str parent_id, str entry_id)
Definition: entity.py:108
None __init__(self, SHCDevice device, str parent_id, str entry_id)
Definition: entity.py:64
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_remove_devices(HomeAssistant hass, SHCBaseEntity entity, str entry_id)
Definition: entity.py:17