mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Use EntityDescription - ecobee (#55088)
This commit is contained in:
parent
2cdaf632a4
commit
ce5c76869d
@ -1,7 +1,9 @@
|
||||
"""Support for Ecobee sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
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 (
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
@ -11,45 +13,51 @@ from homeassistant.const import (
|
||||
|
||||
from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER
|
||||
|
||||
SENSOR_TYPES = {
|
||||
"temperature": ["Temperature", TEMP_FAHRENHEIT, DEVICE_CLASS_TEMPERATURE],
|
||||
"humidity": ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
||||
}
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
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):
|
||||
"""Set up ecobee (temperature and humidity) sensors."""
|
||||
data = hass.data[DOMAIN]
|
||||
dev = []
|
||||
for index in range(len(data.ecobee.thermostats)):
|
||||
for sensor in data.ecobee.get_remote_sensors(index):
|
||||
for item in sensor["capability"]:
|
||||
if item["type"] not in ("temperature", "humidity"):
|
||||
continue
|
||||
entities = [
|
||||
EcobeeSensor(data, sensor["name"], index, description)
|
||||
for index in range(len(data.ecobee.thermostats))
|
||||
for sensor in data.ecobee.get_remote_sensors(index)
|
||||
for item in sensor["capability"]
|
||||
for description in SENSOR_TYPES
|
||||
if description.key == item["type"]
|
||||
]
|
||||
|
||||
dev.append(EcobeeSensor(data, sensor["name"], item["type"], index))
|
||||
|
||||
async_add_entities(dev, True)
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class EcobeeSensor(SensorEntity):
|
||||
"""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."""
|
||||
self.entity_description = description
|
||||
self.data = data
|
||||
self._name = f"{sensor_name} {SENSOR_TYPES[sensor_type][0]}"
|
||||
self.sensor_name = sensor_name
|
||||
self.type = sensor_type
|
||||
self.index = sensor_index
|
||||
self._state = None
|
||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the Ecobee sensor."""
|
||||
return self._name
|
||||
self._attr_name = f"{sensor_name} {description.name}"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
@ -99,13 +107,6 @@ class EcobeeSensor(SensorEntity):
|
||||
thermostat = self.data.ecobee.get_thermostat(self.index)
|
||||
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
|
||||
def native_value(self):
|
||||
"""Return the state of the sensor."""
|
||||
@ -116,16 +117,11 @@ class EcobeeSensor(SensorEntity):
|
||||
):
|
||||
return None
|
||||
|
||||
if self.type == "temperature":
|
||||
if self.entity_description.key == "temperature":
|
||||
return float(self._state) / 10
|
||||
|
||||
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):
|
||||
"""Get the latest state of the sensor."""
|
||||
await self.data.update()
|
||||
@ -133,7 +129,7 @@ class EcobeeSensor(SensorEntity):
|
||||
if sensor["name"] != self.sensor_name:
|
||||
continue
|
||||
for item in sensor["capability"]:
|
||||
if item["type"] != self.type:
|
||||
if item["type"] != self.entity_description.key:
|
||||
continue
|
||||
self._state = item["value"]
|
||||
break
|
||||
|
Loading…
x
Reference in New Issue
Block a user