2015-01-06 08:10:33 +00:00
|
|
|
"""
|
|
|
|
Adds support for Nest thermostats.
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.thermostat import ThermostatDevice
|
2015-01-13 08:00:26 +00:00
|
|
|
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, TEMP_CELCIUS)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-07-07 07:01:46 +00:00
|
|
|
REQUIREMENTS = ['python-nest>=2.3.1']
|
|
|
|
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-03-01 09:35:58 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Sets up the nest thermostat. """
|
2015-01-06 08:10:33 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
|
|
|
if username is None or password is None:
|
|
|
|
logger.error("Missing required configuration items %s or %s",
|
|
|
|
CONF_USERNAME, CONF_PASSWORD)
|
2015-03-01 09:35:58 +00:00
|
|
|
return
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
try:
|
2015-01-13 07:31:31 +00:00
|
|
|
import nest
|
2015-01-06 08:10:33 +00:00
|
|
|
except ImportError:
|
2015-01-13 07:31:31 +00:00
|
|
|
logger.exception(
|
|
|
|
"Error while importing dependency nest. "
|
|
|
|
"Did you maybe not install the python-nest dependency?")
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-03-01 09:35:58 +00:00
|
|
|
return
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-13 07:31:31 +00:00
|
|
|
napi = nest.Nest(username, password)
|
|
|
|
|
2015-03-01 09:35:58 +00:00
|
|
|
add_devices([
|
2015-08-13 18:38:57 +00:00
|
|
|
NestThermostat(nest.utils, structure, device)
|
2015-01-13 07:31:31 +00:00
|
|
|
for structure in napi.structures
|
2015-03-01 09:35:58 +00:00
|
|
|
for device in structure.devices
|
|
|
|
])
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NestThermostat(ThermostatDevice):
|
|
|
|
""" Represents a Nest thermostat within Home Assistant. """
|
|
|
|
|
2015-08-13 18:38:57 +00:00
|
|
|
def __init__(self, nest_utils, structure, device):
|
2015-01-13 07:31:31 +00:00
|
|
|
self.structure = structure
|
|
|
|
self.device = device
|
2015-08-13 18:38:57 +00:00
|
|
|
self.nest_utils = nest_utils
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2015-01-06 08:10:33 +00:00
|
|
|
""" Returns the name of the nest, if any. """
|
2015-08-13 18:38:57 +00:00
|
|
|
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 + ')'
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
""" Returns the unit of measurement. """
|
2015-01-13 07:31:31 +00:00
|
|
|
return TEMP_CELCIUS
|
2015-01-12 05:21:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
""" Returns device specific state attributes. """
|
2015-01-13 07:31:31 +00:00
|
|
|
# Move these to Thermostat Device and make them global
|
|
|
|
return {
|
|
|
|
"humidity": self.device.humidity,
|
|
|
|
"target_humidity": self.device.target_humidity,
|
|
|
|
"fan": self.device.fan,
|
|
|
|
"mode": self.device.mode
|
|
|
|
}
|
2015-01-12 05:21:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_temperature(self):
|
2015-01-06 08:10:33 +00:00
|
|
|
""" Returns the current temperature. """
|
2015-01-13 07:31:31 +00:00
|
|
|
return round(self.device.temperature, 1)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
@property
|
|
|
|
def target_temperature(self):
|
|
|
|
""" Returns the temperature we try to reach. """
|
2015-06-02 05:00:41 +00:00
|
|
|
target = self.device.target
|
|
|
|
|
|
|
|
if isinstance(target, tuple):
|
|
|
|
low, high = target
|
|
|
|
|
|
|
|
if self.current_temperature < low:
|
2015-06-02 07:02:50 +00:00
|
|
|
temp = low
|
2015-06-02 05:00:41 +00:00
|
|
|
elif self.current_temperature > high:
|
2015-06-02 07:02:50 +00:00
|
|
|
temp = high
|
2015-06-02 05:00:41 +00:00
|
|
|
else:
|
2015-06-02 07:02:50 +00:00
|
|
|
temp = (low + high)/2
|
2015-06-02 05:00:41 +00:00
|
|
|
else:
|
|
|
|
temp = target
|
|
|
|
|
|
|
|
return round(temp, 1)
|
2015-01-12 05:21:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_away_mode_on(self):
|
|
|
|
""" Returns if away mode is on. """
|
2015-01-13 07:31:31 +00:00
|
|
|
return self.structure.away
|
2015-01-12 05:21:18 +00:00
|
|
|
|
2015-08-13 18:38:57 +00:00
|
|
|
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)
|
|
|
|
|
2015-01-06 08:10:33 +00:00
|
|
|
def set_temperature(self, temperature):
|
|
|
|
""" Set new target temperature """
|
2015-08-13 18:38:57 +00:00
|
|
|
self.device.target = self.enforce_temp_units(temperature)
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
def turn_away_mode_on(self):
|
|
|
|
""" Turns away on. """
|
2015-01-13 07:31:31 +00:00
|
|
|
self.structure.away = True
|
2015-01-06 08:10:33 +00:00
|
|
|
|
|
|
|
def turn_away_mode_off(self):
|
|
|
|
""" Turns away off. """
|
2015-01-13 07:31:31 +00:00
|
|
|
self.structure.away = False
|
2015-01-06 08:10:33 +00:00
|
|
|
|
2015-08-13 18:38:57 +00:00
|
|
|
@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])
|
|
|
|
|
2015-01-12 05:21:18 +00:00
|
|
|
def update(self):
|
2015-01-13 07:31:31 +00:00
|
|
|
""" Python-nest has its own mechanism for staying up to date. """
|
|
|
|
pass
|