Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The baf integration entities."""
2 
3 from __future__ import annotations
4 
5 from aiobafi6 import Device
6 
7 from homeassistant.core import callback
8 from homeassistant.helpers import device_registry as dr
9 from homeassistant.helpers.device_registry import DeviceInfo, format_mac
10 from homeassistant.helpers.entity import Entity, EntityDescription
11 
12 
14  """Base class for baf entities."""
15 
16  _attr_should_poll = False
17  _attr_has_entity_name = True
18 
19  def __init__(self, device: Device) -> None:
20  """Initialize the entity."""
21  self._device_device = device
22  self._attr_unique_id_attr_unique_id = format_mac(self._device_device.mac_address)
23  self._attr_device_info_attr_device_info = DeviceInfo(
24  connections={(dr.CONNECTION_NETWORK_MAC, self._device_device.mac_address)},
25  name=self._device_device.name,
26  manufacturer="Big Ass Fans",
27  model=self._device_device.model,
28  sw_version=self._device_device.firmware_version,
29  )
30  self._async_update_attrs_async_update_attrs()
31 
32  @callback
33  def _async_update_attrs(self) -> None:
34  """Update attrs from device."""
35  self._attr_available_attr_available = self._device_device.available
36 
37  @callback
38  def _async_update_from_device(self, device: Device) -> None:
39  """Process an update from the device."""
40  self._async_update_attrs_async_update_attrs()
41  self.async_write_ha_stateasync_write_ha_state()
42 
43  async def async_added_to_hass(self) -> None:
44  """Add data updated listener after this object has been initialized."""
45  self._device_device.add_callback(self._async_update_from_device_async_update_from_device)
46 
47  async def async_will_remove_from_hass(self) -> None:
48  """Remove data updated listener after this object has been initialized."""
49  self._device_device.remove_callback(self._async_update_from_device_async_update_from_device)
50 
51 
53  """Base class for baf entities that use an entity description."""
54 
55  def __init__(self, device: Device, description: EntityDescription) -> None:
56  """Initialize the entity."""
57  self.entity_descriptionentity_description = description
58  super().__init__(device)
59  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{device.mac_address}-{description.key}"
None __init__(self, Device device, EntityDescription description)
Definition: entity.py:55
None _async_update_from_device(self, Device device)
Definition: entity.py:38
None __init__(self, Device device)
Definition: entity.py:19