Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Ambient Weather Network sensors."""
2 
3 from __future__ import annotations
4 
5 from datetime import datetime
6 
8  SensorDeviceClass,
9  SensorEntity,
10  SensorEntityDescription,
11  SensorStateClass,
12 )
13 from homeassistant.const import (
14  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
15  CONCENTRATION_PARTS_PER_MILLION,
16  CONF_MAC,
17  DEGREE,
18  PERCENTAGE,
19  UnitOfIrradiance,
20  UnitOfLength,
21  UnitOfPrecipitationDepth,
22  UnitOfPressure,
23  UnitOfSpeed,
24  UnitOfTemperature,
25  UnitOfVolumetricFlux,
26 )
27 from homeassistant.core import HomeAssistant
28 from homeassistant.helpers.entity_platform import AddEntitiesCallback
29 from homeassistant.util import dt as dt_util
30 
31 from . import AmbientNetworkConfigEntry
32 from .coordinator import AmbientNetworkDataUpdateCoordinator
33 from .entity import AmbientNetworkEntity
34 
35 TYPE_AQI_PM25 = "aqi_pm25"
36 TYPE_AQI_PM25_24H = "aqi_pm25_24h"
37 TYPE_BAROMABSIN = "baromabsin"
38 TYPE_BAROMRELIN = "baromrelin"
39 TYPE_CO2 = "co2"
40 TYPE_DAILYRAININ = "dailyrainin"
41 TYPE_DEWPOINT = "dewPoint"
42 TYPE_EVENTRAININ = "eventrainin"
43 TYPE_FEELSLIKE = "feelsLike"
44 TYPE_HOURLYRAININ = "hourlyrainin"
45 TYPE_HUMIDITY = "humidity"
46 TYPE_LASTRAIN = "lastRain"
47 TYPE_LIGHTNING_DISTANCE = "lightning_distance"
48 TYPE_LIGHTNING_PER_DAY = "lightning_day"
49 TYPE_LIGHTNING_PER_HOUR = "lightning_hour"
50 TYPE_MAXDAILYGUST = "maxdailygust"
51 TYPE_MONTHLYRAININ = "monthlyrainin"
52 TYPE_PM25 = "pm25"
53 TYPE_PM25_24H = "pm25_24h"
54 TYPE_SOLARRADIATION = "solarradiation"
55 TYPE_TEMPF = "tempf"
56 TYPE_UV = "uv"
57 TYPE_WEEKLYRAININ = "weeklyrainin"
58 TYPE_WINDDIR = "winddir"
59 TYPE_WINDGUSTMPH = "windgustmph"
60 TYPE_WINDSPEEDMPH = "windspeedmph"
61 TYPE_YEARLYRAININ = "yearlyrainin"
62 
63 
64 SENSOR_DESCRIPTIONS = (
66  key=TYPE_AQI_PM25,
67  translation_key="pm25_aqi",
68  device_class=SensorDeviceClass.AQI,
69  state_class=SensorStateClass.MEASUREMENT,
70  suggested_display_precision=0,
71  ),
73  key=TYPE_AQI_PM25_24H,
74  translation_key="pm25_aqi_24h_average",
75  device_class=SensorDeviceClass.AQI,
76  state_class=SensorStateClass.MEASUREMENT,
77  suggested_display_precision=0,
78  entity_registry_enabled_default=False,
79  ),
81  key=TYPE_BAROMABSIN,
82  translation_key="absolute_pressure",
83  native_unit_of_measurement=UnitOfPressure.INHG,
84  device_class=SensorDeviceClass.PRESSURE,
85  state_class=SensorStateClass.MEASUREMENT,
86  suggested_display_precision=2,
87  entity_registry_enabled_default=False,
88  ),
90  key=TYPE_BAROMRELIN,
91  translation_key="relative_pressure",
92  native_unit_of_measurement=UnitOfPressure.INHG,
93  device_class=SensorDeviceClass.PRESSURE,
94  state_class=SensorStateClass.MEASUREMENT,
95  suggested_display_precision=2,
96  ),
98  key=TYPE_CO2,
99  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
100  device_class=SensorDeviceClass.CO2,
101  state_class=SensorStateClass.MEASUREMENT,
102  suggested_display_precision=2,
103  entity_registry_enabled_default=False,
104  ),
106  key=TYPE_DAILYRAININ,
107  translation_key="daily_rain",
108  native_unit_of_measurement=UnitOfPrecipitationDepth.INCHES,
109  device_class=SensorDeviceClass.PRECIPITATION,
110  state_class=SensorStateClass.TOTAL,
111  suggested_display_precision=2,
112  ),
114  key=TYPE_DEWPOINT,
115  translation_key="dew_point",
116  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
117  device_class=SensorDeviceClass.TEMPERATURE,
118  state_class=SensorStateClass.MEASUREMENT,
119  suggested_display_precision=1,
120  ),
122  key=TYPE_FEELSLIKE,
123  translation_key="feels_like",
124  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
125  device_class=SensorDeviceClass.TEMPERATURE,
126  state_class=SensorStateClass.MEASUREMENT,
127  suggested_display_precision=1,
128  ),
130  key=TYPE_HOURLYRAININ,
131  translation_key="hourly_rain",
132  native_unit_of_measurement=UnitOfVolumetricFlux.INCHES_PER_HOUR,
133  state_class=SensorStateClass.MEASUREMENT,
134  device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
135  suggested_display_precision=2,
136  ),
138  key=TYPE_HUMIDITY,
139  native_unit_of_measurement=PERCENTAGE,
140  device_class=SensorDeviceClass.HUMIDITY,
141  state_class=SensorStateClass.MEASUREMENT,
142  suggested_display_precision=1,
143  ),
145  key=TYPE_LASTRAIN,
146  translation_key="last_rain",
147  device_class=SensorDeviceClass.TIMESTAMP,
148  entity_registry_enabled_default=False,
149  ),
151  key=TYPE_LIGHTNING_PER_DAY,
152  translation_key="lightning_strikes_per_day",
153  native_unit_of_measurement="strikes",
154  state_class=SensorStateClass.TOTAL,
155  entity_registry_enabled_default=False,
156  ),
158  key=TYPE_LIGHTNING_PER_HOUR,
159  translation_key="lightning_strikes_per_hour",
160  native_unit_of_measurement="strikes/hour",
161  state_class=SensorStateClass.TOTAL,
162  entity_registry_enabled_default=False,
163  ),
165  key=TYPE_LIGHTNING_DISTANCE,
166  translation_key="lightning_distance",
167  native_unit_of_measurement=UnitOfLength.MILES,
168  device_class=SensorDeviceClass.DISTANCE,
169  state_class=SensorStateClass.MEASUREMENT,
170  suggested_display_precision=2,
171  entity_registry_enabled_default=False,
172  ),
174  key=TYPE_MAXDAILYGUST,
175  translation_key="max_daily_gust",
176  native_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
177  device_class=SensorDeviceClass.WIND_SPEED,
178  state_class=SensorStateClass.MEASUREMENT,
179  suggested_display_precision=1,
180  ),
182  key=TYPE_MONTHLYRAININ,
183  translation_key="monthly_rain",
184  native_unit_of_measurement=UnitOfPrecipitationDepth.INCHES,
185  device_class=SensorDeviceClass.PRECIPITATION,
186  state_class=SensorStateClass.TOTAL,
187  suggested_display_precision=2,
188  entity_registry_enabled_default=False,
189  ),
191  key=TYPE_PM25_24H,
192  translation_key="pm25_24h_average",
193  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
194  device_class=SensorDeviceClass.PM25,
195  suggested_display_precision=1,
196  entity_registry_enabled_default=False,
197  ),
199  key=TYPE_PM25,
200  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
201  device_class=SensorDeviceClass.PM25,
202  state_class=SensorStateClass.MEASUREMENT,
203  suggested_display_precision=1,
204  entity_registry_enabled_default=False,
205  ),
207  key=TYPE_SOLARRADIATION,
208  native_unit_of_measurement=UnitOfIrradiance.WATTS_PER_SQUARE_METER,
209  device_class=SensorDeviceClass.IRRADIANCE,
210  state_class=SensorStateClass.MEASUREMENT,
211  suggested_display_precision=1,
212  entity_registry_enabled_default=False,
213  ),
215  key=TYPE_TEMPF,
216  native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
217  device_class=SensorDeviceClass.TEMPERATURE,
218  state_class=SensorStateClass.MEASUREMENT,
219  suggested_display_precision=1,
220  ),
222  key=TYPE_UV,
223  translation_key="uv_index",
224  native_unit_of_measurement="index",
225  state_class=SensorStateClass.MEASUREMENT,
226  suggested_display_precision=1,
227  ),
229  key=TYPE_WEEKLYRAININ,
230  translation_key="weekly_rain",
231  native_unit_of_measurement=UnitOfPrecipitationDepth.INCHES,
232  device_class=SensorDeviceClass.PRECIPITATION,
233  state_class=SensorStateClass.TOTAL,
234  suggested_display_precision=2,
235  entity_registry_enabled_default=False,
236  ),
238  key=TYPE_WINDDIR,
239  translation_key="wind_direction",
240  native_unit_of_measurement=DEGREE,
241  suggested_display_precision=0,
242  entity_registry_enabled_default=False,
243  ),
245  key=TYPE_WINDGUSTMPH,
246  translation_key="wind_gust",
247  native_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
248  device_class=SensorDeviceClass.WIND_SPEED,
249  state_class=SensorStateClass.MEASUREMENT,
250  suggested_display_precision=1,
251  ),
253  key=TYPE_WINDSPEEDMPH,
254  native_unit_of_measurement=UnitOfSpeed.MILES_PER_HOUR,
255  device_class=SensorDeviceClass.WIND_SPEED,
256  state_class=SensorStateClass.MEASUREMENT,
257  suggested_display_precision=1,
258  ),
260  key=TYPE_YEARLYRAININ,
261  translation_key="yearly_rain",
262  native_unit_of_measurement=UnitOfPrecipitationDepth.INCHES,
263  device_class=SensorDeviceClass.PRECIPITATION,
264  state_class=SensorStateClass.TOTAL,
265  suggested_display_precision=2,
266  entity_registry_enabled_default=False,
267  ),
268 )
269 
270 
272  hass: HomeAssistant,
273  entry: AmbientNetworkConfigEntry,
274  async_add_entities: AddEntitiesCallback,
275 ) -> None:
276  """Set up the Ambient Network sensor entities."""
277 
278  coordinator = entry.runtime_data
279  if coordinator.config_entry is not None:
282  coordinator,
283  description,
284  coordinator.config_entry.data[CONF_MAC],
285  )
286  for description in SENSOR_DESCRIPTIONS
287  if coordinator.data.get(description.key) is not None
288  )
289 
290 
292  """A sensor implementation for an Ambient Weather Network sensor."""
293 
294  def __init__(
295  self,
296  coordinator: AmbientNetworkDataUpdateCoordinator,
297  description: SensorEntityDescription,
298  mac_address: str,
299  ) -> None:
300  """Initialize a sensor object."""
301  super().__init__(coordinator, description, mac_address)
302 
303  def _update_attrs(self) -> None:
304  """Update sensor attributes."""
305  value = self.coordinator.data.get(self.entity_descriptionentity_description.key)
306 
307  # Treatments for special units.
308  if value is not None and self.device_classdevice_classdevice_classdevice_class == SensorDeviceClass.TIMESTAMP:
309  value = datetime.fromtimestamp(
310  value / 1000, tz=dt_util.get_default_time_zone()
311  )
312 
313  self._attr_available_attr_available = value is not None
314  self._attr_native_value_attr_native_value = value
315 
316  if self.coordinator.last_measured is not None:
317  self._attr_extra_state_attributes_attr_extra_state_attributes = {
318  "last_measured": self.coordinator.last_measured
319  }
None __init__(self, AmbientNetworkDataUpdateCoordinator coordinator, SensorEntityDescription description, str mac_address)
Definition: sensor.py:299
SensorDeviceClass|None device_class(self)
Definition: __init__.py:313
None async_setup_entry(HomeAssistant hass, AmbientNetworkConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:275