Use EntityDescription - airnow (#55684)

This commit is contained in:
Marc Mueller 2021-09-03 22:36:24 +02:00 committed by GitHub
parent bbd9c6eb5b
commit f5cd321185
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,14 +1,15 @@
"""Support for the AirNow sensor service.""" """Support for the AirNow sensor service."""
from homeassistant.components.sensor import SensorEntity from __future__ import annotations
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import ( from homeassistant.const import (
ATTR_ATTRIBUTION, ATTR_ATTRIBUTION,
ATTR_DEVICE_CLASS,
ATTR_ICON,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
) )
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import AirNowDataUpdateCoordinator
from .const import ( from .const import (
ATTR_API_AQI, ATTR_API_AQI,
ATTR_API_AQI_DESCRIPTION, ATTR_API_AQI_DESCRIPTION,
@ -22,69 +23,69 @@ from .const import (
ATTRIBUTION = "Data provided by AirNow" ATTRIBUTION = "Data provided by AirNow"
ATTR_LABEL = "label"
ATTR_UNIT = "unit"
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1
SENSOR_TYPES = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
ATTR_API_AQI: { SensorEntityDescription(
ATTR_DEVICE_CLASS: None, key=ATTR_API_AQI,
ATTR_ICON: "mdi:blur", icon="mdi:blur",
ATTR_LABEL: ATTR_API_AQI, name=ATTR_API_AQI,
ATTR_UNIT: "aqi", native_unit_of_measurement="aqi",
}, ),
ATTR_API_PM25: { SensorEntityDescription(
ATTR_DEVICE_CLASS: None, key=ATTR_API_PM25,
ATTR_ICON: "mdi:blur", icon="mdi:blur",
ATTR_LABEL: ATTR_API_PM25, name=ATTR_API_PM25,
ATTR_UNIT: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
}, ),
ATTR_API_O3: { SensorEntityDescription(
ATTR_DEVICE_CLASS: None, key=ATTR_API_O3,
ATTR_ICON: "mdi:blur", icon="mdi:blur",
ATTR_LABEL: ATTR_API_O3, name=ATTR_API_O3,
ATTR_UNIT: CONCENTRATION_PARTS_PER_MILLION, native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
}, ),
} )
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up AirNow sensor entities based on a config entry.""" """Set up AirNow sensor entities based on a config entry."""
coordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator = hass.data[DOMAIN][config_entry.entry_id]
sensors = [] entities = [AirNowSensor(coordinator, description) for description in SENSOR_TYPES]
for sensor in SENSOR_TYPES:
sensors.append(AirNowSensor(coordinator, sensor))
async_add_entities(sensors, False) async_add_entities(entities, False)
class AirNowSensor(CoordinatorEntity, SensorEntity): class AirNowSensor(CoordinatorEntity, SensorEntity):
"""Define an AirNow sensor.""" """Define an AirNow sensor."""
def __init__(self, coordinator, kind): coordinator: AirNowDataUpdateCoordinator
def __init__(
self,
coordinator: AirNowDataUpdateCoordinator,
description: SensorEntityDescription,
) -> None:
"""Initialize.""" """Initialize."""
super().__init__(coordinator) super().__init__(coordinator)
self.kind = kind self.entity_description = description
self._state = None self._state = None
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION} self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
self._attr_name = f"AirNow {SENSOR_TYPES[self.kind][ATTR_LABEL]}" self._attr_name = f"AirNow {description.name}"
self._attr_icon = SENSOR_TYPES[self.kind][ATTR_ICON] self._attr_unique_id = (
self._attr_device_class = SENSOR_TYPES[self.kind][ATTR_DEVICE_CLASS] f"{coordinator.latitude}-{coordinator.longitude}-{description.key.lower()}"
self._attr_native_unit_of_measurement = SENSOR_TYPES[self.kind][ATTR_UNIT] )
self._attr_unique_id = f"{self.coordinator.latitude}-{self.coordinator.longitude}-{self.kind.lower()}"
@property @property
def native_value(self): def native_value(self):
"""Return the state.""" """Return the state."""
self._state = self.coordinator.data[self.kind] self._state = self.coordinator.data[self.entity_description.key]
return self._state return self._state
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
if self.kind == ATTR_API_AQI: if self.entity_description.key == ATTR_API_AQI:
self._attrs[SENSOR_AQI_ATTR_DESCR] = self.coordinator.data[ self._attrs[SENSOR_AQI_ATTR_DESCR] = self.coordinator.data[
ATTR_API_AQI_DESCRIPTION ATTR_API_AQI_DESCRIPTION
] ]