mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use EntityDescription - luftdaten (#55676)
* Use EntityDescription - luftdaten * Fix name attribute * Remove default values * Move SensorTypes back to __init__
This commit is contained in:
parent
fbf812a845
commit
3c0a34dd01
@ -1,10 +1,13 @@
|
|||||||
"""Support for Luftdaten stations."""
|
"""Support for Luftdaten stations."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from luftdaten import Luftdaten
|
from luftdaten import Luftdaten
|
||||||
from luftdaten.exceptions import LuftdatenError
|
from luftdaten.exceptions import LuftdatenError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntityDescription
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT
|
from homeassistant.config_entries import SOURCE_IMPORT
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||||
@ -47,49 +50,53 @@ SENSOR_TEMPERATURE = "temperature"
|
|||||||
|
|
||||||
TOPIC_UPDATE = f"{DOMAIN}_data_update"
|
TOPIC_UPDATE = f"{DOMAIN}_data_update"
|
||||||
|
|
||||||
SENSORS = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SENSOR_TEMPERATURE: [
|
SensorEntityDescription(
|
||||||
"Temperature",
|
key=SENSOR_TEMPERATURE,
|
||||||
"mdi:thermometer",
|
name="Temperature",
|
||||||
TEMP_CELSIUS,
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
],
|
),
|
||||||
SENSOR_HUMIDITY: [
|
SensorEntityDescription(
|
||||||
"Humidity",
|
key=SENSOR_HUMIDITY,
|
||||||
"mdi:water-percent",
|
name="Humidity",
|
||||||
PERCENTAGE,
|
icon="mdi:water-percent",
|
||||||
DEVICE_CLASS_HUMIDITY,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
],
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
SENSOR_PRESSURE: [
|
),
|
||||||
"Pressure",
|
SensorEntityDescription(
|
||||||
"mdi:arrow-down-bold",
|
key=SENSOR_PRESSURE,
|
||||||
PRESSURE_HPA,
|
name="Pressure",
|
||||||
DEVICE_CLASS_PRESSURE,
|
icon="mdi:arrow-down-bold",
|
||||||
],
|
native_unit_of_measurement=PRESSURE_HPA,
|
||||||
SENSOR_PRESSURE_AT_SEALEVEL: [
|
device_class=DEVICE_CLASS_PRESSURE,
|
||||||
"Pressure at sealevel",
|
),
|
||||||
"mdi:download",
|
SensorEntityDescription(
|
||||||
PRESSURE_HPA,
|
key=SENSOR_PRESSURE_AT_SEALEVEL,
|
||||||
DEVICE_CLASS_PRESSURE,
|
name="Pressure at sealevel",
|
||||||
],
|
icon="mdi:download",
|
||||||
SENSOR_PM10: [
|
native_unit_of_measurement=PRESSURE_HPA,
|
||||||
"PM10",
|
device_class=DEVICE_CLASS_PRESSURE,
|
||||||
"mdi:thought-bubble",
|
),
|
||||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
SensorEntityDescription(
|
||||||
None,
|
key=SENSOR_PM10,
|
||||||
],
|
name="PM10",
|
||||||
SENSOR_PM2_5: [
|
icon="mdi:thought-bubble",
|
||||||
"PM2.5",
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||||
"mdi:thought-bubble-outline",
|
),
|
||||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
SensorEntityDescription(
|
||||||
None,
|
key=SENSOR_PM2_5,
|
||||||
],
|
name="PM2.5",
|
||||||
}
|
icon="mdi:thought-bubble-outline",
|
||||||
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
SENSOR_SCHEMA = vol.Schema(
|
SENSOR_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)): vol.All(
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
|
||||||
cv.ensure_list, [vol.In(SENSORS)]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -174,7 +181,7 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
luftdaten = LuftDatenData(
|
luftdaten = LuftDatenData(
|
||||||
Luftdaten(config_entry.data[CONF_SENSOR_ID], hass.loop, session),
|
Luftdaten(config_entry.data[CONF_SENSOR_ID], hass.loop, session),
|
||||||
config_entry.data.get(CONF_SENSORS, {}).get(
|
config_entry.data.get(CONF_SENSORS, {}).get(
|
||||||
CONF_MONITORED_CONDITIONS, list(SENSORS)
|
CONF_MONITORED_CONDITIONS, SENSOR_KEYS
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
await luftdaten.async_update()
|
await luftdaten.async_update()
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
"""Support for Luftdaten sensors."""
|
"""Support for Luftdaten sensors."""
|
||||||
import logging
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
ATTR_LATITUDE,
|
ATTR_LATITUDE,
|
||||||
@ -16,87 +14,54 @@ from . import (
|
|||||||
DATA_LUFTDATEN_CLIENT,
|
DATA_LUFTDATEN_CLIENT,
|
||||||
DEFAULT_ATTRIBUTION,
|
DEFAULT_ATTRIBUTION,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SENSORS,
|
SENSOR_TYPES,
|
||||||
TOPIC_UPDATE,
|
TOPIC_UPDATE,
|
||||||
)
|
)
|
||||||
from .const import ATTR_SENSOR_ID
|
from .const import ATTR_SENSOR_ID
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
"""Set up a Luftdaten sensor based on a config entry."""
|
"""Set up a Luftdaten sensor based on a config entry."""
|
||||||
luftdaten = hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT][entry.entry_id]
|
luftdaten = hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT][entry.entry_id]
|
||||||
|
|
||||||
sensors = []
|
entities = [
|
||||||
for sensor_type in luftdaten.sensor_conditions:
|
LuftdatenSensor(luftdaten, description, entry.data[CONF_SHOW_ON_MAP])
|
||||||
try:
|
for description in SENSOR_TYPES
|
||||||
name, icon, unit, device_class = SENSORS[sensor_type]
|
if description.key in luftdaten.sensor_conditions
|
||||||
except KeyError:
|
]
|
||||||
_LOGGER.debug("Unknown sensor value type: %s", sensor_type)
|
|
||||||
continue
|
|
||||||
|
|
||||||
sensors.append(
|
async_add_entities(entities, True)
|
||||||
LuftdatenSensor(
|
|
||||||
luftdaten,
|
|
||||||
sensor_type,
|
|
||||||
name,
|
|
||||||
icon,
|
|
||||||
unit,
|
|
||||||
device_class,
|
|
||||||
entry.data[CONF_SHOW_ON_MAP],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
|
||||||
|
|
||||||
|
|
||||||
class LuftdatenSensor(SensorEntity):
|
class LuftdatenSensor(SensorEntity):
|
||||||
"""Implementation of a Luftdaten sensor."""
|
"""Implementation of a Luftdaten sensor."""
|
||||||
|
|
||||||
def __init__(self, luftdaten, sensor_type, name, icon, unit, device_class, show):
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(self, luftdaten, description: SensorEntityDescription, show):
|
||||||
"""Initialize the Luftdaten sensor."""
|
"""Initialize the Luftdaten sensor."""
|
||||||
|
self.entity_description = description
|
||||||
self._async_unsub_dispatcher_connect = None
|
self._async_unsub_dispatcher_connect = None
|
||||||
self.luftdaten = luftdaten
|
self.luftdaten = luftdaten
|
||||||
self._icon = icon
|
|
||||||
self._name = name
|
|
||||||
self._data = None
|
self._data = None
|
||||||
self.sensor_type = sensor_type
|
|
||||||
self._unit_of_measurement = unit
|
|
||||||
self._show_on_map = show
|
self._show_on_map = show
|
||||||
self._attrs = {}
|
self._attrs = {}
|
||||||
self._attr_device_class = device_class
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon."""
|
|
||||||
return self._icon
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
if self._data is not None:
|
if self._data is not None:
|
||||||
try:
|
try:
|
||||||
return self._data[self.sensor_type]
|
return self._data[self.entity_description.key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this entity, if any."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Disable polling."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return a unique, friendly identifier for this entity."""
|
"""Return a unique, friendly identifier for this entity."""
|
||||||
if self._data is not None:
|
if self._data is not None:
|
||||||
try:
|
try:
|
||||||
return f"{self._data['sensor_id']}_{self.sensor_type}"
|
return f"{self._data['sensor_id']}_{self.entity_description.key}"
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user