Use EntityDescription - ecobee (#55088)

This commit is contained in:
Marc Mueller 2021-08-23 20:19:19 +02:00 committed by GitHub
parent 2cdaf632a4
commit ce5c76869d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,9 @@
"""Support for Ecobee sensors.""" """Support for Ecobee sensors."""
from __future__ import annotations
from pyecobee.const import ECOBEE_STATE_CALIBRATING, ECOBEE_STATE_UNKNOWN from pyecobee.const import ECOBEE_STATE_CALIBRATING, ECOBEE_STATE_UNKNOWN
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
@ -11,45 +13,51 @@ from homeassistant.const import (
from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER
SENSOR_TYPES = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
"temperature": ["Temperature", TEMP_FAHRENHEIT, DEVICE_CLASS_TEMPERATURE], SensorEntityDescription(
"humidity": ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY], key="temperature",
} name="Temperature",
native_unit_of_measurement=TEMP_FAHRENHEIT,
device_class=DEVICE_CLASS_TEMPERATURE,
),
SensorEntityDescription(
key="humidity",
name="Humidity",
native_unit_of_measurement=PERCENTAGE,
device_class=DEVICE_CLASS_HUMIDITY,
),
)
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up ecobee (temperature and humidity) sensors.""" """Set up ecobee (temperature and humidity) sensors."""
data = hass.data[DOMAIN] data = hass.data[DOMAIN]
dev = [] entities = [
for index in range(len(data.ecobee.thermostats)): EcobeeSensor(data, sensor["name"], index, description)
for sensor in data.ecobee.get_remote_sensors(index): for index in range(len(data.ecobee.thermostats))
for item in sensor["capability"]: for sensor in data.ecobee.get_remote_sensors(index)
if item["type"] not in ("temperature", "humidity"): for item in sensor["capability"]
continue for description in SENSOR_TYPES
if description.key == item["type"]
]
dev.append(EcobeeSensor(data, sensor["name"], item["type"], index)) async_add_entities(entities, True)
async_add_entities(dev, True)
class EcobeeSensor(SensorEntity): class EcobeeSensor(SensorEntity):
"""Representation of an Ecobee sensor.""" """Representation of an Ecobee sensor."""
def __init__(self, data, sensor_name, sensor_type, sensor_index): def __init__(
self, data, sensor_name, sensor_index, description: SensorEntityDescription
):
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description
self.data = data self.data = data
self._name = f"{sensor_name} {SENSOR_TYPES[sensor_type][0]}"
self.sensor_name = sensor_name self.sensor_name = sensor_name
self.type = sensor_type
self.index = sensor_index self.index = sensor_index
self._state = None self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
@property self._attr_name = f"{sensor_name} {description.name}"
def name(self):
"""Return the name of the Ecobee sensor."""
return self._name
@property @property
def unique_id(self): def unique_id(self):
@ -99,13 +107,6 @@ class EcobeeSensor(SensorEntity):
thermostat = self.data.ecobee.get_thermostat(self.index) thermostat = self.data.ecobee.get_thermostat(self.index)
return thermostat["runtime"]["connected"] return thermostat["runtime"]["connected"]
@property
def device_class(self):
"""Return the device class of the sensor."""
if self.type in (DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE):
return self.type
return None
@property @property
def native_value(self): def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
@ -116,16 +117,11 @@ class EcobeeSensor(SensorEntity):
): ):
return None return None
if self.type == "temperature": if self.entity_description.key == "temperature":
return float(self._state) / 10 return float(self._state) / 10
return self._state return self._state
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement this sensor expresses itself in."""
return self._unit_of_measurement
async def async_update(self): async def async_update(self):
"""Get the latest state of the sensor.""" """Get the latest state of the sensor."""
await self.data.update() await self.data.update()
@ -133,7 +129,7 @@ class EcobeeSensor(SensorEntity):
if sensor["name"] != self.sensor_name: if sensor["name"] != self.sensor_name:
continue continue
for item in sensor["capability"]: for item in sensor["capability"]:
if item["type"] != self.type: if item["type"] != self.entity_description.key:
continue continue
self._state = item["value"] self._state = item["value"]
break break