Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Minut Point."""
2 
3 import logging
4 
5 from homeassistant.helpers import device_registry as dr
6 from homeassistant.helpers.device_registry import DeviceInfo
7 from homeassistant.helpers.dispatcher import async_dispatcher_connect
8 from homeassistant.helpers.entity import Entity
9 from homeassistant.util.dt import as_local, parse_datetime, utc_from_timestamp
10 
11 from .const import DOMAIN, SIGNAL_UPDATE_ENTITY
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 
17  """Base Entity used by the sensors."""
18 
19  _attr_should_poll = False
20 
21  def __init__(self, point_client, device_id, device_class) -> None:
22  """Initialize the entity."""
23  self._async_unsub_dispatcher_connect_async_unsub_dispatcher_connect = None
24  self._client_client = point_client
25  self._id_id = device_id
26  self._name_name = self.devicedevice.name
27  self._attr_device_class_attr_device_class = device_class
28  self._updated_updated = utc_from_timestamp(0)
29  self._attr_unique_id_attr_unique_id = f"point.{device_id}-{device_class}"
30  device = self.devicedevice.device
31  self._attr_device_info_attr_device_info = DeviceInfo(
32  connections={(dr.CONNECTION_NETWORK_MAC, device["device_mac"])},
33  identifiers={(DOMAIN, device["device_id"])},
34  manufacturer="Minut",
35  model=f"Point v{device['hardware_version']}",
36  name=device["description"],
37  sw_version=device["firmware"]["installed"],
38  via_device=(DOMAIN, device["home"]),
39  )
40  if device_class:
41  self._attr_name_attr_name = f"{self._name} {device_class.capitalize()}"
42 
43  def __str__(self) -> str:
44  """Return string representation of device."""
45  return f"MinutPoint {self.name}"
46 
47  async def async_added_to_hass(self):
48  """Call when entity is added to hass."""
49  _LOGGER.debug("Created device %s", self)
50  self._async_unsub_dispatcher_connect_async_unsub_dispatcher_connect = async_dispatcher_connect(
51  self.hasshass, SIGNAL_UPDATE_ENTITY, self._update_callback_update_callback
52  )
53  await self._update_callback_update_callback()
54 
55  async def async_will_remove_from_hass(self):
56  """Disconnect dispatcher listener when removed."""
57  if self._async_unsub_dispatcher_connect_async_unsub_dispatcher_connect:
58  self._async_unsub_dispatcher_connect_async_unsub_dispatcher_connect()
59 
60  async def _update_callback(self):
61  """Update the value of the sensor."""
62 
63  @property
64  def available(self):
65  """Return true if device is not offline."""
66  return self._client_client.is_available(self.device_iddevice_id)
67 
68  @property
69  def device(self):
70  """Return the representation of the device."""
71  return self._client_client.device(self.device_iddevice_id)
72 
73  @property
74  def device_id(self):
75  """Return the id of the device."""
76  return self._id_id
77 
78  @property
80  """Return status of device."""
81  attrs = self.devicedevice.device_status
82  attrs["last_heard_from"] = as_local(self.last_updatelast_update).strftime(
83  "%Y-%m-%d %H:%M:%S"
84  )
85  return attrs
86 
87  @property
88  def is_updated(self):
89  """Return true if sensor have been updated."""
90  return self.last_updatelast_update > self._updated_updated
91 
92  @property
93  def last_update(self):
94  """Return the last_update time for the device."""
95  return parse_datetime(self.devicedevice.last_update)
None __init__(self, point_client, device_id, device_class)
Definition: entity.py:21
datetime|None parse_datetime(str|None value)
Definition: sensor.py:138
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103
dt.datetime as_local(dt.datetime dattim)
Definition: dt.py:157