2021-09-23 20:20:30 +00:00
|
|
|
"""Support for Airthings sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from airthings import AirthingsDevice
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
2021-12-01 09:59:24 +00:00
|
|
|
SensorDeviceClass,
|
2021-09-23 20:20:30 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-01 12:57:54 +00:00
|
|
|
SensorStateClass,
|
2021-09-23 20:20:30 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
2021-09-27 11:27:02 +00:00
|
|
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
2021-09-23 20:20:30 +00:00
|
|
|
CONCENTRATION_PARTS_PER_BILLION,
|
|
|
|
CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
PERCENTAGE,
|
2021-09-27 11:27:02 +00:00
|
|
|
SIGNAL_STRENGTH_DECIBELS,
|
2022-12-19 20:41:46 +00:00
|
|
|
UnitOfPressure,
|
2022-12-20 16:59:20 +00:00
|
|
|
UnitOfTemperature,
|
2021-09-23 20:20:30 +00:00
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-08 20:17:03 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
2021-09-23 20:20:30 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-01-05 13:56:34 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-09-23 20:20:30 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
SENSORS: dict[str, SensorEntityDescription] = {
|
|
|
|
"radonShortTermAvg": SensorEntityDescription(
|
|
|
|
key="radonShortTermAvg",
|
|
|
|
native_unit_of_measurement="Bq/m³",
|
|
|
|
name="Radon",
|
|
|
|
),
|
|
|
|
"temp": SensorEntityDescription(
|
|
|
|
key="temp",
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2022-12-20 16:59:20 +00:00
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
2021-09-23 20:20:30 +00:00
|
|
|
name="Temperature",
|
|
|
|
),
|
|
|
|
"humidity": SensorEntityDescription(
|
|
|
|
key="humidity",
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
2021-09-23 20:20:30 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
name="Humidity",
|
|
|
|
),
|
|
|
|
"pressure": SensorEntityDescription(
|
|
|
|
key="pressure",
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.PRESSURE,
|
2022-12-19 20:41:46 +00:00
|
|
|
native_unit_of_measurement=UnitOfPressure.MBAR,
|
2021-09-23 20:20:30 +00:00
|
|
|
name="Pressure",
|
|
|
|
),
|
|
|
|
"battery": SensorEntityDescription(
|
|
|
|
key="battery",
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
2021-09-23 20:20:30 +00:00
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
2021-12-08 20:17:03 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-23 20:20:30 +00:00
|
|
|
name="Battery",
|
|
|
|
),
|
|
|
|
"co2": SensorEntityDescription(
|
|
|
|
key="co2",
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.CO2,
|
2021-09-23 20:20:30 +00:00
|
|
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
|
|
|
name="CO2",
|
|
|
|
),
|
|
|
|
"voc": SensorEntityDescription(
|
|
|
|
key="voc",
|
|
|
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_BILLION,
|
|
|
|
name="VOC",
|
|
|
|
),
|
2021-09-27 11:27:02 +00:00
|
|
|
"light": SensorEntityDescription(
|
|
|
|
key="light",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
name="Light",
|
|
|
|
),
|
|
|
|
"virusRisk": SensorEntityDescription(
|
|
|
|
key="virusRisk",
|
|
|
|
name="Virus Risk",
|
|
|
|
),
|
|
|
|
"mold": SensorEntityDescription(
|
|
|
|
key="mold",
|
|
|
|
name="Mold",
|
|
|
|
),
|
|
|
|
"rssi": SensorEntityDescription(
|
|
|
|
key="rssi",
|
|
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
2021-09-27 11:27:02 +00:00
|
|
|
name="RSSI",
|
|
|
|
entity_registry_enabled_default=False,
|
2021-12-08 20:17:03 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-27 11:27:02 +00:00
|
|
|
),
|
|
|
|
"pm1": SensorEntityDescription(
|
|
|
|
key="pm1",
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.PM1,
|
2021-09-27 11:27:02 +00:00
|
|
|
name="PM1",
|
|
|
|
),
|
|
|
|
"pm25": SensorEntityDescription(
|
|
|
|
key="pm25",
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
2021-12-01 09:59:24 +00:00
|
|
|
device_class=SensorDeviceClass.PM25,
|
2021-09-27 11:27:02 +00:00
|
|
|
name="PM25",
|
|
|
|
),
|
2021-09-23 20:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Airthings sensor."""
|
|
|
|
|
|
|
|
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
entities = [
|
|
|
|
AirthingsHeaterEnergySensor(
|
|
|
|
coordinator,
|
|
|
|
airthings_device,
|
|
|
|
SENSORS[sensor_types],
|
|
|
|
)
|
|
|
|
for airthings_device in coordinator.data.values()
|
|
|
|
for sensor_types in airthings_device.sensor_types
|
|
|
|
if sensor_types in SENSORS
|
|
|
|
]
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class AirthingsHeaterEnergySensor(CoordinatorEntity, SensorEntity):
|
|
|
|
"""Representation of a Airthings Sensor device."""
|
|
|
|
|
2021-12-01 12:57:54 +00:00
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2021-09-23 20:20:30 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
airthings_device: AirthingsDevice,
|
|
|
|
entity_description: SensorEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
self.entity_description = entity_description
|
|
|
|
|
|
|
|
self._attr_name = f"{airthings_device.name} {entity_description.name}"
|
|
|
|
self._attr_unique_id = f"{airthings_device.device_id}_{entity_description.key}"
|
|
|
|
self._id = airthings_device.device_id
|
2021-10-19 16:47:14 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
configuration_url="https://dashboard.airthings.com/",
|
|
|
|
identifiers={(DOMAIN, airthings_device.device_id)},
|
|
|
|
name=airthings_device.name,
|
|
|
|
manufacturer="Airthings",
|
|
|
|
)
|
2021-09-23 20:20:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def native_value(self) -> StateType:
|
|
|
|
"""Return the value reported by the sensor."""
|
|
|
|
return self.coordinator.data[self._id].sensors[self.entity_description.key]
|