2020-05-12 14:10:03 +00:00
|
|
|
"""BleBox sensor entities."""
|
2022-09-30 09:37:47 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
from blebox_uniapi.box import Box
|
|
|
|
import blebox_uniapi.sensor
|
|
|
|
|
2022-09-30 09:37:47 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-09-30 09:37:47 +00:00
|
|
|
from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, TEMP_CELSIUS
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-12 14:10:03 +00:00
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
from . import BleBoxEntity
|
|
|
|
from .const import DOMAIN, PRODUCT
|
2022-07-11 10:24:37 +00:00
|
|
|
|
|
|
|
|
2022-09-30 09:37:47 +00:00
|
|
|
@dataclass
|
|
|
|
class BleboxSensorEntityDescription(SensorEntityDescription):
|
|
|
|
"""Class describing Blebox sensor entities."""
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES = (
|
|
|
|
BleboxSensorEntityDescription(
|
|
|
|
key="pm1",
|
|
|
|
device_class=SensorDeviceClass.PM1,
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
),
|
|
|
|
BleboxSensorEntityDescription(
|
|
|
|
key="pm2_5",
|
|
|
|
device_class=SensorDeviceClass.PM25,
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
),
|
|
|
|
BleboxSensorEntityDescription(
|
|
|
|
key="pm10",
|
|
|
|
device_class=SensorDeviceClass.PM10,
|
|
|
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
|
|
|
),
|
|
|
|
BleboxSensorEntityDescription(
|
|
|
|
key="temperature",
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
),
|
|
|
|
)
|
2020-05-12 14:10:03 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:18:00 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-05-12 14:10:03 +00:00
|
|
|
"""Set up a BleBox entry."""
|
2022-10-19 15:49:40 +00:00
|
|
|
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
|
|
entities = [
|
|
|
|
BleBoxSensorEntity(feature) for feature in product.features.get("sensors", [])
|
|
|
|
]
|
|
|
|
async_add_entities(entities, True)
|
2020-05-12 14:10:03 +00:00
|
|
|
|
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
class BleBoxSensorEntity(BleBoxEntity[blebox_uniapi.sensor.BaseSensor], SensorEntity):
|
2020-05-12 14:10:03 +00:00
|
|
|
"""Representation of a BleBox sensor feature."""
|
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
def __init__(self, feature: blebox_uniapi.sensor.BaseSensor) -> None:
|
2021-07-12 20:52:38 +00:00
|
|
|
"""Initialize a BleBox sensor feature."""
|
|
|
|
super().__init__(feature)
|
2022-09-30 09:37:47 +00:00
|
|
|
|
|
|
|
for description in SENSOR_TYPES:
|
|
|
|
if description.key == feature.device_class:
|
|
|
|
self.entity_description = description
|
|
|
|
break
|
2021-07-12 20:52:38 +00:00
|
|
|
|
2020-05-12 14:10:03 +00:00
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self):
|
2020-05-12 14:10:03 +00:00
|
|
|
"""Return the state."""
|
2022-09-30 09:37:47 +00:00
|
|
|
return self._feature.native_value
|