Use EntityDescription - luftdaten (#55676)

* Use EntityDescription - luftdaten

* Fix name attribute

* Remove default values

* Move SensorTypes back to __init__
This commit is contained in:
Marc Mueller 2021-09-03 22:34:01 +02:00 committed by GitHub
parent fbf812a845
commit 3c0a34dd01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 90 deletions

View file

@ -1,7 +1,5 @@
"""Support for Luftdaten sensors."""
import logging
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_LATITUDE,
@ -16,87 +14,54 @@ from . import (
DATA_LUFTDATEN_CLIENT,
DEFAULT_ATTRIBUTION,
DOMAIN,
SENSORS,
SENSOR_TYPES,
TOPIC_UPDATE,
)
from .const import ATTR_SENSOR_ID
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up a Luftdaten sensor based on a config entry."""
luftdaten = hass.data[DOMAIN][DATA_LUFTDATEN_CLIENT][entry.entry_id]
sensors = []
for sensor_type in luftdaten.sensor_conditions:
try:
name, icon, unit, device_class = SENSORS[sensor_type]
except KeyError:
_LOGGER.debug("Unknown sensor value type: %s", sensor_type)
continue
entities = [
LuftdatenSensor(luftdaten, description, entry.data[CONF_SHOW_ON_MAP])
for description in SENSOR_TYPES
if description.key in luftdaten.sensor_conditions
]
sensors.append(
LuftdatenSensor(
luftdaten,
sensor_type,
name,
icon,
unit,
device_class,
entry.data[CONF_SHOW_ON_MAP],
)
)
async_add_entities(sensors, True)
async_add_entities(entities, True)
class LuftdatenSensor(SensorEntity):
"""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."""
self.entity_description = description
self._async_unsub_dispatcher_connect = None
self.luftdaten = luftdaten
self._icon = icon
self._name = name
self._data = None
self.sensor_type = sensor_type
self._unit_of_measurement = unit
self._show_on_map = show
self._attrs = {}
self._attr_device_class = device_class
@property
def icon(self):
"""Return the icon."""
return self._icon
@property
def native_value(self):
"""Return the state of the device."""
if self._data is not None:
try:
return self._data[self.sensor_type]
return self._data[self.entity_description.key]
except KeyError:
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
def unique_id(self) -> str:
"""Return a unique, friendly identifier for this entity."""
if self._data is not None:
try:
return f"{self._data['sensor_id']}_{self.sensor_type}"
return f"{self._data['sensor_id']}_{self.entity_description.key}"
except KeyError:
return None