Add ecobee CO2, VOC, and AQI sensors (#76366)

* Add support for CO2, VOC, and AQI sensors

* Update sensor.py

* Formatting changes

* Use class thermostat member in async_update

* Remove thermostat attribute, add mixin class

* Add docstrings

* Add blank line

* Sort imports

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/76336/head
rlippmann 2022-08-08 16:34:38 -04:00 committed by GitHub
parent d139d1e175
commit 36d6ef6228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 60 additions and 8 deletions

View File

@ -1,6 +1,8 @@
"""Support for Ecobee sensors.""" """Support for Ecobee sensors."""
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
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 ( from homeassistant.components.sensor import (
@ -10,27 +12,73 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, TEMP_FAHRENHEIT from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
TEMP_FAHRENHEIT,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER from .const import DOMAIN, ECOBEE_MODEL_TO_NAME, MANUFACTURER
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( @dataclass
class EcobeeSensorEntityDescriptionMixin:
"""Represent the required ecobee entity description attributes."""
runtime_key: str
@dataclass
class EcobeeSensorEntityDescription(
SensorEntityDescription, EcobeeSensorEntityDescriptionMixin
):
"""Represent the ecobee sensor entity description."""
SENSOR_TYPES: tuple[EcobeeSensorEntityDescription, ...] = (
EcobeeSensorEntityDescription(
key="temperature", key="temperature",
name="Temperature", name="Temperature",
native_unit_of_measurement=TEMP_FAHRENHEIT, native_unit_of_measurement=TEMP_FAHRENHEIT,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
runtime_key="actualTemperature",
), ),
SensorEntityDescription( EcobeeSensorEntityDescription(
key="humidity", key="humidity",
name="Humidity", name="Humidity",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.HUMIDITY, device_class=SensorDeviceClass.HUMIDITY,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
runtime_key="actualHumidity",
),
EcobeeSensorEntityDescription(
key="co2PPM",
name="CO2",
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
device_class=SensorDeviceClass.CO2,
state_class=SensorStateClass.MEASUREMENT,
runtime_key="actualCO2",
),
EcobeeSensorEntityDescription(
key="vocPPM",
name="VOC",
device_class=SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
runtime_key="actualVOC",
),
EcobeeSensorEntityDescription(
key="airQuality",
name="Air Quality Index",
device_class=SensorDeviceClass.AQI,
native_unit_of_measurement=None,
state_class=SensorStateClass.MEASUREMENT,
runtime_key="actualAQScore",
), ),
) )
@ -40,7 +88,7 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up ecobee (temperature and humidity) sensors.""" """Set up ecobee sensors."""
data = hass.data[DOMAIN] data = hass.data[DOMAIN]
entities = [ entities = [
EcobeeSensor(data, sensor["name"], index, description) EcobeeSensor(data, sensor["name"], index, description)
@ -58,7 +106,11 @@ class EcobeeSensor(SensorEntity):
"""Representation of an Ecobee sensor.""" """Representation of an Ecobee sensor."""
def __init__( def __init__(
self, data, sensor_name, sensor_index, description: SensorEntityDescription self,
data,
sensor_name,
sensor_index,
description: EcobeeSensorEntityDescription,
): ):
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description self.entity_description = description
@ -66,7 +118,6 @@ class EcobeeSensor(SensorEntity):
self.sensor_name = sensor_name self.sensor_name = sensor_name
self.index = sensor_index self.index = sensor_index
self._state = None self._state = None
self._attr_name = f"{sensor_name} {description.name}" self._attr_name = f"{sensor_name} {description.name}"
@property @property
@ -141,5 +192,6 @@ class EcobeeSensor(SensorEntity):
for item in sensor["capability"]: for item in sensor["capability"]:
if item["type"] != self.entity_description.key: if item["type"] != self.entity_description.key:
continue continue
self._state = item["value"] thermostat = self.data.ecobee.get_thermostat(self.index)
self._state = thermostat["runtime"][self.entity_description.runtime_key]
break break