From 6a81821399d011d24ee6aeb6c682a98743db8e1e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 20 Dec 2021 19:20:40 +0100 Subject: [PATCH] Refactor kaiterra to use SensorEntityDescription (#61865) Co-authored-by: epenet --- homeassistant/components/kaiterra/sensor.py | 62 +++++++++------------ 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/kaiterra/sensor.py b/homeassistant/components/kaiterra/sensor.py index fbaa730ab9f..096ca414d50 100644 --- a/homeassistant/components/kaiterra/sensor.py +++ b/homeassistant/components/kaiterra/sensor.py @@ -1,20 +1,25 @@ """Support for Kaiterra Temperature ahn Humidity Sensors.""" -from homeassistant.components.sensor import SensorEntity -from homeassistant.const import ( - CONF_DEVICE_ID, - CONF_NAME, - DEVICE_CLASS_HUMIDITY, - DEVICE_CLASS_TEMPERATURE, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, ) +from homeassistant.const import CONF_DEVICE_ID, CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.helpers.dispatcher import async_dispatcher_connect from .const import DISPATCHER_KAITERRA, DOMAIN SENSORS = [ - {"name": "Temperature", "prop": "rtemp", "device_class": DEVICE_CLASS_TEMPERATURE}, - {"name": "Humidity", "prop": "rhumid", "device_class": DEVICE_CLASS_HUMIDITY}, + SensorEntityDescription( + 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] 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): """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.""" self._api = api - self._name = f'{name} {sensor["name"]}' self._device_id = device_id - self._kind = sensor["name"].lower() - self._property = sensor["prop"] - self._device_class = sensor["device_class"] + self.entity_description = description + self._attr_name = f"{name} {description.name}" + self._attr_unique_id = f"{device_id}_{description.name.lower()}" @property def _sensor(self): """Return the sensor data.""" - return self._api.data.get(self._device_id, {}).get(self._property, {}) - - @property - def should_poll(self): - """Return that the sensor should not be polled.""" - return False + return self._api.data.get(self._device_id, {}).get( + self.entity_description.key, {} + ) @property def available(self): """Return the availability of the sensor.""" 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 def native_value(self): """Return the state.""" return self._sensor.get("value") - @property - def unique_id(self): - """Return the sensor's unique id.""" - return f"{self._device_id}_{self._kind}" - @property def native_unit_of_measurement(self): """Return the unit the value is expressed in."""