2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ecobee binary sensors."""
|
2019-09-25 20:38:21 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDevice,
|
|
|
|
DEVICE_CLASS_OCCUPANCY,
|
|
|
|
)
|
2016-08-19 07:11:56 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
from .const import DOMAIN
|
2016-08-19 07:11:56 +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 binary sensors."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up ecobee binary (occupancy) sensors."""
|
|
|
|
data = hass.data[DOMAIN]
|
2016-08-19 07:11:56 +00:00
|
|
|
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"] != "occupancy":
|
2016-08-19 07:11:56 +00:00
|
|
|
continue
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
dev.append(EcobeeBinarySensor(data, sensor["name"], index))
|
2016-08-19 07:11:56 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
async_add_entities(dev, True)
|
2016-08-19 07:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EcobeeBinarySensor(BinarySensorDevice):
|
|
|
|
"""Representation of an Ecobee sensor."""
|
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
def __init__(self, data, sensor_name, sensor_index):
|
2019-02-13 20:21:14 +00:00
|
|
|
"""Initialize the Ecobee sensor."""
|
2019-09-25 20:38:21 +00:00
|
|
|
self.data = data
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = sensor_name + " Occupancy"
|
2016-08-19 07:11:56 +00:00
|
|
|
self.sensor_name = sensor_name
|
|
|
|
self.index = sensor_index
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the Ecobee sensor."""
|
|
|
|
return self._name.rstrip()
|
|
|
|
|
2019-10-01 21:28:13 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this sensor."""
|
|
|
|
for sensor in self.data.ecobee.get_remote_sensors(self.index):
|
|
|
|
if sensor["name"] == self.sensor_name:
|
|
|
|
if "code" in sensor:
|
|
|
|
return f"{sensor['code']}-{self.device_class}"
|
|
|
|
return f"{sensor['id']}-{self.device_class}"
|
|
|
|
|
2016-08-19 07:11:56 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the status of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._state == "true"
|
2016-08-19 07:11:56 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor, from DEVICE_CLASSES."""
|
2019-09-25 20:38:21 +00:00
|
|
|
return DEVICE_CLASS_OCCUPANCY
|
2016-08-19 07:11:56 +00:00
|
|
|
|
2019-09-25 20:38:21 +00:00
|
|
|
async def async_update(self):
|
2016-08-19 07:11:56 +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"] == "occupancy" and self.sensor_name == sensor["name"]:
|
|
|
|
self._state = item["value"]
|