Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity Ambient Weather Station Service."""
2 
3 from __future__ import annotations
4 
5 from aioambient.util import get_public_device_id
6 
7 from homeassistant.core import callback
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity, EntityDescription
11 
12 from . import AmbientStation
13 from .const import ATTR_LAST_DATA, DOMAIN, TYPE_SOLARRADIATION, TYPE_SOLARRADIATION_LX
14 
15 
17  """Define a base Ambient PWS entity."""
18 
19  _attr_has_entity_name = True
20  _attr_should_poll = False
21 
22  def __init__(
23  self,
24  ambient: AmbientStation,
25  mac_address: str,
26  station_name: str,
27  description: EntityDescription,
28  ) -> None:
29  """Initialize the entity."""
30  self._ambient_ambient = ambient
31 
32  public_device_id = get_public_device_id(mac_address)
33  self._attr_device_info_attr_device_info = DeviceInfo(
34  configuration_url=(
35  f"https://ambientweather.net/dashboard/{public_device_id}"
36  ),
37  identifiers={(DOMAIN, mac_address)},
38  manufacturer="Ambient Weather",
39  name=station_name.capitalize(),
40  )
41 
42  self._attr_unique_id_attr_unique_id = f"{mac_address}_{description.key}"
43  self._mac_address_mac_address = mac_address
44  self.entity_descriptionentity_description = description
45 
46  @callback
47  def _async_update(self) -> None:
48  """Update the state."""
49  last_data = self._ambient_ambient.stations[self._mac_address_mac_address][ATTR_LAST_DATA]
50  key = self.entity_descriptionentity_description.key
51  available_key = TYPE_SOLARRADIATION if key == TYPE_SOLARRADIATION_LX else key
52  self._attr_available_attr_available = last_data.get(available_key) is not None
53  self.update_from_latest_dataupdate_from_latest_data()
54  self.async_write_ha_stateasync_write_ha_state()
55 
56  async def async_added_to_hass(self) -> None:
57  """Register callbacks."""
58  self.async_on_removeasync_on_remove(
60  self.hasshass,
61  f"ambient_station_data_update_{self._mac_address}",
62  self._async_update_async_update,
63  )
64  )
65 
66  self.update_from_latest_dataupdate_from_latest_data()
67 
68  @callback
69  def update_from_latest_data(self) -> None:
70  """Update the entity from the latest data."""
71  raise NotImplementedError
None __init__(self, AmbientStation ambient, str mac_address, str station_name, EntityDescription description)
Definition: entity.py:28
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103