Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for Arve devices."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from asyncarve import ArveSensProData
7 
9  SensorDeviceClass,
10  SensorEntity,
11  SensorEntityDescription,
12  SensorStateClass,
13 )
14 from homeassistant.const import (
15  CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
16  CONCENTRATION_PARTS_PER_MILLION,
17  PERCENTAGE,
18  UnitOfTemperature,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 
23 from .coordinator import ArveConfigEntry
24 from .entity import ArveDeviceEntity
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Describes Arve device entity."""
30 
31  value_fn: Callable[[ArveSensProData], float | int]
32 
33 
34 SENSORS: tuple[ArveDeviceEntityDescription, ...] = (
36  key="CO2",
37  native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
38  device_class=SensorDeviceClass.CO2,
39  value_fn=lambda arve_data: arve_data.co2,
40  state_class=SensorStateClass.MEASUREMENT,
41  ),
43  key="AQI",
44  device_class=SensorDeviceClass.AQI,
45  value_fn=lambda arve_data: arve_data.aqi,
46  state_class=SensorStateClass.MEASUREMENT,
47  ),
49  key="Humidity",
50  native_unit_of_measurement=PERCENTAGE,
51  device_class=SensorDeviceClass.HUMIDITY,
52  value_fn=lambda arve_data: arve_data.humidity,
53  state_class=SensorStateClass.MEASUREMENT,
54  ),
56  key="PM10",
57  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
58  device_class=SensorDeviceClass.PM10,
59  value_fn=lambda arve_data: arve_data.pm10,
60  state_class=SensorStateClass.MEASUREMENT,
61  ),
63  key="PM25",
64  native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
65  device_class=SensorDeviceClass.PM25,
66  value_fn=lambda arve_data: arve_data.pm25,
67  state_class=SensorStateClass.MEASUREMENT,
68  ),
70  key="Temperature",
71  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
72  device_class=SensorDeviceClass.TEMPERATURE,
73  value_fn=lambda arve_data: arve_data.temperature,
74  state_class=SensorStateClass.MEASUREMENT,
75  ),
77  key="TVOC",
78  translation_key="tvoc",
79  value_fn=lambda arve_data: arve_data.tvoc,
80  state_class=SensorStateClass.MEASUREMENT,
81  ),
82 )
83 
84 
86  hass: HomeAssistant, entry: ArveConfigEntry, async_add_entities: AddEntitiesCallback
87 ) -> None:
88  """Set up Arve device based on a config entry."""
89  coordinator = entry.runtime_data
90 
92  ArveDevice(coordinator, description, sn)
93  for description in SENSORS
94  for sn in coordinator.devices.sn
95  )
96 
97 
99  """Define an Arve device."""
100 
101  entity_description: ArveDeviceEntityDescription
102 
103  @property
104  def native_value(self) -> int | float:
105  """State of the sensor."""
106  return self.entity_descriptionentity_description.value_fn(self.devicedevice.sensors)
None async_setup_entry(HomeAssistant hass, ArveConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:87