Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Component for wiffi support."""
2 
3 from datetime import timedelta
4 
5 from homeassistant.const import CONF_TIMEOUT
6 from homeassistant.core import callback
7 from homeassistant.helpers import device_registry as dr
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity
11 from homeassistant.util.dt import utcnow
12 
13 from .const import CHECK_ENTITIES_SIGNAL, DEFAULT_TIMEOUT, DOMAIN, UPDATE_ENTITY_SIGNAL
14 
15 
16 def generate_unique_id(device, metric):
17  """Generate a unique string for the entity."""
18  return f"{device.mac_address.replace(':', '')}-{metric.name}"
19 
20 
22  """Common functionality for all wiffi entities."""
23 
24  _attr_should_poll = False
25 
26  def __init__(self, device, metric, options):
27  """Initialize the base elements of a wiffi entity."""
28  self._id_id = generate_unique_id(device, metric)
29  self._attr_unique_id_attr_unique_id = self._id_id
30  self._attr_device_info_attr_device_info = DeviceInfo(
31  connections={(dr.CONNECTION_NETWORK_MAC, device.mac_address)},
32  identifiers={(DOMAIN, device.mac_address)},
33  manufacturer="stall.biz",
34  model=device.moduletype,
35  name=f"{device.moduletype} {device.mac_address}",
36  sw_version=device.sw_version,
37  configuration_url=device.configuration_url,
38  )
39  self._attr_name_attr_name = metric.description
40  self._expiration_date_expiration_date = None
41  self._value_value = None
42  self._timeout_timeout = options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)
43 
44  async def async_added_to_hass(self):
45  """Entity has been added to hass."""
46  self.async_on_removeasync_on_remove(
48  self.hasshass,
49  f"{UPDATE_ENTITY_SIGNAL}-{self._id}",
50  self._update_value_callback_update_value_callback,
51  )
52  )
53  self.async_on_removeasync_on_remove(
55  self.hasshass, CHECK_ENTITIES_SIGNAL, self._check_expiration_date_check_expiration_date
56  )
57  )
58 
60  """Reset value expiration date.
61 
62  Will be called by derived classes after a value update has been received.
63  """
64  self._expiration_date_expiration_date = utcnow() + timedelta(minutes=self._timeout_timeout)
65 
66  @callback
67  def _update_value_callback(self, device, metric):
68  """Update the value of the entity."""
69 
70  @callback
72  """Periodically check if entity value has been updated.
73 
74  If there are no more updates from the wiffi device, the value will be
75  set to unavailable.
76  """
77  if (
78  self._value_value is not None
79  and self._expiration_date_expiration_date is not None
80  and utcnow() > self._expiration_date_expiration_date
81  ):
82  self._value_value = None
83  self.async_write_ha_stateasync_write_ha_state()
84 
86  """Measurement entities have a value in present time."""
87  return (
88  not self._attr_name_attr_name.endswith("_gestern") and not self._is_metered_entity_is_metered_entity()
89  )
90 
91  def _is_metered_entity(self):
92  """Metered entities have a value that keeps increasing until reset."""
93  return self._attr_name_attr_name.endswith("_pro_h") or self._attr_name_attr_name.endswith("_heute")
def __init__(self, device, metric, options)
Definition: entity.py:26
def _update_value_callback(self, device, metric)
Definition: entity.py:67
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
def generate_unique_id(device, metric)
Definition: entity.py:16
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103