2016-01-13 21:05:47 -07:00
|
|
|
"""
|
|
|
|
Support for Nest Thermostat Sensors.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.nest/
|
|
|
|
"""
|
2016-05-15 12:29:12 -07:00
|
|
|
from itertools import chain
|
|
|
|
|
2016-04-12 00:52:19 -04:00
|
|
|
import voluptuous as vol
|
2016-01-14 11:37:17 -07:00
|
|
|
|
2016-11-05 01:22:47 +01:00
|
|
|
from homeassistant.components.nest import DATA_NEST, DOMAIN
|
2016-02-18 21:27:50 -08:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-04-12 00:52:19 -04:00
|
|
|
from homeassistant.const import (
|
2016-11-27 19:18:47 -05:00
|
|
|
TEMP_CELSIUS, TEMP_FAHRENHEIT, CONF_PLATFORM,
|
|
|
|
CONF_SCAN_INTERVAL, CONF_MONITORED_CONDITIONS
|
2016-04-12 00:52:19 -04:00
|
|
|
)
|
2016-01-13 21:05:47 -07:00
|
|
|
|
|
|
|
DEPENDENCIES = ['nest']
|
|
|
|
SENSOR_TYPES = ['humidity',
|
2016-09-30 02:44:14 -04:00
|
|
|
'operation_mode',
|
2016-11-27 19:18:47 -05:00
|
|
|
'last_connection']
|
2016-01-13 21:05:47 -07:00
|
|
|
|
2016-11-27 19:18:47 -05:00
|
|
|
SENSOR_TYPES_DEPRECATED = ['battery_health',
|
|
|
|
'last_ip',
|
|
|
|
'local_ip']
|
2016-02-18 15:39:05 -05:00
|
|
|
|
2016-11-27 19:18:47 -05:00
|
|
|
WEATHER_VARS = {}
|
|
|
|
|
|
|
|
DEPRECATED_WEATHER_VARS = {'weather_humidity': 'humidity',
|
|
|
|
'weather_temperature': 'temperature',
|
|
|
|
'weather_condition': 'condition',
|
|
|
|
'wind_speed': 'kph',
|
|
|
|
'wind_direction': 'direction'}
|
|
|
|
|
|
|
|
SENSOR_UNITS = {'humidity': '%',
|
|
|
|
'temperature': '°C'}
|
2016-01-13 21:05:47 -07:00
|
|
|
|
2016-05-15 12:29:12 -07:00
|
|
|
PROTECT_VARS = ['co_status',
|
|
|
|
'smoke_status',
|
2016-11-27 19:18:47 -05:00
|
|
|
'battery_health']
|
|
|
|
|
|
|
|
PROTECT_VARS_DEPRECATED = ['battery_level']
|
2016-05-15 12:29:12 -07:00
|
|
|
|
2016-03-22 08:45:44 -07:00
|
|
|
SENSOR_TEMP_TYPES = ['temperature', 'target']
|
2016-01-13 21:05:47 -07:00
|
|
|
|
2016-05-15 12:29:12 -07:00
|
|
|
_VALID_SENSOR_TYPES = SENSOR_TYPES + SENSOR_TEMP_TYPES + PROTECT_VARS + \
|
2016-04-12 00:52:19 -04:00
|
|
|
list(WEATHER_VARS.keys())
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
2016-11-05 01:22:47 +01:00
|
|
|
vol.Required(CONF_PLATFORM): DOMAIN,
|
2016-04-12 00:52:19 -04:00
|
|
|
vol.Optional(CONF_SCAN_INTERVAL):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
|
|
|
vol.Required(CONF_MONITORED_CONDITIONS): [vol.In(_VALID_SENSOR_TYPES)],
|
|
|
|
})
|
|
|
|
|
2016-01-14 14:19:35 -07:00
|
|
|
|
2016-01-13 21:05:47 -07:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Setup the Nest Sensor."""
|
2016-11-05 01:22:47 +01:00
|
|
|
nest = hass.data[DATA_NEST]
|
2016-11-27 19:18:47 -05:00
|
|
|
conf = config.get(CONF_MONITORED_CONDITIONS, _VALID_SENSOR_TYPES)
|
2016-11-05 01:22:47 +01:00
|
|
|
|
|
|
|
all_sensors = []
|
2016-05-15 12:29:12 -07:00
|
|
|
for structure, device in chain(nest.devices(), nest.protect_devices()):
|
2016-04-12 00:52:19 -04:00
|
|
|
sensors = [NestBasicSensor(structure, device, variable)
|
2016-11-27 19:18:47 -05:00
|
|
|
for variable in conf
|
2016-05-15 12:29:12 -07:00
|
|
|
if variable in SENSOR_TYPES and is_thermostat(device)]
|
2016-04-12 00:52:19 -04:00
|
|
|
sensors += [NestTempSensor(structure, device, variable)
|
2016-11-27 19:18:47 -05:00
|
|
|
for variable in conf
|
2016-05-15 12:29:12 -07:00
|
|
|
if variable in SENSOR_TEMP_TYPES and is_thermostat(device)]
|
2016-04-12 00:52:19 -04:00
|
|
|
sensors += [NestWeatherSensor(structure, device,
|
|
|
|
WEATHER_VARS[variable])
|
2016-11-27 19:18:47 -05:00
|
|
|
for variable in conf
|
2016-05-15 12:29:12 -07:00
|
|
|
if variable in WEATHER_VARS and is_thermostat(device)]
|
|
|
|
sensors += [NestProtectSensor(structure, device, variable)
|
2016-11-27 19:18:47 -05:00
|
|
|
for variable in conf
|
2016-05-15 12:29:12 -07:00
|
|
|
if variable in PROTECT_VARS and is_protect(device)]
|
2016-11-05 01:22:47 +01:00
|
|
|
all_sensors.extend(sensors)
|
2016-05-15 12:29:12 -07:00
|
|
|
|
2016-11-05 01:22:47 +01:00
|
|
|
add_devices(all_sensors, True)
|
2016-01-13 21:05:47 -07:00
|
|
|
|
2016-01-14 14:17:28 -07:00
|
|
|
|
2016-05-15 12:29:12 -07:00
|
|
|
def is_thermostat(device):
|
|
|
|
"""Target devices that are Nest Thermostats."""
|
|
|
|
return bool(device.__class__.__name__ == 'Device')
|
|
|
|
|
|
|
|
|
|
|
|
def is_protect(device):
|
|
|
|
"""Target devices that are Nest Protect Smoke Alarms."""
|
|
|
|
return bool(device.__class__.__name__ == 'ProtectDevice')
|
|
|
|
|
|
|
|
|
2016-01-13 21:05:47 -07:00
|
|
|
class NestSensor(Entity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a Nest sensor."""
|
2016-01-13 21:05:47 -07:00
|
|
|
|
|
|
|
def __init__(self, structure, device, variable):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2016-01-13 21:05:47 -07:00
|
|
|
self.structure = structure
|
|
|
|
self.device = device
|
|
|
|
self.variable = variable
|
|
|
|
|
2016-11-05 01:22:47 +01:00
|
|
|
# device specific
|
|
|
|
self._location = self.device.where
|
|
|
|
self._name = self.device.name
|
|
|
|
self._state = None
|
|
|
|
|
2016-01-13 21:05:47 -07:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the name of the nest, if any."""
|
2016-11-27 19:18:47 -05:00
|
|
|
return "{} {}".format(self._name, self.variable)
|
2016-01-14 14:19:35 -07:00
|
|
|
|
2016-01-16 12:52:22 -07:00
|
|
|
|
|
|
|
class NestBasicSensor(NestSensor):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation a basic Nest sensor."""
|
|
|
|
|
2016-01-13 21:05:47 -07:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 01:22:47 +01:00
|
|
|
return self._state
|
2016-01-13 21:05:47 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the unit the value is expressed in."""
|
2016-01-13 21:05:47 -07:00
|
|
|
return SENSOR_UNITS.get(self.variable, None)
|
|
|
|
|
2016-11-05 01:22:47 +01:00
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
|
|
|
if self.variable == 'operation_mode':
|
|
|
|
self._state = getattr(self.device, "mode")
|
|
|
|
else:
|
|
|
|
self._state = getattr(self.device, self.variable)
|
|
|
|
|
2016-01-14 14:19:35 -07:00
|
|
|
|
2016-01-16 12:53:42 -07:00
|
|
|
class NestTempSensor(NestSensor):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a Nest Temperature sensor."""
|
|
|
|
|
2016-01-13 21:05:47 -07:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the unit the value is expressed in."""
|
2016-11-27 19:18:47 -05:00
|
|
|
if self.device.temperature_scale == 'C':
|
|
|
|
return TEMP_CELSIUS
|
|
|
|
else:
|
|
|
|
return TEMP_FAHRENHEIT
|
2016-01-13 21:05:47 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 01:22:47 +01:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-01-13 21:05:47 -07:00
|
|
|
temp = getattr(self.device, self.variable)
|
|
|
|
if temp is None:
|
2016-11-05 01:22:47 +01:00
|
|
|
self._state = None
|
2016-01-13 21:05:47 -07:00
|
|
|
|
2016-10-17 22:55:53 -04:00
|
|
|
if isinstance(temp, tuple):
|
|
|
|
low, high = temp
|
2016-11-05 01:22:47 +01:00
|
|
|
self._state = "%s-%s" % (int(low), int(high))
|
2016-10-17 22:55:53 -04:00
|
|
|
else:
|
2016-11-05 01:22:47 +01:00
|
|
|
self._state = round(temp, 1)
|
2016-02-18 15:39:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
class NestWeatherSensor(NestSensor):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation a basic Nest Weather Conditions sensor."""
|
|
|
|
|
2016-02-18 15:39:05 -05:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 01:22:47 +01:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-02-18 15:39:05 -05:00
|
|
|
if self.variable == 'kph' or self.variable == 'direction':
|
2016-11-05 01:22:47 +01:00
|
|
|
self._state = getattr(self.structure.weather.current.wind,
|
|
|
|
self.variable)
|
2016-02-18 15:39:05 -05:00
|
|
|
else:
|
2016-11-05 01:22:47 +01:00
|
|
|
self._state = getattr(self.structure.weather.current,
|
|
|
|
self.variable)
|
2016-02-18 15:39:05 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the unit the value is expressed in."""
|
2016-02-18 15:39:05 -05:00
|
|
|
return SENSOR_UNITS.get(self.variable, None)
|
2016-05-15 12:29:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
class NestProtectSensor(NestSensor):
|
|
|
|
"""Return the state of nest protect."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 01:22:47 +01:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-11-27 19:18:47 -05:00
|
|
|
self._state = getattr(self.device, self.variable).capitalize()
|