Use EntityDescription - openuv (#55022)

This commit is contained in:
Marc Mueller 2021-08-22 20:30:50 +02:00 committed by GitHub
parent bba6a75934
commit bfb6eaf6f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
"""Support for OpenUV sensors.""" """Support for OpenUV sensors."""
from __future__ import annotations from __future__ import annotations
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TIME_MINUTES, UV_INDEX from homeassistant.const import TIME_MINUTES, UV_INDEX
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -42,42 +42,68 @@ UV_LEVEL_HIGH = "High"
UV_LEVEL_MODERATE = "Moderate" UV_LEVEL_MODERATE = "Moderate"
UV_LEVEL_LOW = "Low" UV_LEVEL_LOW = "Low"
SENSORS = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
TYPE_CURRENT_OZONE_LEVEL: ("Current Ozone Level", "mdi:vector-triangle", "du"), SensorEntityDescription(
TYPE_CURRENT_UV_INDEX: ("Current UV Index", "mdi:weather-sunny", UV_INDEX), key=TYPE_CURRENT_OZONE_LEVEL,
TYPE_CURRENT_UV_LEVEL: ("Current UV Level", "mdi:weather-sunny", None), name="Current Ozone Level",
TYPE_MAX_UV_INDEX: ("Max UV Index", "mdi:weather-sunny", UV_INDEX), icon="mdi:vector-triangle",
TYPE_SAFE_EXPOSURE_TIME_1: ( native_unit_of_measurement="du",
"Skin Type 1 Safe Exposure Time",
"mdi:timer-outline",
TIME_MINUTES,
), ),
TYPE_SAFE_EXPOSURE_TIME_2: ( SensorEntityDescription(
"Skin Type 2 Safe Exposure Time", key=TYPE_CURRENT_UV_INDEX,
"mdi:timer-outline", name="Current UV Index",
TIME_MINUTES, icon="mdi:weather-sunny",
native_unit_of_measurement=UV_INDEX,
), ),
TYPE_SAFE_EXPOSURE_TIME_3: ( SensorEntityDescription(
"Skin Type 3 Safe Exposure Time", key=TYPE_CURRENT_UV_LEVEL,
"mdi:timer-outline", name="Current UV Level",
TIME_MINUTES, icon="mdi:weather-sunny",
native_unit_of_measurement=None,
), ),
TYPE_SAFE_EXPOSURE_TIME_4: ( SensorEntityDescription(
"Skin Type 4 Safe Exposure Time", key=TYPE_MAX_UV_INDEX,
"mdi:timer-outline", name="Max UV Index",
TIME_MINUTES, icon="mdi:weather-sunny",
native_unit_of_measurement=UV_INDEX,
), ),
TYPE_SAFE_EXPOSURE_TIME_5: ( SensorEntityDescription(
"Skin Type 5 Safe Exposure Time", key=TYPE_SAFE_EXPOSURE_TIME_1,
"mdi:timer-outline", name="Skin Type 1 Safe Exposure Time",
TIME_MINUTES, icon="mdi:timer-outline",
native_unit_of_measurement=TIME_MINUTES,
), ),
TYPE_SAFE_EXPOSURE_TIME_6: ( SensorEntityDescription(
"Skin Type 6 Safe Exposure Time", key=TYPE_SAFE_EXPOSURE_TIME_2,
"mdi:timer-outline", name="Skin Type 2 Safe Exposure Time",
TIME_MINUTES, icon="mdi:timer-outline",
native_unit_of_measurement=TIME_MINUTES,
), ),
} SensorEntityDescription(
key=TYPE_SAFE_EXPOSURE_TIME_3,
name="Skin Type 3 Safe Exposure Time",
icon="mdi:timer-outline",
native_unit_of_measurement=TIME_MINUTES,
),
SensorEntityDescription(
key=TYPE_SAFE_EXPOSURE_TIME_4,
name="Skin Type 4 Safe Exposure Time",
icon="mdi:timer-outline",
native_unit_of_measurement=TIME_MINUTES,
),
SensorEntityDescription(
key=TYPE_SAFE_EXPOSURE_TIME_5,
name="Skin Type 5 Safe Exposure Time",
icon="mdi:timer-outline",
native_unit_of_measurement=TIME_MINUTES,
),
SensorEntityDescription(
key=TYPE_SAFE_EXPOSURE_TIME_6,
name="Skin Type 6 Safe Exposure Time",
icon="mdi:timer-outline",
native_unit_of_measurement=TIME_MINUTES,
),
)
async def async_setup_entry( async def async_setup_entry(
@ -86,26 +112,21 @@ async def async_setup_entry(
"""Set up a OpenUV sensor based on a config entry.""" """Set up a OpenUV sensor based on a config entry."""
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
sensors = [] entities = [OpenUvSensor(openuv, description) for description in SENSOR_TYPES]
for kind, attrs in SENSORS.items(): async_add_entities(entities, True)
name, icon, unit = attrs
sensors.append(OpenUvSensor(openuv, kind, name, icon, unit))
async_add_entities(sensors, True)
class OpenUvSensor(OpenUvEntity, SensorEntity): class OpenUvSensor(OpenUvEntity, SensorEntity):
"""Define a binary sensor for OpenUV.""" """Define a binary sensor for OpenUV."""
def __init__( def __init__(
self, openuv: OpenUV, sensor_type: str, name: str, icon: str, unit: str | None self,
openuv: OpenUV,
description: SensorEntityDescription,
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(openuv, sensor_type) super().__init__(openuv, description.key)
self.entity_description = description
self._attr_icon = icon
self._attr_name = name
self._attr_native_unit_of_measurement = unit
@callback @callback
def update_from_latest_data(self) -> None: def update_from_latest_data(self) -> None: