Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The Ecowitt Weather Station Entity."""
2 
3 from __future__ import annotations
4 
5 import time
6 
7 from aioecowitt import EcoWittSensor
8 
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.entity import Entity
11 
12 from .const import DOMAIN
13 
14 
16  """Base class for Ecowitt Weather Station."""
17 
18  _attr_has_entity_name = True
19  _attr_should_poll = False
20 
21  def __init__(self, sensor: EcoWittSensor) -> None:
22  """Construct the entity."""
23  self.ecowitt: EcoWittSensor = sensor
24 
25  self._attr_unique_id_attr_unique_id = f"{sensor.station.key}-{sensor.key}"
26  self._attr_device_info_attr_device_info = DeviceInfo(
27  identifiers={
28  (DOMAIN, sensor.station.key),
29  },
30  name=sensor.station.model,
31  model=sensor.station.model,
32  sw_version=sensor.station.version,
33  )
34 
35  async def async_added_to_hass(self) -> None:
36  """Install listener for updates later."""
37 
38  def _update_state() -> None:
39  """Update the state on callback."""
40  self.async_write_ha_stateasync_write_ha_state()
41 
42  self.ecowitt.update_cb.append(_update_state)
43  self.async_on_removeasync_on_remove(lambda: self.ecowitt.update_cb.remove(_update_state))
44 
45  @property
46  def available(self) -> bool:
47  """Return whether the state is based on actual reading from device."""
48  return (self.ecowitt.last_update_m + 5 * 60) > time.monotonic()
None __init__(self, EcoWittSensor sensor)
Definition: entity.py:21
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331