-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathsensor.py
201 lines (182 loc) · 7.46 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Support for PurpleAir sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from aiopurpleair.models.sensors import SensorModel
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
EntityCategory,
UnitOfPressure,
UnitOfTemperature,
UnitOfTime,
UnitOfVolume,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_SENSOR_INDICES, DOMAIN
from .coordinator import PurpleAirDataUpdateCoordinator
from .entity import PurpleAirEntity
CONCENTRATION_PARTICLES_PER_100_MILLILITERS = f"particles/100{UnitOfVolume.MILLILITERS}"
@dataclass(frozen=True, kw_only=True)
class PurpleAirSensorEntityDescription(SensorEntityDescription):
"""Define an object to describe PurpleAir sensor entities."""
value_fn: Callable[[SensorModel], float | str | None]
SENSOR_DESCRIPTIONS = [
PurpleAirSensorEntityDescription(
key="humidity",
device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.humidity,
),
PurpleAirSensorEntityDescription(
key="pm0.3_count_concentration",
translation_key="pm0_3_count_concentration",
entity_registry_enabled_default=False,
native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm0_3_um_count,
),
PurpleAirSensorEntityDescription(
key="pm0.5_count_concentration",
translation_key="pm0_5_count_concentration",
entity_registry_enabled_default=False,
native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm0_5_um_count,
),
PurpleAirSensorEntityDescription(
key="pm1.0_count_concentration",
translation_key="pm1_0_count_concentration",
entity_registry_enabled_default=False,
native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm1_0_um_count,
),
PurpleAirSensorEntityDescription(
key="pm1.0_mass_concentration",
device_class=SensorDeviceClass.PM1,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm1_0,
),
PurpleAirSensorEntityDescription(
key="pm10.0_count_concentration",
translation_key="pm10_0_count_concentration",
entity_registry_enabled_default=False,
native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm10_0_um_count,
),
PurpleAirSensorEntityDescription(
key="pm10.0_mass_concentration",
device_class=SensorDeviceClass.PM10,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm10_0,
),
PurpleAirSensorEntityDescription(
key="pm2.5_count_concentration",
translation_key="pm2_5_count_concentration",
entity_registry_enabled_default=False,
native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm2_5_um_count,
),
PurpleAirSensorEntityDescription(
key="pm2.5_mass_concentration",
device_class=SensorDeviceClass.PM25,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm2_5,
),
PurpleAirSensorEntityDescription(
key="pm5.0_count_concentration",
translation_key="pm5_0_count_concentration",
entity_registry_enabled_default=False,
native_unit_of_measurement=CONCENTRATION_PARTICLES_PER_100_MILLILITERS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pm5_0_um_count,
),
PurpleAirSensorEntityDescription(
key="pressure",
device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=UnitOfPressure.MBAR,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pressure,
),
PurpleAirSensorEntityDescription(
key="rssi",
translation_key="rssi",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.pressure,
),
PurpleAirSensorEntityDescription(
key="temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.temperature,
),
PurpleAirSensorEntityDescription(
key="uptime",
translation_key="uptime",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.MINUTES,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda sensor: sensor.uptime,
),
PurpleAirSensorEntityDescription(
# This sensor is an air quality index for VOCs. More info at https://github.com/home-assistant/core/pull/84896
key="voc",
translation_key="voc_aqi",
device_class=SensorDeviceClass.AQI,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda sensor: sensor.voc,
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up PurpleAir sensors based on a config entry."""
coordinator: PurpleAirDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
PurpleAirSensorEntity(coordinator, entry, sensor_index, description)
for sensor_index in entry.options[CONF_SENSOR_INDICES]
for description in SENSOR_DESCRIPTIONS
)
class PurpleAirSensorEntity(PurpleAirEntity, SensorEntity):
"""Define a representation of a PurpleAir sensor."""
entity_description: PurpleAirSensorEntityDescription
def __init__(
self,
coordinator: PurpleAirDataUpdateCoordinator,
entry: ConfigEntry,
sensor_index: int,
description: PurpleAirSensorEntityDescription,
) -> None:
"""Initialize."""
super().__init__(coordinator, entry, sensor_index)
self._attr_unique_id = f"{self._sensor_index}-{description.key}"
self.entity_description = description
@property
def native_value(self) -> float | str | None:
"""Return the sensor value."""
return self.entity_description.value_fn(self.sensor_data)