2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ecobee sensors."""
|
2019-09-25 20:38:21 +00:00
|
|
|
from pyecobee.const import ECOBEE_STATE_CALIBRATING, ECOBEE_STATE_UNKNOWN
|
|
|
|
|
2018-05-05 13:37:40 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-11-18 00:14:29 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
from .const import DOMAIN
|
2017-06-05 19:28:13 +00:00
|
|
|
|
2015-11-18 00:14:29 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"temperature": ["Temperature", TEMP_FAHRENHEIT],
|
|
|
|
"humidity": ["Humidity", "%"],
|
2015-11-18 00:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Old way of setting up ecobee sensors."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up ecobee (temperature and humidity) sensors."""
|
|
|
|
data = hass.data[DOMAIN]
|
2015-12-02 22:42:53 +00:00
|
|
|
dev = list()
|
2015-12-02 21:22:25 +00:00
|
|
|
for index in range(len(data.ecobee.thermostats)):
|
|
|
|
for sensor in data.ecobee.get_remote_sensors(index):
|
2019-07-31 19:25:30 +00:00
|
|
|
for item in sensor["capability"]:
|
|
|
|
if item["type"] not in ("temperature", "humidity"):
|
2015-12-03 13:57:28 +00:00
|
|
|
continue
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
dev.append(EcobeeSensor(data, sensor["name"], item["type"], index))
|
2015-11-18 00:14:29 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
async_add_entities(dev, True)
|
2015-11-18 00:14:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EcobeeSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of an Ecobee sensor."""
|
2015-11-18 00:14:29 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
def __init__(self, data, sensor_name, sensor_type, sensor_index):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-09-25 20:38:21 +00:00
|
|
|
self.data = data
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "{} {}".format(sensor_name, SENSOR_TYPES[sensor_type][0])
|
2015-11-18 00:14:29 +00:00
|
|
|
self.sensor_name = sensor_name
|
|
|
|
self.type = sensor_type
|
2015-12-02 21:22:25 +00:00
|
|
|
self.index = sensor_index
|
2015-11-18 00:14:29 +00:00
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the Ecobee sensor."""
|
2018-01-30 09:39:39 +00:00
|
|
|
return self._name
|
2015-11-18 00:14:29 +00:00
|
|
|
|
2018-04-20 13:38:27 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
2018-05-05 13:37:40 +00:00
|
|
|
if self.type in (DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE):
|
2018-04-20 13:38:27 +00:00
|
|
|
return self.type
|
|
|
|
return None
|
|
|
|
|
2015-11-18 00:14:29 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2019-09-25 20:38:21 +00:00
|
|
|
if self._state in [ECOBEE_STATE_CALIBRATING, ECOBEE_STATE_UNKNOWN]:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if self.type == "temperature":
|
|
|
|
return float(self._state) / 10
|
|
|
|
|
2015-11-18 00:14:29 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement this sensor expresses itself in."""
|
2015-11-18 00:14:29 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
async def async_update(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Get the latest state of the sensor."""
|
2019-09-25 20:38:21 +00:00
|
|
|
await self.data.update()
|
|
|
|
for sensor in self.data.ecobee.get_remote_sensors(self.index):
|
2019-07-31 19:25:30 +00:00
|
|
|
for item in sensor["capability"]:
|
|
|
|
if item["type"] == self.type and self.sensor_name == sensor["name"]:
|
2019-09-25 20:38:21 +00:00
|
|
|
self._state = item["value"]
|