Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Broadlink entities."""
2 
3 from homeassistant.helpers import device_registry as dr
4 from homeassistant.helpers.device_registry import DeviceInfo
5 from homeassistant.helpers.entity import Entity
6 
7 from .const import DOMAIN
8 
9 
11  """Representation of a Broadlink entity."""
12 
13  _attr_should_poll = False
14 
15  def __init__(self, device):
16  """Initialize the entity."""
17  self._device_device = device
18  self._coordinator_coordinator = device.update_manager.coordinator
19 
20  async def async_added_to_hass(self):
21  """Call when the entity is added to hass."""
22  self.async_on_removeasync_on_remove(self._coordinator_coordinator.async_add_listener(self._recv_data_recv_data))
23  if self._coordinator_coordinator.data:
24  self._update_state_update_state(self._coordinator_coordinator.data)
25 
26  async def async_update(self):
27  """Update the state of the entity."""
28  await self._coordinator_coordinator.async_request_refresh()
29 
30  def _recv_data(self):
31  """Receive data from the update coordinator.
32 
33  This event listener should be called by the coordinator whenever
34  there is an update available.
35 
36  It works as a template for the _update_state() method, which should
37  be overridden by child classes in order to update the state of the
38  entities, when applicable.
39  """
40  if self._coordinator_coordinator.last_update_success:
41  self._update_state_update_state(self._coordinator_coordinator.data)
42  self.async_write_ha_stateasync_write_ha_state()
43 
44  def _update_state(self, data):
45  """Update the state of the entity.
46 
47  This method should be overridden by child classes in order to
48  internalize state and attributes received from the coordinator.
49  """
50 
51  @property
52  def available(self):
53  """Return True if the entity is available."""
54  return self._device_device.available
55 
56  @property
57  def device_info(self) -> DeviceInfo:
58  """Return device info."""
59  device = self._device_device
60 
61  return DeviceInfo(
62  connections={(dr.CONNECTION_NETWORK_MAC, device.mac_address)},
63  identifiers={(DOMAIN, device.unique_id)},
64  manufacturer=device.api.manufacturer,
65  model=device.api.model,
66  name=device.name,
67  sw_version=device.fw_version,
68  )
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_add_listener(HomeAssistant hass, Callable[[], None] listener)
Definition: __init__.py:82