Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity class for Flo entities."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
6 from homeassistant.helpers.entity import Entity
7 
8 from .const import DOMAIN as FLO_DOMAIN
9 from .coordinator import FloDeviceDataUpdateCoordinator
10 
11 
13  """A base class for Flo entities."""
14 
15  _attr_force_update = False
16  _attr_has_entity_name = True
17  _attr_should_poll = False
18 
19  def __init__(
20  self,
21  entity_type: str,
22  device: FloDeviceDataUpdateCoordinator,
23  **kwargs,
24  ) -> None:
25  """Init Flo entity."""
26  self._attr_unique_id_attr_unique_id = f"{device.mac_address}_{entity_type}"
27 
28  self._device: FloDeviceDataUpdateCoordinator = device
29 
30  @property
31  def device_info(self) -> DeviceInfo:
32  """Return a device description for device registry."""
33  return DeviceInfo(
34  connections={(CONNECTION_NETWORK_MAC, self._device.mac_address)},
35  identifiers={(FLO_DOMAIN, self._device.id)},
36  serial_number=self._device.serial_number,
37  manufacturer=self._device.manufacturer,
38  model=self._device.model,
39  name=self._device.device_name.capitalize(),
40  sw_version=self._device.firmware_version,
41  )
42 
43  @property
44  def available(self) -> bool:
45  """Return True if device is available."""
46  return self._device.available
47 
48  async def async_update(self):
49  """Update Flo entity."""
50  await self._device.async_request_refresh()
51 
52  async def async_added_to_hass(self):
53  """When entity is added to hass."""
54  self.async_on_removeasync_on_remove(self._device.async_add_listener(self.async_write_ha_stateasync_write_ha_state))
None __init__(self, str entity_type, FloDeviceDataUpdateCoordinator device, **kwargs)
Definition: entity.py:24
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