From 2a563e1604cf52b2efd8908e9e483b430a84bbce Mon Sep 17 00:00:00 2001 From: Nolan Gilley Date: Fri, 19 Aug 2016 03:11:56 -0400 Subject: [PATCH] binary occupancy sensor (#2869) --- .../components/binary_sensor/ecobee.py | 72 +++++++++++++++++++ homeassistant/components/ecobee.py | 1 + homeassistant/components/sensor/ecobee.py | 9 +-- 3 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 homeassistant/components/binary_sensor/ecobee.py diff --git a/homeassistant/components/binary_sensor/ecobee.py b/homeassistant/components/binary_sensor/ecobee.py new file mode 100644 index 00000000000..09cbfd852e3 --- /dev/null +++ b/homeassistant/components/binary_sensor/ecobee.py @@ -0,0 +1,72 @@ +""" +Support for Ecobee sensors. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.ecobee/ +""" +from homeassistant.components import ecobee +from homeassistant.components.binary_sensor import BinarySensorDevice + +DEPENDENCIES = ['ecobee'] + +ECOBEE_CONFIG_FILE = 'ecobee.conf' + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup 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): + for item in sensor['capability']: + if item['type'] != 'occupancy': + continue + + dev.append(EcobeeBinarySensor(sensor['name'], index)) + + add_devices(dev) + + +class EcobeeBinarySensor(BinarySensorDevice): + """Representation of an Ecobee sensor.""" + + def __init__(self, sensor_name, sensor_index): + """Initialize the sensor.""" + self._name = sensor_name + ' Occupancy' + self.sensor_name = sensor_name + self.index = sensor_index + self._state = None + self._sensor_class = 'motion' + self.update() + + @property + def name(self): + """Return the name of the Ecobee sensor.""" + return self._name.rstrip() + + @property + def is_on(self): + """Return the status of the sensor.""" + return self._state == 'true' + + @property + def unique_id(self): + """Return the unique ID of this sensor.""" + return "binary_sensor_ecobee_{}_{}".format(self._name, self.index) + + @property + def sensor_class(self): + """Return the class of this sensor, from SENSOR_CLASSES.""" + return self._sensor_class + + def update(self): + """Get the latest state of the sensor.""" + data = ecobee.NETWORK + data.update() + for sensor in data.ecobee.get_remote_sensors(self.index): + for item in sensor['capability']: + if (item['type'] == 'occupancy' and + self.sensor_name == sensor['name']): + self._state = item['value'] diff --git a/homeassistant/components/ecobee.py b/homeassistant/components/ecobee.py index 48d689364ee..470449b02cb 100644 --- a/homeassistant/components/ecobee.py +++ b/homeassistant/components/ecobee.py @@ -72,6 +72,7 @@ def setup_ecobee(hass, network, config): discovery.load_platform(hass, 'thermostat', DOMAIN, {'hold_temp': hold_temp}, config) discovery.load_platform(hass, 'sensor', DOMAIN, {}, config) + discovery.load_platform(hass, 'binary_sensor', DOMAIN, {}, config) # pylint: disable=too-few-public-methods diff --git a/homeassistant/components/sensor/ecobee.py b/homeassistant/components/sensor/ecobee.py index 61ce2b6770c..961fb9aabe3 100644 --- a/homeassistant/components/sensor/ecobee.py +++ b/homeassistant/components/sensor/ecobee.py @@ -4,8 +4,6 @@ Support for Ecobee sensors. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.ecobee/ """ -import logging - from homeassistant.components import ecobee from homeassistant.const import TEMP_FAHRENHEIT from homeassistant.helpers.entity import Entity @@ -13,11 +11,9 @@ from homeassistant.helpers.entity import Entity DEPENDENCIES = ['ecobee'] SENSOR_TYPES = { 'temperature': ['Temperature', TEMP_FAHRENHEIT], - 'humidity': ['Humidity', '%'], - 'occupancy': ['Occupancy', None] + 'humidity': ['Humidity', '%'] } -_LOGGER = logging.getLogger(__name__) ECOBEE_CONFIG_FILE = 'ecobee.conf' @@ -30,8 +26,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): 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', 'occupancy'): + if item['type'] not in ('temperature', 'humidity'): continue dev.append(EcobeeSensor(sensor['name'], item['type'], index))