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-12-03 17:26:47 +00:00
|
|
|
import logging
|
2016-05-15 19:29:12 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
from homeassistant.components.nest import DATA_NEST, NestSensorDevice
|
2018-05-05 13:37:40 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
TEMP_CELSIUS, TEMP_FAHRENHEIT, CONF_MONITORED_CONDITIONS,
|
2018-06-03 01:54:48 +00:00
|
|
|
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_HUMIDITY)
|
2016-01-14 04:05:47 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['nest']
|
2018-06-01 14:44:58 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
SENSOR_TYPES = ['humidity', 'operation_mode', 'hvac_state']
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
TEMP_SENSOR_TYPES = ['temperature', 'target']
|
2016-11-28 00:18:47 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
PROTECT_SENSOR_TYPES = ['co_status', 'smoke_status', 'battery_health']
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
STRUCTURE_SENSOR_TYPES = ['eta']
|
2016-11-28 00:18:47 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
_VALID_SENSOR_TYPES = SENSOR_TYPES + TEMP_SENSOR_TYPES + PROTECT_SENSOR_TYPES \
|
|
|
|
+ STRUCTURE_SENSOR_TYPES
|
2016-05-15 19:29:12 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
SENSOR_UNITS = {'humidity': '%'}
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
SENSOR_DEVICE_CLASSES = {'humidity': DEVICE_CLASS_HUMIDITY}
|
2018-05-23 19:40:33 +00:00
|
|
|
|
|
|
|
VARIABLE_NAME_MAPPING = {'eta': 'eta_begin', 'operation_mode': 'mode'}
|
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
SENSOR_TYPES_DEPRECATED = ['last_ip',
|
|
|
|
'local_ip',
|
|
|
|
'last_connection',
|
|
|
|
'battery_level']
|
|
|
|
|
|
|
|
DEPRECATED_WEATHER_VARS = ['weather_humidity',
|
|
|
|
'weather_temperature',
|
|
|
|
'weather_condition',
|
|
|
|
'wind_speed',
|
|
|
|
'wind_direction']
|
2016-12-03 17:26:47 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
_SENSOR_TYPES_DEPRECATED = SENSOR_TYPES_DEPRECATED + DEPRECATED_WEATHER_VARS
|
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):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up 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]
|
|
|
|
|
2017-01-17 08:12:15 +00:00
|
|
|
# Add all available sensors if no Nest sensor config is set
|
|
|
|
if discovery_info == {}:
|
|
|
|
conditions = _VALID_SENSOR_TYPES
|
|
|
|
else:
|
|
|
|
conditions = discovery_info.get(CONF_MONITORED_CONDITIONS, {})
|
|
|
|
|
|
|
|
for variable in conditions:
|
2016-12-03 17:26:47 +00:00
|
|
|
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/"
|
2017-01-17 08:12:15 +00:00
|
|
|
"binary_sensor.nest/ for valid options.")
|
2016-12-03 17:26:47 +00:00
|
|
|
_LOGGER.error(wstr)
|
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
all_sensors = []
|
2018-05-23 19:40:33 +00:00
|
|
|
for structure in nest.structures():
|
|
|
|
all_sensors += [NestBasicSensor(structure, None, variable)
|
|
|
|
for variable in conditions
|
|
|
|
if variable in STRUCTURE_SENSOR_TYPES]
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
for structure, device in nest.thermostats():
|
|
|
|
all_sensors += [NestBasicSensor(structure, device, variable)
|
|
|
|
for variable in conditions
|
|
|
|
if variable in SENSOR_TYPES]
|
|
|
|
all_sensors += [NestTempSensor(structure, device, variable)
|
|
|
|
for variable in conditions
|
|
|
|
if variable in TEMP_SENSOR_TYPES]
|
2018-06-01 14:44:58 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
for structure, device in nest.smoke_co_alarms():
|
|
|
|
all_sensors += [NestBasicSensor(structure, device, variable)
|
|
|
|
for variable in conditions
|
|
|
|
if variable in PROTECT_SENSOR_TYPES]
|
2018-06-01 14:44:58 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
add_devices(all_sensors, True)
|
2018-06-01 14:44:58 +00:00
|
|
|
|
2016-01-16 19:52:22 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
class NestBasicSensor(NestSensorDevice):
|
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
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return SENSOR_DEVICE_CLASSES.get(self.variable)
|
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
def update(self):
|
|
|
|
"""Retrieve latest state."""
|
2018-06-03 01:54:48 +00:00
|
|
|
self._unit = SENSOR_UNITS.get(self.variable)
|
2016-12-12 01:34:26 +00:00
|
|
|
|
2018-05-23 19:40:33 +00:00
|
|
|
if self.variable in VARIABLE_NAME_MAPPING:
|
|
|
|
self._state = getattr(self.device,
|
|
|
|
VARIABLE_NAME_MAPPING[self.variable])
|
2018-06-03 01:54:48 +00:00
|
|
|
elif self.variable in PROTECT_SENSOR_TYPES:
|
|
|
|
# keep backward compatibility
|
|
|
|
self._state = getattr(self.device, self.variable).capitalize()
|
2016-11-05 00:22:47 +00:00
|
|
|
else:
|
|
|
|
self._state = getattr(self.device, self.variable)
|
|
|
|
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2018-06-03 01:54:48 +00:00
|
|
|
class NestTempSensor(NestSensorDevice):
|
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
|
|
|
|
|
2018-04-20 13:38:27 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
2018-05-05 13:37:40 +00:00
|
|
|
return DEVICE_CLASS_TEMPERATURE
|
2018-04-20 13:38:27 +00:00
|
|
|
|
2016-11-05 00:22:47 +00:00
|
|
|
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)
|