Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Airthings sensors."""
2 
3 from __future__ import annotations
4 
5 from airthings import AirthingsDevice
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_BILLION,
16  CONCENTRATION_PARTS_PER_MILLION,
17  PERCENTAGE,
18  SIGNAL_STRENGTH_DECIBELS,
19  EntityCategory,
20  UnitOfPressure,
21  UnitOfTemperature,
22 )
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.device_registry import DeviceInfo
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.typing import StateType
27 from homeassistant.helpers.update_coordinator import CoordinatorEntity
28 
29 from . import AirthingsConfigEntry, AirthingsDataCoordinatorType
30 from .const import DOMAIN
31 
32 SENSORS: dict[str, SensorEntityDescription] = {
33  "radonShortTermAvg": SensorEntityDescription(
34  key="radonShortTermAvg",
35  native_unit_of_measurement="Bq/m³",
36  translation_key="radon",
37  ),
39  key="temp",
40  device_class=SensorDeviceClass.TEMPERATURE,
41  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
42  ),
43  "humidity": SensorEntityDescription(
44  key="humidity",
45  device_class=SensorDeviceClass.HUMIDITY,
46  native_unit_of_measurement=PERCENTAGE,
47  ),
48  "pressure": SensorEntityDescription(
49  key="pressure",
50  device_class=SensorDeviceClass.ATMOSPHERIC_PRESSURE,
51  native_unit_of_measurement=UnitOfPressure.MBAR,
52  ),
53  "battery": SensorEntityDescription(
54  key="battery",
55  device_class=SensorDeviceClass.BATTERY,
56  native_unit_of_measurement=PERCENTAGE,
57  entity_category=EntityCategory.DIAGNOSTIC,
58  ),
60  key="co2",
61  device_class=SensorDeviceClass.CO2,
62  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
63  ),
65  key="voc",
66  device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS,
67  native_unit_of_measurement=CONCENTRATION_PARTS_PER_BILLION,
68  ),
69  "light": SensorEntityDescription(
70  key="light",
71  native_unit_of_measurement=PERCENTAGE,
72  translation_key="light",
73  ),
74  "virusRisk": SensorEntityDescription(
75  key="virusRisk",
76  translation_key="virus_risk",
77  ),
79  key="mold",
80  translation_key="mold",
81  ),
83  key="rssi",
84  native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
85  device_class=SensorDeviceClass.SIGNAL_STRENGTH,
86  entity_registry_enabled_default=False,
87  entity_category=EntityCategory.DIAGNOSTIC,
88  ),
90  key="pm1",
91  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
92  device_class=SensorDeviceClass.PM1,
93  ),
95  key="pm25",
96  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
97  device_class=SensorDeviceClass.PM25,
98  ),
99 }
100 
101 
103  hass: HomeAssistant,
104  entry: AirthingsConfigEntry,
105  async_add_entities: AddEntitiesCallback,
106 ) -> None:
107  """Set up the Airthings sensor."""
108 
109  coordinator = entry.runtime_data
110  entities = [
112  coordinator,
113  airthings_device,
114  SENSORS[sensor_types],
115  )
116  for airthings_device in coordinator.data.values()
117  for sensor_types in airthings_device.sensor_types
118  if sensor_types in SENSORS
119  ]
120  async_add_entities(entities)
121 
122 
124  CoordinatorEntity[AirthingsDataCoordinatorType], SensorEntity
125 ):
126  """Representation of a Airthings Sensor device."""
127 
128  _attr_state_class = SensorStateClass.MEASUREMENT
129  _attr_has_entity_name = True
130 
131  def __init__(
132  self,
133  coordinator: AirthingsDataCoordinatorType,
134  airthings_device: AirthingsDevice,
135  entity_description: SensorEntityDescription,
136  ) -> None:
137  """Initialize the sensor."""
138  super().__init__(coordinator)
139 
140  self.entity_descriptionentity_description = entity_description
141 
142  self._attr_unique_id_attr_unique_id = f"{airthings_device.device_id}_{entity_description.key}"
143  self._id_id = airthings_device.device_id
144  self._attr_device_info_attr_device_info = DeviceInfo(
145  configuration_url=(
146  "https://dashboard.airthings.com/devices/"
147  f"{airthings_device.device_id}"
148  ),
149  identifiers={(DOMAIN, airthings_device.device_id)},
150  name=airthings_device.name,
151  manufacturer="Airthings",
152  model=airthings_device.product_name,
153  )
154 
155  @property
156  def native_value(self) -> StateType:
157  """Return the value reported by the sensor."""
158  return self.coordinator.data[self._id_id].sensors[self.entity_descriptionentity_description.key] # type: ignore[no-any-return]
159 
160  @property
161  def available(self) -> bool:
162  """Check if device and sensor is available in data."""
163  return (
164  super().available
165  and self.entity_descriptionentity_description.key in self.coordinator.data[self._id_id].sensors
166  )
None __init__(self, AirthingsDataCoordinatorType coordinator, AirthingsDevice airthings_device, SensorEntityDescription entity_description)
Definition: sensor.py:136
None async_setup_entry(HomeAssistant hass, AirthingsConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:106