Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Tellstick Net/Telstick Live sensors."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components import sensor
7  SensorDeviceClass,
8  SensorEntity,
9  SensorEntityDescription,
10  SensorStateClass,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import (
14  LIGHT_LUX,
15  PERCENTAGE,
16  UV_INDEX,
17  UnitOfPower,
18  UnitOfPrecipitationDepth,
19  UnitOfPressure,
20  UnitOfSpeed,
21  UnitOfTemperature,
22  UnitOfVolumetricFlux,
23 )
24 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.dispatcher import async_dispatcher_connect
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 
28 from .const import DOMAIN, TELLDUS_DISCOVERY_NEW
29 from .entity import TelldusLiveEntity
30 
31 SENSOR_TYPE_TEMPERATURE = "temp"
32 SENSOR_TYPE_HUMIDITY = "humidity"
33 SENSOR_TYPE_RAINRATE = "rrate"
34 SENSOR_TYPE_RAINTOTAL = "rtot"
35 SENSOR_TYPE_WINDDIRECTION = "wdir"
36 SENSOR_TYPE_WINDAVERAGE = "wavg"
37 SENSOR_TYPE_WINDGUST = "wgust"
38 SENSOR_TYPE_UV = "uv"
39 SENSOR_TYPE_WATT = "watt"
40 SENSOR_TYPE_LUMINANCE = "lum"
41 SENSOR_TYPE_DEW_POINT = "dewp"
42 SENSOR_TYPE_BAROMETRIC_PRESSURE = "barpress"
43 
44 SENSOR_TYPES: dict[str, SensorEntityDescription] = {
45  SENSOR_TYPE_TEMPERATURE: SensorEntityDescription(
46  key=SENSOR_TYPE_TEMPERATURE,
47  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
48  device_class=SensorDeviceClass.TEMPERATURE,
49  state_class=SensorStateClass.MEASUREMENT,
50  ),
51  SENSOR_TYPE_HUMIDITY: SensorEntityDescription(
52  key=SENSOR_TYPE_HUMIDITY,
53  native_unit_of_measurement=PERCENTAGE,
54  device_class=SensorDeviceClass.HUMIDITY,
55  state_class=SensorStateClass.MEASUREMENT,
56  ),
57  SENSOR_TYPE_RAINRATE: SensorEntityDescription(
58  key=SENSOR_TYPE_RAINRATE,
59  native_unit_of_measurement=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
60  state_class=SensorStateClass.MEASUREMENT,
61  device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
62  ),
63  SENSOR_TYPE_RAINTOTAL: SensorEntityDescription(
64  key=SENSOR_TYPE_RAINTOTAL,
65  native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS,
66  device_class=SensorDeviceClass.PRECIPITATION,
67  state_class=SensorStateClass.TOTAL_INCREASING,
68  ),
69  SENSOR_TYPE_WINDDIRECTION: SensorEntityDescription(
70  key=SENSOR_TYPE_WINDDIRECTION,
71  translation_key="wind_direction",
72  ),
73  SENSOR_TYPE_WINDAVERAGE: SensorEntityDescription(
74  key=SENSOR_TYPE_WINDAVERAGE,
75  translation_key="wind_average",
76  native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
77  device_class=SensorDeviceClass.WIND_SPEED,
78  state_class=SensorStateClass.MEASUREMENT,
79  ),
80  SENSOR_TYPE_WINDGUST: SensorEntityDescription(
81  key=SENSOR_TYPE_WINDGUST,
82  translation_key="wind_gust",
83  native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
84  device_class=SensorDeviceClass.WIND_SPEED,
85  state_class=SensorStateClass.MEASUREMENT,
86  ),
87  SENSOR_TYPE_UV: SensorEntityDescription(
88  key=SENSOR_TYPE_UV,
89  translation_key="uv",
90  native_unit_of_measurement=UV_INDEX,
91  state_class=SensorStateClass.MEASUREMENT,
92  ),
93  SENSOR_TYPE_WATT: SensorEntityDescription(
94  key=SENSOR_TYPE_WATT,
95  native_unit_of_measurement=UnitOfPower.WATT,
96  device_class=SensorDeviceClass.POWER,
97  state_class=SensorStateClass.MEASUREMENT,
98  ),
99  SENSOR_TYPE_LUMINANCE: SensorEntityDescription(
100  key=SENSOR_TYPE_LUMINANCE,
101  native_unit_of_measurement=LIGHT_LUX,
102  device_class=SensorDeviceClass.ILLUMINANCE,
103  state_class=SensorStateClass.MEASUREMENT,
104  ),
105  SENSOR_TYPE_DEW_POINT: SensorEntityDescription(
106  key=SENSOR_TYPE_DEW_POINT,
107  translation_key="dew_point",
108  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
109  device_class=SensorDeviceClass.TEMPERATURE,
110  state_class=SensorStateClass.MEASUREMENT,
111  ),
112  SENSOR_TYPE_BAROMETRIC_PRESSURE: SensorEntityDescription(
113  key=SENSOR_TYPE_BAROMETRIC_PRESSURE,
114  native_unit_of_measurement=UnitOfPressure.KPA,
115  device_class=SensorDeviceClass.PRESSURE,
116  state_class=SensorStateClass.MEASUREMENT,
117  ),
118 }
119 
120 
122  hass: HomeAssistant,
123  config_entry: ConfigEntry,
124  async_add_entities: AddEntitiesCallback,
125 ) -> None:
126  """Set up tellduslive sensors dynamically."""
127 
128  async def async_discover_sensor(device_id):
129  """Discover and add a discovered sensor."""
130  client = hass.data[DOMAIN]
131  async_add_entities([TelldusLiveSensor(client, device_id)])
132 
134  hass,
135  TELLDUS_DISCOVERY_NEW.format(sensor.DOMAIN, DOMAIN),
136  async_discover_sensor,
137  )
138 
139 
141  """Representation of a Telldus Live sensor."""
142 
143  def __init__(self, client, device_id):
144  """Initialize TelldusLiveSensor."""
145  super().__init__(client, device_id)
146  if desc := SENSOR_TYPES.get(self._type_type_type):
147  self.entity_descriptionentity_description = desc
148  else:
149  self._attr_name_attr_name = None
150 
151  @property
152  def device_id(self):
153  """Return id of the device."""
154  return self._id_id[0]
155 
156  @property
157  def _type(self):
158  """Return the type of the sensor."""
159  return self._id_id[1]
160 
161  @property
162  def _value(self):
163  """Return value of the sensor."""
164  return self.devicedevice.value(*self._id_id[1:])
165 
166  @property
168  """Return the value as temperature."""
169  return round(float(self._value_value), 1)
170 
171  @property
173  """Return the value as luminance."""
174  return round(float(self._value_value), 1)
175 
176  @property
178  """Return the value as humidity."""
179  return int(round(float(self._value_value)))
180 
181  @property
182  def native_value(self):
183  """Return the state of the sensor."""
184  if not self.availableavailableavailable:
185  return None
186  if self._type_type_type == SENSOR_TYPE_TEMPERATURE:
187  return self._value_as_temperature_value_as_temperature
188  if self._type_type_type == SENSOR_TYPE_HUMIDITY:
189  return self._value_as_humidity_value_as_humidity
190  if self._type_type_type == SENSOR_TYPE_LUMINANCE:
191  return self._value_as_luminance_value_as_luminance
192  return self._value_value
193 
194  @property
195  def unique_id(self) -> str:
196  """Return a unique ID."""
197  return "-".join(map(str, self._id_id))
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:125
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103