From c12b7e70d956b67df259b672530b0aa8d0d2456c Mon Sep 17 00:00:00 2001 From: zyell Date: Thu, 13 Aug 2015 11:38:57 -0700 Subject: [PATCH 1/3] Updated to support Nest API changes and fix set_temp and away_mode and pull in new required python-nest version --- homeassistant/components/thermostat/nest.py | 42 +++++++++++++++++++-- requirements.txt | 2 +- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/thermostat/nest.py b/homeassistant/components/thermostat/nest.py index b2e48b96bcd..5b50c444dc7 100644 --- a/homeassistant/components/thermostat/nest.py +++ b/homeassistant/components/thermostat/nest.py @@ -34,7 +34,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): napi = nest.Nest(username, password) add_devices([ - NestThermostat(structure, device) + NestThermostat(nest.utils, structure, device) for structure in napi.structures for device in structure.devices ]) @@ -43,14 +43,23 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class NestThermostat(ThermostatDevice): """ Represents a Nest thermostat within Home Assistant. """ - def __init__(self, structure, device): + def __init__(self, nest_utils, structure, device): self.structure = structure self.device = device + self.nest_utils = nest_utils @property def name(self): """ Returns the name of the nest, if any. """ - return self.device.name + location = self.device.where + name = self.device.name + if location is None: + return name + else: + if name == '': + return location.capitalize() + else: + return location.capitalize() + '(' + name + ')' @property def unit_of_measurement(self): @@ -97,9 +106,16 @@ class NestThermostat(ThermostatDevice): """ Returns if away mode is on. """ return self.structure.away + def enforce_temp_units(self, temperature): + """ Enforces temp units in C for nest API, returns temp """ + if self.hass.config.temperature_unit == self.unit_of_measurement: + return temperature + else: + return self.nest_utils.f_to_c(temperature) + def set_temperature(self, temperature): """ Set new target temperature """ - self.device.target = temperature + self.device.target = self.enforce_temp_units(temperature) def turn_away_mode_on(self): """ Turns away on. """ @@ -109,6 +125,24 @@ class NestThermostat(ThermostatDevice): """ Turns away off. """ self.structure.away = False + @property + def min_temp(self): + """ Identifies min_temp in Nest API or defaults if not available. """ + temp = self.device.away_temperature.low + if temp is None: + return super().min_temp + else: + return round(self.hass.config.temperature(temp, TEMP_CELCIUS)[0]) + + @property + def max_temp(self): + """ Identifies mxn_temp in Nest API or defaults if not available. """ + temp = self.device.away_temperature.high + if temp is None: + return super().max_temp + else: + return round(self.hass.config.temperature(temp, TEMP_CELCIUS)[0]) + def update(self): """ Python-nest has its own mechanism for staying up to date. """ pass diff --git a/requirements.txt b/requirements.txt index e284cad72b9..8bcd5470034 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,7 +30,7 @@ python-libnmap>=0.6.3 pushbullet.py>=0.7.1 # Nest Thermostat bindings (thermostat.nest) -python-nest>=2.3.1 +python-nest>=2.4.0 # Z-Wave (*.zwave) pydispatcher>=2.0.5 From 6647894c36aed92f81690d7fdc247019f79b9d89 Mon Sep 17 00:00:00 2001 From: zyell Date: Thu, 20 Aug 2015 10:44:46 -0700 Subject: [PATCH 2/3] updated nest to use helper method and updated requirements --- homeassistant/components/thermostat/nest.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/thermostat/nest.py b/homeassistant/components/thermostat/nest.py index 5b50c444dc7..503483ee6c1 100644 --- a/homeassistant/components/thermostat/nest.py +++ b/homeassistant/components/thermostat/nest.py @@ -4,9 +4,10 @@ Adds support for Nest thermostats. import logging from homeassistant.components.thermostat import ThermostatDevice +from homeassistant.helpers.temperature import convert from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, TEMP_CELCIUS) -REQUIREMENTS = ['python-nest>=2.3.1'] +REQUIREMENTS = ['python-nest>=2.4.0'] # pylint: disable=unused-argument @@ -34,7 +35,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): napi = nest.Nest(username, password) add_devices([ - NestThermostat(nest.utils, structure, device) + NestThermostat(structure, device) for structure in napi.structures for device in structure.devices ]) @@ -43,10 +44,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class NestThermostat(ThermostatDevice): """ Represents a Nest thermostat within Home Assistant. """ - def __init__(self, nest_utils, structure, device): + def __init__(self, structure, device): self.structure = structure self.device = device - self.nest_utils = nest_utils @property def name(self): @@ -106,16 +106,11 @@ class NestThermostat(ThermostatDevice): """ Returns if away mode is on. """ return self.structure.away - def enforce_temp_units(self, temperature): - """ Enforces temp units in C for nest API, returns temp """ - if self.hass.config.temperature_unit == self.unit_of_measurement: - return temperature - else: - return self.nest_utils.f_to_c(temperature) - def set_temperature(self, temperature): - """ Set new target temperature """ - self.device.target = self.enforce_temp_units(temperature) + """ Set new target temperature ensuring it is in proper units """ + temperature = convert(temperature, self.hass.config.temperature_unit, + self.unit_of_measurement) + self.device.target = temperature def turn_away_mode_on(self): """ Turns away on. """ From e15eb90b33c228e6ca8ec8aafa4cdc0c63f2dc04 Mon Sep 17 00:00:00 2001 From: zyell Date: Mon, 24 Aug 2015 16:50:22 -0700 Subject: [PATCH 3/3] Have thermostat class make all necessary unit conversions --- .../components/thermostat/__init__.py | 32 ++++++++++++++----- homeassistant/components/thermostat/nest.py | 11 +++---- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/thermostat/__init__.py b/homeassistant/components/thermostat/__init__.py index 40e392709f2..bbc0979e38c 100644 --- a/homeassistant/components/thermostat/__init__.py +++ b/homeassistant/components/thermostat/__init__.py @@ -10,6 +10,7 @@ from homeassistant.helpers.entity_component import EntityComponent import homeassistant.util as util from homeassistant.helpers.entity import Entity +from homeassistant.helpers.temperature import convert from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_TEMPERATURE, STATE_ON, STATE_OFF, TEMP_CELCIUS) @@ -86,7 +87,9 @@ def setup(hass, config): return for thermostat in target_thermostats: - thermostat.set_temperature(temperature) + thermostat.set_temperature(convert( + temperature, hass.config.temperature_unit, + thermostat.unit_of_measurement)) for thermostat in target_thermostats: thermostat.update_ha_state(True) @@ -118,9 +121,20 @@ class ThermostatDevice(Entity): @property def state_attributes(self): """ Returns optional state attributes. """ + + thermostat_unit = self.unit_of_measurement + user_unit = self.hass.config.temperature_unit + data = { - ATTR_CURRENT_TEMPERATURE: self.hass.config.temperature( - self.current_temperature, self.unit_of_measurement)[0] + ATTR_CURRENT_TEMPERATURE: round(convert(self.current_temperature, + thermostat_unit, + user_unit), 1), + ATTR_MIN_TEMP: round(convert(self.min_temp, + thermostat_unit, + user_unit), 0), + ATTR_MAX_TEMP: round(convert(self.max_temp, + thermostat_unit, + user_unit), 0) } is_away = self.is_away_mode_on @@ -133,11 +147,13 @@ class ThermostatDevice(Entity): if device_attr is not None: data.update(device_attr) - data[ATTR_MIN_TEMP] = self.min_temp - data[ATTR_MAX_TEMP] = self.max_temp - return data + @property + def unit_of_measurement(self): + """ Unit of measurement this thermostat expresses itself in. """ + return NotImplementedError + @property def current_temperature(self): """ Returns the current temperature. """ @@ -171,9 +187,9 @@ class ThermostatDevice(Entity): @property def min_temp(self): """ Return minimum temperature. """ - return self.hass.config.temperature(7, TEMP_CELCIUS)[0] + return convert(7, TEMP_CELCIUS, self.unit_of_measurement) @property def max_temp(self): """ Return maxmum temperature. """ - return self.hass.config.temperature(35, TEMP_CELCIUS)[0] + return convert(35, TEMP_CELCIUS, self.unit_of_measurement) diff --git a/homeassistant/components/thermostat/nest.py b/homeassistant/components/thermostat/nest.py index 503483ee6c1..cb74fa091ff 100644 --- a/homeassistant/components/thermostat/nest.py +++ b/homeassistant/components/thermostat/nest.py @@ -4,7 +4,6 @@ Adds support for Nest thermostats. import logging from homeassistant.components.thermostat import ThermostatDevice -from homeassistant.helpers.temperature import convert from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, TEMP_CELCIUS) REQUIREMENTS = ['python-nest>=2.4.0'] @@ -63,7 +62,7 @@ class NestThermostat(ThermostatDevice): @property def unit_of_measurement(self): - """ Returns the unit of measurement. """ + """ Unit of measurement this thermostat expresses itself in. """ return TEMP_CELCIUS @property @@ -107,9 +106,7 @@ class NestThermostat(ThermostatDevice): return self.structure.away def set_temperature(self, temperature): - """ Set new target temperature ensuring it is in proper units """ - temperature = convert(temperature, self.hass.config.temperature_unit, - self.unit_of_measurement) + """ Set new target temperature """ self.device.target = temperature def turn_away_mode_on(self): @@ -127,7 +124,7 @@ class NestThermostat(ThermostatDevice): if temp is None: return super().min_temp else: - return round(self.hass.config.temperature(temp, TEMP_CELCIUS)[0]) + return temp @property def max_temp(self): @@ -136,7 +133,7 @@ class NestThermostat(ThermostatDevice): if temp is None: return super().max_temp else: - return round(self.hass.config.temperature(temp, TEMP_CELCIUS)[0]) + return temp def update(self): """ Python-nest has its own mechanism for staying up to date. """