core/homeassistant/components/ecobee/sensor.py

80 lines
2.5 KiB
Python
Raw Normal View History

"""Support for Ecobee sensors."""
2015-11-24 14:29:33 +00:00
from homeassistant.components import ecobee
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
2019-07-31 19:25:30 +00:00
ECOBEE_CONFIG_FILE = "ecobee.conf"
SENSOR_TYPES = {
2019-07-31 19:25:30 +00:00
"temperature": ["Temperature", TEMP_FAHRENHEIT],
"humidity": ["Humidity", "%"],
}
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Ecobee sensors."""
if discovery_info is None:
return
data = ecobee.NETWORK
dev = list()
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"):
continue
2019-07-31 19:25:30 +00:00
dev.append(EcobeeSensor(sensor["name"], item["type"], index))
add_entities(dev, True)
class EcobeeSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of an Ecobee sensor."""
def __init__(self, sensor_name, sensor_type, sensor_index):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2019-07-31 19:25:30 +00:00
self._name = "{} {}".format(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]
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the Ecobee sensor."""
return self._name
2018-04-20 13:38:27 +00:00
@property
def device_class(self):
"""Return the device class of the sensor."""
if self.type in (DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE):
2018-04-20 13:38:27 +00:00
return self.type
return None
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
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."""
return self._unit_of_measurement
def update(self):
2016-02-23 05:21:49 +00:00
"""Get the latest state of the sensor."""
data = ecobee.NETWORK
data.update()
for sensor in 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"]:
if self.type == "temperature" and item["value"] != "unknown":
self._state = float(item["value"]) / 10
else:
2019-07-31 19:25:30 +00:00
self._state = item["value"]