Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for IQVIA."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import callback
9 from homeassistant.helpers.entity import EntityDescription
11  CoordinatorEntity,
12  DataUpdateCoordinator,
13 )
14 
15 from .const import CONF_ZIP_CODE, DOMAIN, TYPE_ALLERGY_FORECAST, TYPE_ALLERGY_OUTLOOK
16 
17 
18 class IQVIAEntity(CoordinatorEntity[DataUpdateCoordinator[dict[str, Any]]]):
19  """Define a base IQVIA entity."""
20 
21  _attr_has_entity_name = True
22 
23  def __init__(
24  self,
25  coordinator: DataUpdateCoordinator[dict[str, Any]],
26  entry: ConfigEntry,
27  description: EntityDescription,
28  ) -> None:
29  """Initialize."""
30  super().__init__(coordinator)
31 
32  self._attr_extra_state_attributes_attr_extra_state_attributes = {}
33  self._attr_unique_id_attr_unique_id = f"{entry.data[CONF_ZIP_CODE]}_{description.key}"
34  self._entry_entry = entry
35  self.entity_descriptionentity_description = description
36 
37  @callback
38  def _handle_coordinator_update(self) -> None:
39  """Handle updated data from the coordinator."""
40  if not self.coordinator.last_update_success:
41  return
42 
43  self.update_from_latest_dataupdate_from_latest_data()
44  self.async_write_ha_state()
45 
46  async def async_added_to_hass(self) -> None:
47  """Register callbacks."""
48  await super().async_added_to_hass()
49 
50  if self.entity_descriptionentity_description.key == TYPE_ALLERGY_FORECAST:
51  self.async_on_remove(
52  self.hasshass.data[DOMAIN][self._entry_entry.entry_id][
53  TYPE_ALLERGY_OUTLOOK
54  ].async_add_listener(self._handle_coordinator_update_handle_coordinator_update)
55  )
56 
57  self.update_from_latest_dataupdate_from_latest_data()
58 
59  @callback
60  def update_from_latest_data(self) -> None:
61  """Update the entity from the latest data."""
62  raise NotImplementedError
None __init__(self, DataUpdateCoordinator[dict[str, Any]] coordinator, ConfigEntry entry, EntityDescription description)
Definition: entity.py:28
Callable[[], None] async_add_listener(self, CALLBACK_TYPE update_callback, Any context=None)