Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """StarLine base entity."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 
7 from homeassistant.helpers.entity import Entity
8 
9 from .account import StarlineAccount, StarlineDevice
10 
11 
13  """StarLine base entity class."""
14 
15  _attr_should_poll = False
16  _attr_has_entity_name = True
17 
18  def __init__(
19  self, account: StarlineAccount, device: StarlineDevice, key: str
20  ) -> None:
21  """Initialize StarLine entity."""
22  self._account_account = account
23  self._device_device = device
24  self._key_key = key
25  self._attr_unique_id_attr_unique_id = f"starline-{key}-{device.device_id}"
26  self._attr_device_info_attr_device_info = account.device_info(device)
27  self._unsubscribe_api_unsubscribe_api: Callable | None = None
28 
29  @property
30  def available(self):
31  """Return True if entity is available."""
32  return self._account_account.api.available
33 
34  def update(self):
35  """Read new state data."""
36  self.schedule_update_ha_stateschedule_update_ha_state()
37 
38  async def async_added_to_hass(self):
39  """Call when entity about to be added to Home Assistant."""
40  await super().async_added_to_hass()
41  self._unsubscribe_api_unsubscribe_api = self._account_account.api.add_update_listener(self.updateupdate)
42 
43  async def async_will_remove_from_hass(self):
44  """Call when entity is being removed from Home Assistant."""
45  await super().async_will_remove_from_hass()
46  if self._unsubscribe_api_unsubscribe_api is not None:
47  self._unsubscribe_api_unsubscribe_api()
48  self._unsubscribe_api_unsubscribe_api = None
None __init__(self, StarlineAccount account, StarlineDevice device, str key)
Definition: entity.py:20
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244