Refactor blebox sensors (#80671)

refactor: sensors, entity description pick moved to async_setup_entry, removed redundant dataclass
This commit is contained in:
Michał Huryn 2022-10-20 15:51:29 +02:00 committed by GitHub
parent e84e5f134e
commit 3f8362fe1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,4 @@
"""BleBox sensor entities.""" """BleBox sensor entities."""
from dataclasses import dataclass
from blebox_uniapi.box import Box from blebox_uniapi.box import Box
import blebox_uniapi.sensor import blebox_uniapi.sensor
@ -17,29 +16,23 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import BleBoxEntity from . import BleBoxEntity
from .const import DOMAIN, PRODUCT from .const import DOMAIN, PRODUCT
@dataclass
class BleboxSensorEntityDescription(SensorEntityDescription):
"""Class describing Blebox sensor entities."""
SENSOR_TYPES = ( SENSOR_TYPES = (
BleboxSensorEntityDescription( SensorEntityDescription(
key="pm1", key="pm1",
device_class=SensorDeviceClass.PM1, device_class=SensorDeviceClass.PM1,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
), ),
BleboxSensorEntityDescription( SensorEntityDescription(
key="pm2_5", key="pm2_5",
device_class=SensorDeviceClass.PM25, device_class=SensorDeviceClass.PM25,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
), ),
BleboxSensorEntityDescription( SensorEntityDescription(
key="pm10", key="pm10",
device_class=SensorDeviceClass.PM10, device_class=SensorDeviceClass.PM10,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
), ),
BleboxSensorEntityDescription( SensorEntityDescription(
key="temperature", key="temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
@ -55,7 +48,10 @@ async def async_setup_entry(
"""Set up a BleBox entry.""" """Set up a BleBox entry."""
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT] product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
entities = [ entities = [
BleBoxSensorEntity(feature) for feature in product.features.get("sensors", []) BleBoxSensorEntity(feature, description)
for feature in product.features.get("sensors", [])
for description in SENSOR_TYPES
if description.key == feature.device_class
] ]
async_add_entities(entities, True) async_add_entities(entities, True)
@ -63,14 +59,14 @@ async def async_setup_entry(
class BleBoxSensorEntity(BleBoxEntity[blebox_uniapi.sensor.BaseSensor], SensorEntity): class BleBoxSensorEntity(BleBoxEntity[blebox_uniapi.sensor.BaseSensor], SensorEntity):
"""Representation of a BleBox sensor feature.""" """Representation of a BleBox sensor feature."""
def __init__(self, feature: blebox_uniapi.sensor.BaseSensor) -> None: def __init__(
self,
feature: blebox_uniapi.sensor.BaseSensor,
description: SensorEntityDescription,
) -> None:
"""Initialize a BleBox sensor feature.""" """Initialize a BleBox sensor feature."""
super().__init__(feature) super().__init__(feature)
self.entity_description = description
for description in SENSOR_TYPES:
if description.key == feature.device_class:
self.entity_description = description
break
@property @property
def native_value(self): def native_value(self):