Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base Entity for all TelldusLive entities."""
2 
3 from datetime import datetime
4 import logging
5 
6 from tellduslive import BATTERY_LOW, BATTERY_OK, BATTERY_UNKNOWN
7 
8 from homeassistant.const import (
9  ATTR_BATTERY_LEVEL,
10  ATTR_MANUFACTURER,
11  ATTR_MODEL,
12  ATTR_VIA_DEVICE,
13 )
14 from homeassistant.helpers.device_registry import DeviceInfo
15 from homeassistant.helpers.dispatcher import async_dispatcher_connect
16 from homeassistant.helpers.entity import Entity
17 
18 from .const import SIGNAL_UPDATE_ENTITY
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 ATTR_LAST_UPDATED = "time_last_updated"
23 
24 
26  """Base class for all Telldus Live entities."""
27 
28  _attr_should_poll = False
29  _attr_has_entity_name = True
30 
31  def __init__(self, client, device_id):
32  """Initialize the entity."""
33  self._id_id = device_id
34  self._client_client = client
35 
36  async def async_added_to_hass(self):
37  """Call when entity is added to hass."""
38  _LOGGER.debug("Created device %s", self)
39  self.async_on_removeasync_on_remove(
41  self.hasshass, SIGNAL_UPDATE_ENTITY, self.async_write_ha_stateasync_write_ha_state
42  )
43  )
44 
45  @property
46  def device_id(self):
47  """Return the id of the device."""
48  return self._id_id
49 
50  @property
51  def device(self):
52  """Return the representation of the device."""
53  return self._client_client.device(self.device_iddevice_id)
54 
55  @property
56  def _state(self):
57  """Return the state of the device."""
58  return self.devicedevice.state
59 
60  @property
61  def assumed_state(self):
62  """Return true if unable to access real state of entity."""
63  return True
64 
65  @property
66  def available(self):
67  """Return true if device is not offline."""
68  return self._client_client.is_available(self.device_iddevice_id)
69 
70  @property
72  """Return the state attributes."""
73  attrs = {}
74  if self._battery_level_battery_level:
75  attrs[ATTR_BATTERY_LEVEL] = self._battery_level_battery_level
76  if self._last_updated_last_updated:
77  attrs[ATTR_LAST_UPDATED] = self._last_updated_last_updated
78  return attrs
79 
80  @property
81  def _battery_level(self):
82  """Return the battery level of a device."""
83  if self.devicedevice.battery == BATTERY_LOW:
84  return 1
85  if self.devicedevice.battery == BATTERY_UNKNOWN:
86  return None
87  if self.devicedevice.battery == BATTERY_OK:
88  return 100
89  return self.devicedevice.battery # Percentage
90 
91  @property
92  def _last_updated(self):
93  """Return the last update of a device."""
94  return (
95  str(datetime.fromtimestamp(self.devicedevice.lastUpdated))
96  if self.devicedevice.lastUpdated
97  else None
98  )
99 
100  @property
101  def unique_id(self) -> str:
102  """Return a unique ID."""
103  return self._id_id
104 
105  @property
106  def device_info(self) -> DeviceInfo:
107  """Return device info."""
108  device = self._client_client.device_info(self.devicedevice.device_id)
109  device_info = DeviceInfo(
110  identifiers={("tellduslive", self.devicedevice.device_id)},
111  name=self.devicedevice.name,
112  )
113  if (model := device.get("model")) is not None:
114  device_info[ATTR_MODEL] = model.title()
115  if (protocol := device.get("protocol")) is not None:
116  device_info[ATTR_MANUFACTURER] = protocol.title()
117  if (client := device.get("client")) is not None:
118  device_info[ATTR_VIA_DEVICE] = ("tellduslive", client)
119  return device_info
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