Refactor kaiterra to use SensorEntityDescription (#61865)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-20 19:20:40 +01:00 committed by GitHub
parent 7bee0e6423
commit 6a81821399
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,20 +1,25 @@
"""Support for Kaiterra Temperature ahn Humidity Sensors.""" """Support for Kaiterra Temperature ahn Humidity Sensors."""
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import (
from homeassistant.const import ( SensorDeviceClass,
CONF_DEVICE_ID, SensorEntity,
CONF_NAME, SensorEntityDescription,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
) )
from homeassistant.const import CONF_DEVICE_ID, CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DISPATCHER_KAITERRA, DOMAIN from .const import DISPATCHER_KAITERRA, DOMAIN
SENSORS = [ SENSORS = [
{"name": "Temperature", "prop": "rtemp", "device_class": DEVICE_CLASS_TEMPERATURE}, SensorEntityDescription(
{"name": "Humidity", "prop": "rhumid", "device_class": DEVICE_CLASS_HUMIDITY}, name="Temperature",
key="rtemp",
device_class=SensorDeviceClass.TEMPERATURE,
),
SensorEntityDescription(
name="Humidity",
key="rhumid",
device_class=SensorDeviceClass.HUMIDITY,
),
] ]
@ -28,57 +33,40 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
device_id = discovery_info[CONF_DEVICE_ID] device_id = discovery_info[CONF_DEVICE_ID]
async_add_entities( async_add_entities(
[KaiterraSensor(api, name, device_id, sensor) for sensor in SENSORS] [KaiterraSensor(api, name, device_id, description) for description in SENSORS]
) )
class KaiterraSensor(SensorEntity): class KaiterraSensor(SensorEntity):
"""Implementation of a Kaittera sensor.""" """Implementation of a Kaittera sensor."""
def __init__(self, api, name, device_id, sensor): _attr_should_poll = False
def __init__(self, api, name, device_id, description: SensorEntityDescription):
"""Initialize the sensor.""" """Initialize the sensor."""
self._api = api self._api = api
self._name = f'{name} {sensor["name"]}'
self._device_id = device_id self._device_id = device_id
self._kind = sensor["name"].lower() self.entity_description = description
self._property = sensor["prop"] self._attr_name = f"{name} {description.name}"
self._device_class = sensor["device_class"] self._attr_unique_id = f"{device_id}_{description.name.lower()}"
@property @property
def _sensor(self): def _sensor(self):
"""Return the sensor data.""" """Return the sensor data."""
return self._api.data.get(self._device_id, {}).get(self._property, {}) return self._api.data.get(self._device_id, {}).get(
self.entity_description.key, {}
@property )
def should_poll(self):
"""Return that the sensor should not be polled."""
return False
@property @property
def available(self): def available(self):
"""Return the availability of the sensor.""" """Return the availability of the sensor."""
return self._api.data.get(self._device_id) is not None return self._api.data.get(self._device_id) is not None
@property
def device_class(self):
"""Return the device class."""
return self._device_class
@property
def name(self):
"""Return the name."""
return self._name
@property @property
def native_value(self): def native_value(self):
"""Return the state.""" """Return the state."""
return self._sensor.get("value") return self._sensor.get("value")
@property
def unique_id(self):
"""Return the sensor's unique id."""
return f"{self._device_id}_{self._kind}"
@property @property
def native_unit_of_measurement(self): def native_unit_of_measurement(self):
"""Return the unit the value is expressed in.""" """Return the unit the value is expressed in."""