diff --git a/homeassistant/components/nest.py b/homeassistant/components/nest.py new file mode 100644 index 00000000000..af71774b591 --- /dev/null +++ b/homeassistant/components/nest.py @@ -0,0 +1,45 @@ +""" +homeassistant.components.thermostat.nest +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Adds support for Nest thermostats. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/thermostat.nest/ +""" +import logging + +from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD) + +REQUIREMENTS = ['python-nest==2.6.0'] +DOMAIN = 'nest' + +NEST = None + + +# pylint: disable=unused-argument +def setup(hass, config): + """ Sets up the nest thermostat. """ + global NEST + + logger = logging.getLogger(__name__) + print("nest config", config[DOMAIN]) + username = config[DOMAIN].get(CONF_USERNAME) + password = config[DOMAIN].get(CONF_PASSWORD) + + if username is None or password is None: + logger.error("Missing required configuration items %s or %s", + CONF_USERNAME, CONF_PASSWORD) + return + + try: + import nest + except ImportError: + logger.exception( + "Error while importing dependency nest. " + "Did you maybe not install the python-nest dependency?") + + return + + NEST = nest.Nest(username, password) + + return True diff --git a/homeassistant/components/sensor/nest.py b/homeassistant/components/sensor/nest.py new file mode 100644 index 00000000000..6f850a30b72 --- /dev/null +++ b/homeassistant/components/sensor/nest.py @@ -0,0 +1,116 @@ +""" +homeassistant.components.sensor.nest +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Support for Nest Thermostat Sensors. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.nest/ +""" +from homeassistant.helpers.entity import Entity +from homeassistant.const import (STATE_ON, STATE_OFF, TEMP_CELCIUS) +from homeassistant.helpers.temperature import convert + +import homeassistant.components.nest as nest +import logging +import socket + +DEPENDENCIES = ['nest'] +SENSOR_TYPES = ['humidity', + 'mode', + 'last_ip', + 'local_ip', + 'last_connection', + 'battery_level'] + +BINARY_TYPES = ['fan', + 'hvac_ac_state', + 'hvac_aux_heater_state', + 'hvac_heat_x2_state', + 'hvac_heat_x3_state', + 'hvac_alt_heat_state', + 'hvac_alt_heat_x2_state', + 'hvac_emer_heat_state', + 'online'] + +SENSOR_UNITS = {'humidity': '%', 'battery_level': '%'} + +SENSOR_TEMP_TYPES = ['temperature', + 'target', + 'away_temperature[0]', + 'away_temperature[1]'] + +def setup_platform(hass, config, add_devices, discovery_info=None): + logger = logging.getLogger(__name__) + try: + for structure in nest.NEST.structures: + for device in structure.devices: + for variable in config['monitored_conditions']: + if variable in SENSOR_TYPES: + add_devices([NestSensor(structure, device, variable)]) + elif variable in BINARY_TYPES: + add_devices([NestBinarySensor(structure, device, variable)]) + elif variable in SENSOR_TEMP_TYPES: + add_devices([NestTempSensor(structure, device, variable)]) + else: + logger.error('Nest sensor type: "%s" does not exist', variable) + except socket.error: + logger.error( + "Connection error logging into the nest web service." + ) + +class NestSensor(Entity): + """ Represents a Nest sensor. """ + + def __init__(self, structure, device, variable): + self.structure = structure + self.device = device + self.variable = variable + + @property + def name(self): + """ Returns the name of the nest, if any. """ + location = self.device.where + name = self.device.name + if location is None: + return name + ' ' + self.variable + else: + if name == '': + return location.capitalize() + ' ' + self.variable + else: + return location.capitalize() + '(' + name + ')' + self.variable + @property + def state(self): + """ Returns the state of the sensor. """ + return getattr(self.device, self.variable) + + @property + def unit_of_measurement(self): + return SENSOR_UNITS.get(self.variable, None) + +class NestTempSensor(NestSensor): + """ Represents a Nest Temperature sensor. """ + + @property + def unit_of_measurement(self): + return self.hass.config.temperature_unit + + @property + def state(self): + temp = getattr(self.device, self.variable) + if temp is None: + return None + + value = convert(temp, TEMP_CELCIUS, + self.hass.config.temperature_unit) + + return round(value, 1) + +class NestBinarySensor(NestSensor): + """ Represents a Nst Binary sensor. """ + + @property + def state(self): + if getattr(self.device, self.variable): + return STATE_ON + else: + return STATE_OFF diff --git a/homeassistant/components/thermostat/nest.py b/homeassistant/components/thermostat/nest.py index f38935b726e..5530f021755 100644 --- a/homeassistant/components/thermostat/nest.py +++ b/homeassistant/components/thermostat/nest.py @@ -11,38 +11,18 @@ import logging from homeassistant.components.thermostat import (ThermostatDevice, STATE_COOL, STATE_IDLE, STATE_HEAT) -from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, TEMP_CELCIUS) +from homeassistant.const import (TEMP_CELCIUS) +import homeassistant.components.nest as nest -REQUIREMENTS = ['python-nest==2.6.0'] +DEPENDENCIES = ['nest'] - -# pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): - """ Sets up the nest thermostat. """ 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) - return - - try: - import nest - except ImportError: - logger.exception( - "Error while importing dependency nest. " - "Did you maybe not install the python-nest dependency?") - - return - - napi = nest.Nest(username, password) try: add_devices([ NestThermostat(structure, device) - for structure in napi.structures + for structure in nest.NEST.structures for device in structure.devices ]) except socket.error: @@ -50,7 +30,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None): "Connection error logging into the nest web service." ) - class NestThermostat(ThermostatDevice): """ Represents a Nest thermostat. """