Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Wireless Sensor Tags."""
2 
3 import logging
4 
5 from homeassistant.const import (
6  ATTR_BATTERY_LEVEL,
7  ATTR_VOLTAGE,
8  PERCENTAGE,
9  SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
10  UnitOfElectricPotential,
11 )
12 from homeassistant.helpers.entity import Entity
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 
17 # Strength of signal in dBm
18 ATTR_TAG_SIGNAL_STRENGTH = "signal_strength"
19 # Indicates if tag is out of range or not
20 ATTR_TAG_OUT_OF_RANGE = "out_of_range"
21 # Number in percents from max power of tag receiver
22 ATTR_TAG_POWER_CONSUMPTION = "power_consumption"
23 
24 
26  """Base class for HA implementation for Wireless Sensor Tag."""
27 
28  def __init__(self, api, tag):
29  """Initialize a base sensor for Wireless Sensor Tag platform."""
30  self._api_api = api
31  self._tag_tag = tag
32  self._uuid_uuid = self._tag_tag.uuid
33  self.tag_idtag_id = self._tag_tag.tag_id
34  self.tag_manager_mactag_manager_mac = self._tag_tag.tag_manager_mac
35  self._name_name = self._tag_tag.name
36  self._state_state = None
37 
38  @property
39  def name(self):
40  """Return the name of the sensor."""
41  return self._name_name
42 
43  @property
44  def principal_value(self):
45  """Return base value.
46 
47  Subclasses need override based on type of sensor.
48  """
49  return 0
50 
52  """Return formatted value.
53 
54  The default implementation formats principal value.
55  """
56  return self.decorate_valuedecorate_value(self.principal_valueprincipal_value)
57 
58  def decorate_value(self, value):
59  """Decorate input value to be well presented for end user."""
60  return f"{value:.1f}"
61 
62  @property
63  def available(self):
64  """Return True if entity is available."""
65  return self._tag_tag.is_alive
66 
67  def update(self):
68  """Update state."""
69  if not self.should_pollshould_poll:
70  return
71 
72  updated_tags = self._api_api.load_tags()
73  if (updated_tag := updated_tags[self._uuid_uuid]) is None:
74  _LOGGER.error('Unable to update tag: "%s"', self.namenamename)
75  return
76 
77  self._tag_tag = updated_tag
78  self._state_state = self.updated_state_valueupdated_state_value()
79 
80  @property
82  """Return the state attributes."""
83  return {
84  ATTR_BATTERY_LEVEL: int(self._tag_tag.battery_remaining * 100),
85  ATTR_VOLTAGE: (
86  f"{self._tag.battery_volts:.2f}{UnitOfElectricPotential.VOLT}"
87  ),
88  ATTR_TAG_SIGNAL_STRENGTH: (
89  f"{self._tag.signal_strength}{SIGNAL_STRENGTH_DECIBELS_MILLIWATT}"
90  ),
91  ATTR_TAG_OUT_OF_RANGE: not self._tag_tag.is_in_range,
92  ATTR_TAG_POWER_CONSUMPTION: (
93  f"{self._tag.power_consumption:.2f}{PERCENTAGE}"
94  ),
95  }
str|UndefinedType|None name(self)
Definition: entity.py:738