From 08de7d954aeb416b369554e770bc2bca782d1f74 Mon Sep 17 00:00:00 2001 From: "nkgilley@gmail.com" Date: Wed, 2 Dec 2015 16:22:25 -0500 Subject: [PATCH 1/4] improve support for multiple thermostats. --- homeassistant/components/ecobee.py | 2 +- homeassistant/components/sensor/ecobee.py | 52 ++++++++++++++----- homeassistant/components/thermostat/ecobee.py | 20 +++---- requirements_all.txt | 2 +- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/ecobee.py b/homeassistant/components/ecobee.py index 03f17133501..0dc69c9d7a2 100644 --- a/homeassistant/components/ecobee.py +++ b/homeassistant/components/ecobee.py @@ -44,7 +44,7 @@ HOLD_TEMP = 'hold_temp' REQUIREMENTS = [ 'https://github.com/nkgilley/python-ecobee-api/archive/' - 'd35596b67c75451fa47001c493a15eebee195e93.zip#python-ecobee==0.0.1'] + '5645f843b64ac4f6e59dfb96233a07083c5e10c1.zip#python-ecobee==0.0.3'] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/sensor/ecobee.py b/homeassistant/components/sensor/ecobee.py index a6499949015..c1bf5eab052 100644 --- a/homeassistant/components/sensor/ecobee.py +++ b/homeassistant/components/sensor/ecobee.py @@ -43,18 +43,36 @@ _LOGGER = logging.getLogger(__name__) ECOBEE_CONFIG_FILE = 'ecobee.conf' + def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the sensors. """ if discovery_info is None: return + data = ecobee.NETWORK + sensor_list = list() + for index in range(len(data.ecobee.thermostats)): + sensors = dict() + for sensor in data.ecobee.get_remote_sensors(index): + sensor_info = dict() + for item in sensor['capability']: + if item['type'] == 'temperature': + sensor_info['temp'] = float(item['value']) / 10 + elif item['type'] == 'humidity': + sensor_info['humidity'] = item['value'] + elif item['type'] == 'occupancy': + sensor_info['occupancy'] = item['value'] + sensors[sensor['name']] = sensor_info + sensor_list.append(sensors) + dev = list() - for name, data in ecobee.NETWORK.ecobee.sensors.items(): - if 'temp' in data: - dev.append(EcobeeSensor(name, 'temperature')) - if 'humidity' in data: - dev.append(EcobeeSensor(name, 'humidity')) - if 'occupancy' in data: - dev.append(EcobeeSensor(name, 'occupancy')) + for index in range(len(sensor_list)): + for name, data in sensor_list[index].items(): + if 'temp' in data: + dev.append(EcobeeSensor(name, 'temperature', index)) + if 'humidity' in data: + dev.append(EcobeeSensor(name, 'humidity', index)) + if 'occupancy' in data: + dev.append(EcobeeSensor(name, 'occupancy', index)) add_devices(dev) @@ -62,10 +80,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class EcobeeSensor(Entity): """ An ecobee sensor. """ - def __init__(self, sensor_name, sensor_type): + def __init__(self, sensor_name, sensor_type, sensor_index): self._name = 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] self.update() @@ -85,10 +104,19 @@ class EcobeeSensor(Entity): def update(self): ecobee.NETWORK.update() - data = ecobee.NETWORK.ecobee.sensors[self.sensor_name] + data = ecobee.NETWORK + for sensor in data.ecobee.get_remote_sensors(self.index): + sensor_info = dict() + for item in sensor['capability']: + if item['type'] == 'temperature': + sensor_info['temp'] = float(item['value']) / 10 + elif item['type'] == 'humidity': + sensor_info['humidity'] = item['value'] + elif item['type'] == 'occupancy': + sensor_info['occupancy'] = item['value'] if self.type == 'temperature': - self._state = data['temp'] + self._state = sensor_info['temp'] elif self.type == 'humidity': - self._state = data['humidity'] + self._state = sensor_info['humidity'] elif self.type == 'occupancy': - self._state = data['occupancy'] + self._state = sensor_info['occupancy'] diff --git a/homeassistant/components/thermostat/ecobee.py b/homeassistant/components/thermostat/ecobee.py index 78f4d555c9c..f55bdace587 100644 --- a/homeassistant/components/thermostat/ecobee.py +++ b/homeassistant/components/thermostat/ecobee.py @@ -163,14 +163,14 @@ class Thermostat(ThermostatDevice): """ Turns away on. """ self._away = True if self.hold_temp: - self.data.ecobee.set_climate_hold("away", "indefinite") + self.data.ecobee.set_climate_hold(self.thermostat_index, "away", "indefinite") else: - self.data.ecobee.set_climate_hold("away") + self.data.ecobee.set_climate_hold(self.thermostat_index, "away") def turn_away_mode_off(self): """ Turns away off. """ self._away = False - self.data.ecobee.resume_program() + self.data.ecobee.resume_program(self.thermostat_index) def set_temperature(self, temperature): """ Set new target temperature """ @@ -178,32 +178,32 @@ class Thermostat(ThermostatDevice): low_temp = temperature - 1 high_temp = temperature + 1 if self.hold_temp: - self.data.ecobee.set_hold_temp(low_temp, high_temp, "indefinite") + self.data.ecobee.set_hold_temp(self.thermostat_index, low_temp, high_temp, "indefinite") else: - self.data.ecobee.set_hold_temp(low_temp, high_temp) + self.data.ecobee.set_hold_temp(self.thermostat_index, low_temp, high_temp) def set_hvac_mode(self, mode): """ Set HVAC mode (auto, auxHeatOnly, cool, heat, off) """ - self.data.ecobee.set_hvac_mode(mode) + self.data.ecobee.set_hvac_mode(self.thermostat_index, mode) # Home and Sleep mode aren't used in UI yet: # def turn_home_mode_on(self): # """ Turns home mode on. """ # self._away = False - # self.data.ecobee.set_climate_hold("home") + # self.data.ecobee.set_climate_hold(self.thermostat_index, "home") # def turn_home_mode_off(self): # """ Turns home mode off. """ # self._away = False - # self.data.ecobee.resume_program() + # self.data.ecobee.resume_program(self.thermostat_index) # def turn_sleep_mode_on(self): # """ Turns sleep mode on. """ # self._away = False - # self.data.ecobee.set_climate_hold("sleep") + # self.data.ecobee.set_climate_hold(self.thermostat_index, "sleep") # def turn_sleep_mode_off(self): # """ Turns sleep mode off. """ # self._away = False - # self.data.ecobee.resume_program() + # self.data.ecobee.resume_program(self.thermostat_index) diff --git a/requirements_all.txt b/requirements_all.txt index b74226f4991..06464e4e00a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -161,4 +161,4 @@ pushetta==1.0.15 orvibo==1.0.0 # Ecobee (*.ecobee) -https://github.com/nkgilley/python-ecobee-api/archive/d35596b67c75451fa47001c493a15eebee195e93.zip#python-ecobee==0.0.1 +https://github.com/nkgilley/python-ecobee-api/archive/5645f843b64ac4f6e59dfb96233a07083c5e10c1.zip#python-ecobee==0.0.3 From 502184812da72f07285c9028bc3af3d63237eee9 Mon Sep 17 00:00:00 2001 From: "nkgilley@gmail.com" Date: Wed, 2 Dec 2015 16:37:16 -0500 Subject: [PATCH 2/4] fix flake8 warnings --- homeassistant/components/sensor/ecobee.py | 1 - homeassistant/components/thermostat/ecobee.py | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/ecobee.py b/homeassistant/components/sensor/ecobee.py index fd70aadf001..5cf6875aded 100644 --- a/homeassistant/components/sensor/ecobee.py +++ b/homeassistant/components/sensor/ecobee.py @@ -44,7 +44,6 @@ _LOGGER = logging.getLogger(__name__) ECOBEE_CONFIG_FILE = 'ecobee.conf' - def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the sensors. """ if discovery_info is None: diff --git a/homeassistant/components/thermostat/ecobee.py b/homeassistant/components/thermostat/ecobee.py index 12ff186d766..a10d2940001 100644 --- a/homeassistant/components/thermostat/ecobee.py +++ b/homeassistant/components/thermostat/ecobee.py @@ -164,7 +164,8 @@ class Thermostat(ThermostatDevice): """ Turns away on. """ self._away = True if self.hold_temp: - self.data.ecobee.set_climate_hold(self.thermostat_index, "away", "indefinite") + self.data.ecobee.set_climate_hold(self.thermostat_index, + "away", "indefinite") else: self.data.ecobee.set_climate_hold(self.thermostat_index, "away") @@ -179,9 +180,11 @@ class Thermostat(ThermostatDevice): low_temp = temperature - 1 high_temp = temperature + 1 if self.hold_temp: - self.data.ecobee.set_hold_temp(self.thermostat_index, low_temp, high_temp, "indefinite") + self.data.ecobee.set_hold_temp(self.thermostat_index, low_temp, + high_temp, "indefinite") else: - self.data.ecobee.set_hold_temp(self.thermostat_index, low_temp, high_temp) + self.data.ecobee.set_hold_temp(self.thermostat_index, low_temp, + high_temp) def set_hvac_mode(self, mode): """ Set HVAC mode (auto, auxHeatOnly, cool, heat, off) """ From 7985468aba98da620ce2d7c72835064573a0e805 Mon Sep 17 00:00:00 2001 From: "nkgilley@gmail.com" Date: Wed, 2 Dec 2015 17:42:53 -0500 Subject: [PATCH 3/4] remove the use of unnecessary dictionary. --- homeassistant/components/sensor/ecobee.py | 55 ++++++++++------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/sensor/ecobee.py b/homeassistant/components/sensor/ecobee.py index 5cf6875aded..9f4b6ef23e0 100644 --- a/homeassistant/components/sensor/ecobee.py +++ b/homeassistant/components/sensor/ecobee.py @@ -49,30 +49,19 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if discovery_info is None: return data = ecobee.NETWORK - sensor_list = list() + dev = list() for index in range(len(data.ecobee.thermostats)): - sensors = dict() for sensor in data.ecobee.get_remote_sensors(index): - sensor_info = dict() for item in sensor['capability']: if item['type'] == 'temperature': - sensor_info['temp'] = float(item['value']) / 10 + dev.append( + EcobeeSensor(sensor['name'], 'temperature', index)) elif item['type'] == 'humidity': - sensor_info['humidity'] = item['value'] + dev.append( + EcobeeSensor(sensor['name'], 'humidity', index)) elif item['type'] == 'occupancy': - sensor_info['occupancy'] = item['value'] - sensors[sensor['name']] = sensor_info - sensor_list.append(sensors) - - dev = list() - for index in range(len(sensor_list)): - for name, data in sensor_list[index].items(): - if 'temp' in data: - dev.append(EcobeeSensor(name, 'temperature', index)) - if 'humidity' in data: - dev.append(EcobeeSensor(name, 'humidity', index)) - if 'occupancy' in data: - dev.append(EcobeeSensor(name, 'occupancy', index)) + dev.append( + EcobeeSensor(sensor['name'], 'occupancy', index)) add_devices(dev) @@ -103,20 +92,22 @@ class EcobeeSensor(Entity): return self._unit_of_measurement def update(self): - ecobee.NETWORK.update() data = ecobee.NETWORK + data.update() for sensor in data.ecobee.get_remote_sensors(self.index): - sensor_info = dict() for item in sensor['capability']: - if item['type'] == 'temperature': - sensor_info['temp'] = float(item['value']) / 10 - elif item['type'] == 'humidity': - sensor_info['humidity'] = item['value'] - elif item['type'] == 'occupancy': - sensor_info['occupancy'] = item['value'] - if self.type == 'temperature': - self._state = sensor_info['temp'] - elif self.type == 'humidity': - self._state = sensor_info['humidity'] - elif self.type == 'occupancy': - self._state = sensor_info['occupancy'] + if ( + item['type'] == self.type and + self.type == 'temperature' and + self.sensor_name == sensor['name']): + self._state = float(item['value']) / 10 + elif ( + item['type'] == self.type and + self.type == 'humidity' and + self.sensor_name == sensor['name']): + self._state = item['value'] + elif ( + item['type'] == self.type and + self.type == 'occupancy' and + self.sensor_name == sensor['name']): + self._state = item['value'] From 107994f3ac39b60b389fa4d7eb4efbb284b70a10 Mon Sep 17 00:00:00 2001 From: "nkgilley@gmail.com" Date: Thu, 3 Dec 2015 08:57:28 -0500 Subject: [PATCH 4/4] implement logic improvement suggestion by @balloob --- homeassistant/components/sensor/ecobee.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/sensor/ecobee.py b/homeassistant/components/sensor/ecobee.py index 9f4b6ef23e0..02a2575d88b 100644 --- a/homeassistant/components/sensor/ecobee.py +++ b/homeassistant/components/sensor/ecobee.py @@ -53,15 +53,11 @@ 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'] == 'temperature': - dev.append( - EcobeeSensor(sensor['name'], 'temperature', index)) - elif item['type'] == 'humidity': - dev.append( - EcobeeSensor(sensor['name'], 'humidity', index)) - elif item['type'] == 'occupancy': - dev.append( - EcobeeSensor(sensor['name'], 'occupancy', index)) + if item['type'] not in ('temperature', + 'humidity', 'occupancy'): + continue + + dev.append(EcobeeSensor(sensor['name'], item['type'], index)) add_devices(dev)