2016-01-14 04:05:47 +00: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 19:29:12 +00:00
|
|
|
from itertools import chain
|
2016-12-03 17:26:47 +00:00
|
|
|
import logging
|
2016-05-15 19:29:12 +00:00
|
|
|
|
2016-04-12 04:52:19 +00:00
|
|
|
import voluptuous as vol
|
2016-01-14 18:37:17 +00:00
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
from homeassistant.components.nest import DATA_NEST, DOMAIN
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-04-12 04:52:19 +00:00
|
|
|
from homeassistant.const import (
|
2016-11-28 00:18:47 +00:00
|
|
|
TEMP_CELSIUS, TEMP_FAHRENHEIT, CONF_PLATFORM,
|
|
|
|
CONF_SCAN_INTERVAL, CONF_MONITORED_CONDITIONS
|
2016-04-12 04:52:19 +00:00
|
|
|
)
|
2016-01-14 04:05:47 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['nest']
|
|
|
|
SENSOR_TYPES = ['humidity',
|
2016-12-16 05:39:59 +00:00
|
|
|
'operation_mode',
|
|
|
|
'hvac_state']
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2016-12-03 17:26:47 +00:00
|
|
|
SENSOR_TYPES_DEPRECATED = ['last_ip',
|
|
|
|
'local_ip',
|
|
|
|
'last_connection']
|
|
|
|
|
|
|
|
SENSOR_TYPES_DEPRECATED = ['last_ip',
|
2016-11-28 00:18:47 +00:00
|
|
|
'local_ip']
|
2016-02-18 20:39:05 +00:00
|
|
|
|
2016-11-28 00:18:47 +00: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-14 04:05:47 +00:00
|
|
|
|
2016-05-15 19:29:12 +00:00
|
|
|
PROTECT_VARS = ['co_status',
|
|
|
|
'smoke_status',
|
2016-11-28 00:18:47 +00:00
|
|
|
'battery_health']
|
|
|
|
|
|
|
|
PROTECT_VARS_DEPRECATED = ['battery_level']
|
2016-05-15 19:29:12 +00:00
|
|
|
|
2016-03-22 15:45:44 +00:00
|
|
|
SENSOR_TEMP_TYPES = ['temperature', 'target']
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2016-12-03 17:26:47 +00:00
|
|
|
_SENSOR_TYPES_DEPRECATED = SENSOR_TYPES_DEPRECATED \
|
|
|
|
+ list(DEPRECATED_WEATHER_VARS.keys()) + PROTECT_VARS_DEPRECATED
|
|
|
|
|
|
|
|
_VALID_SENSOR_TYPES = SENSOR_TYPES + SENSOR_TEMP_TYPES + PROTECT_VARS \
|
|
|
|
+ list(WEATHER_VARS.keys())
|
|
|
|
|
|
|
|
_VALID_SENSOR_TYPES_WITH_DEPRECATED = _VALID_SENSOR_TYPES \
|
|
|
|
+ _SENSOR_TYPES_DEPRECATED
|
2016-04-12 04:52:19 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
2016-11-05 00:22:47 +00:00
|
|
|
vol.Required(CONF_PLATFORM): DOMAIN,
|
2016-04-12 04:52:19 +00:00
|
|
|
vol.Optional(CONF_SCAN_INTERVAL):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(min=1)),
|
2016-12-03 17:26:47 +00:00
|
|
|
vol.Required(CONF_MONITORED_CONDITIONS):
|
|
|
|
[vol.In(_VALID_SENSOR_TYPES_WITH_DEPRECATED)]
|
2016-04-12 04:52:19 +00:00
|
|
|
})
|
|
|
|
|
2016-12-03 17:26:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Setup the Nest Sensor."""
|
2016-12-04 22:33:50 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
nest = hass.data[DATA_NEST]
|
2016-11-28 00:18:47 +00:00
|
|
|
conf = config.get(CONF_MONITORED_CONDITIONS, _VALID_SENSOR_TYPES)
|
2016-11-05 00:22:47 +00:00
|
|
|
|
2016-12-03 17:26:47 +00:00
|
|
|
for variable in conf:
|
|
|
|
if variable in _SENSOR_TYPES_DEPRECATED:
|
|
|
|
if variable in DEPRECATED_WEATHER_VARS:
|
|
|
|
wstr = ("Nest no longer provides weather data like %s. See "
|
|
|
|
"https://home-assistant.io/components/#weather "
|
|
|
|
"for a list of other weather components to use." %
|
|
|
|
variable)
|
|
|
|
else:
|
|
|
|
wstr = (variable + " is no a longer supported "
|
|
|
|
"monitored_conditions. See "
|
|
|
|
"https://home-assistant.io/components/"
|
|
|
|
"binary_sensor.nest/ "
|
|
|
|
"for valid options, or remove monitored_conditions "
|
|
|
|
"entirely to get a reasonable default")
|
|
|
|
|
|
|
|
_LOGGER.error(wstr)
|
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
all_sensors = []
|
2016-05-15 19:29:12 +00:00
|
|
|
for structure, device in chain(nest.devices(), nest.protect_devices()):
|
2016-04-12 04:52:19 +00:00
|
|
|
sensors = [NestBasicSensor(structure, device, variable)
|
2016-11-28 00:18:47 +00:00
|
|
|
for variable in conf
|
2016-05-15 19:29:12 +00:00
|
|
|
if variable in SENSOR_TYPES and is_thermostat(device)]
|
2016-04-12 04:52:19 +00:00
|
|
|
sensors += [NestTempSensor(structure, device, variable)
|
2016-11-28 00:18:47 +00:00
|
|
|
for variable in conf
|
2016-05-15 19:29:12 +00:00
|
|
|
if variable in SENSOR_TEMP_TYPES and is_thermostat(device)]
|
|
|
|
sensors += [NestProtectSensor(structure, device, variable)
|
2016-11-28 00:18:47 +00:00
|
|
|
for variable in conf
|
2016-05-15 19:29:12 +00:00
|
|
|
if variable in PROTECT_VARS and is_protect(device)]
|
2016-11-05 00:22:47 +00:00
|
|
|
all_sensors.extend(sensors)
|
2016-05-15 19:29:12 +00:00
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
add_devices(all_sensors, True)
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2016-01-14 21:17:28 +00:00
|
|
|
|
2016-05-15 19:29:12 +00: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-14 04:05:47 +00:00
|
|
|
class NestSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Nest sensor."""
|
2016-01-14 04:05:47 +00:00
|
|
|
|
|
|
|
def __init__(self, structure, device, variable):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-01-14 04:05:47 +00:00
|
|
|
self.structure = structure
|
|
|
|
self.device = device
|
|
|
|
self.variable = variable
|
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
# device specific
|
|
|
|
self._location = self.device.where
|
2016-12-12 01:34:26 +00:00
|
|
|
self._name = "{} {}".format(self.device.name_long,
|
|
|
|
self.variable.replace("_", " "))
|
2016-11-05 00:22:47 +00:00
|
|
|
self._state = None
|
2016-12-12 01:34:26 +00:00
|
|
|
self._unit = None
|
2016-11-05 00:22:47 +00:00
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the nest, if any."""
|
2016-12-12 01:34:26 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
return self._unit
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2016-01-16 19:52:22 +00:00
|
|
|
|
|
|
|
class NestBasicSensor(NestSensor):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation a basic Nest sensor."""
|
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 00:22:47 +00:00
|
|
|
return self._state
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-12-12 01:34:26 +00:00
|
|
|
self._unit = SENSOR_UNITS.get(self.variable, None)
|
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
if self.variable == 'operation_mode':
|
|
|
|
self._state = getattr(self.device, "mode")
|
|
|
|
else:
|
|
|
|
self._state = getattr(self.device, self.variable)
|
|
|
|
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2016-01-16 19:53:42 +00:00
|
|
|
class NestTempSensor(NestSensor):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Nest Temperature sensor."""
|
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 00:22:47 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-12-12 01:34:26 +00:00
|
|
|
if self.device.temperature_scale == 'C':
|
|
|
|
self._unit = TEMP_CELSIUS
|
|
|
|
else:
|
|
|
|
self._unit = TEMP_FAHRENHEIT
|
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
temp = getattr(self.device, self.variable)
|
|
|
|
if temp is None:
|
2016-11-05 00:22:47 +00:00
|
|
|
self._state = None
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2016-10-18 02:55:53 +00:00
|
|
|
if isinstance(temp, tuple):
|
|
|
|
low, high = temp
|
2016-11-05 00:22:47 +00:00
|
|
|
self._state = "%s-%s" % (int(low), int(high))
|
2016-10-18 02:55:53 +00:00
|
|
|
else:
|
2016-11-05 00:22:47 +00:00
|
|
|
self._state = round(temp, 1)
|
2016-02-18 20:39:05 +00:00
|
|
|
|
|
|
|
|
2016-05-15 19:29:12 +00:00
|
|
|
class NestProtectSensor(NestSensor):
|
|
|
|
"""Return the state of nest protect."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2016-11-05 00:22:47 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2016-11-28 00:18:47 +00:00
|
|
|
self._state = getattr(self.device, self.variable).capitalize()
|