core/homeassistant/components/sensor/nest.py

154 lines
5.0 KiB
Python
Raw Normal View History

"""
Support for Nest Thermostat Sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.nest/
"""
import logging
from homeassistant.components.nest import DATA_NEST, NestSensorDevice
from homeassistant.const import (
TEMP_CELSIUS, TEMP_FAHRENHEIT, CONF_MONITORED_CONDITIONS,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_HUMIDITY)
DEPENDENCIES = ['nest']
SENSOR_TYPES = ['humidity', 'operation_mode', 'hvac_state']
TEMP_SENSOR_TYPES = ['temperature', 'target']
Nest Cam support (#4292) * start nestcam support * start nestcam support * introduce a access_token_cache_file * Bare minimum to get nest thermostat loading * occaisonally the image works * switch to nest-aware interval for testing * Add Nest Aware awareness * remove duplicate error logging line * Fix nest protect support * address baloobot * fix copy pasta * fix more baloobot * last baloobot thing for now? * Use streaming status to determine online or not. online from nest means its on the network * Fix temperature scale for climate * Add support for eco mode * Fix auto mode for nest climate * update update current_operation and set_operation mode to use constant when possible. try to get setting something working * remove stale comment * unused-argument already disabled globally * Add eco to the end, instead of after off * Simplify conditional when the hass mode is the same as the nest one * away_temperature became eco_temperature, and works with eco mode * Update min/max temp based on locked temperature * Forgot to set locked stuff during construction * Cache image instead of throttling (which returns none), respect NestAware subscription * Fix _time_between_snapshots before the first update * WIP pin authorization * Add some more logging * Working configurator, woo. Fix some hound errors * Updated pin workflow * Deprecate more sensors * Don't update during access of name * Don't update during access of name * Add camera brand * Fix up some syntastic errors * Fix ups ome hound errors * Maybe fix some more? * Move snapshot simulator url checking down into python-nest * Rename _ready_to_update_camera_image to _ready_for_snapshot * More fixes * Set the next time a snapshot can be taken when one is taken to simplify logic * Add a FIXME about update not getting called * Call update during constructor, so values get set at least once * Fix up names * Remove todo about eco, since that's pretty nest * thanks hound * Fix temperature being off for farenheight. * Fix some lint errors, which includes using a git version of python-nest with updated code * generate requirements_all.py * fix pylint * Update nestcam before adding * Fix polling of NestCamera * Lint
2016-11-28 00:18:47 +00:00
PROTECT_SENSOR_TYPES = ['co_status', 'smoke_status', 'battery_health']
STRUCTURE_SENSOR_TYPES = ['eta']
Nest Cam support (#4292) * start nestcam support * start nestcam support * introduce a access_token_cache_file * Bare minimum to get nest thermostat loading * occaisonally the image works * switch to nest-aware interval for testing * Add Nest Aware awareness * remove duplicate error logging line * Fix nest protect support * address baloobot * fix copy pasta * fix more baloobot * last baloobot thing for now? * Use streaming status to determine online or not. online from nest means its on the network * Fix temperature scale for climate * Add support for eco mode * Fix auto mode for nest climate * update update current_operation and set_operation mode to use constant when possible. try to get setting something working * remove stale comment * unused-argument already disabled globally * Add eco to the end, instead of after off * Simplify conditional when the hass mode is the same as the nest one * away_temperature became eco_temperature, and works with eco mode * Update min/max temp based on locked temperature * Forgot to set locked stuff during construction * Cache image instead of throttling (which returns none), respect NestAware subscription * Fix _time_between_snapshots before the first update * WIP pin authorization * Add some more logging * Working configurator, woo. Fix some hound errors * Updated pin workflow * Deprecate more sensors * Don't update during access of name * Don't update during access of name * Add camera brand * Fix up some syntastic errors * Fix ups ome hound errors * Maybe fix some more? * Move snapshot simulator url checking down into python-nest * Rename _ready_to_update_camera_image to _ready_for_snapshot * More fixes * Set the next time a snapshot can be taken when one is taken to simplify logic * Add a FIXME about update not getting called * Call update during constructor, so values get set at least once * Fix up names * Remove todo about eco, since that's pretty nest * thanks hound * Fix temperature being off for farenheight. * Fix some lint errors, which includes using a git version of python-nest with updated code * generate requirements_all.py * fix pylint * Update nestcam before adding * Fix polling of NestCamera * Lint
2016-11-28 00:18:47 +00:00
_VALID_SENSOR_TYPES = SENSOR_TYPES + TEMP_SENSOR_TYPES + PROTECT_SENSOR_TYPES \
+ STRUCTURE_SENSOR_TYPES
SENSOR_UNITS = {'humidity': '%'}
SENSOR_DEVICE_CLASSES = {'humidity': DEVICE_CLASS_HUMIDITY}
VARIABLE_NAME_MAPPING = {'eta': 'eta_begin', 'operation_mode': 'mode'}
SENSOR_TYPES_DEPRECATED = ['last_ip',
'local_ip',
'last_connection',
'battery_level']
DEPRECATED_WEATHER_VARS = ['weather_humidity',
'weather_temperature',
'weather_condition',
'wind_speed',
'wind_direction']
_SENSOR_TYPES_DEPRECATED = SENSOR_TYPES_DEPRECATED + DEPRECATED_WEATHER_VARS
_LOGGER = logging.getLogger(__name__)
2016-01-14 21:19:35 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Nest Sensor."""
if discovery_info is None:
return
nest = hass.data[DATA_NEST]
# 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:
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.")
_LOGGER.error(wstr)
all_sensors = []
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
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]
for structure, device in nest.smoke_co_alarms():
all_sensors += [NestBasicSensor(structure, device, variable)
for variable in conditions
if variable in PROTECT_SENSOR_TYPES]
add_devices(all_sensors, True)
2016-01-16 19:52:22 +00:00
class NestBasicSensor(NestSensorDevice):
2016-03-08 15:46:34 +00:00
"""Representation a basic Nest sensor."""
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
return self._state
@property
def device_class(self):
"""Return the device class of the sensor."""
return SENSOR_DEVICE_CLASSES.get(self.variable)
def update(self):
"""Retrieve latest state."""
self._unit = SENSOR_UNITS.get(self.variable)
if self.variable in VARIABLE_NAME_MAPPING:
self._state = getattr(self.device,
VARIABLE_NAME_MAPPING[self.variable])
elif self.variable in PROTECT_SENSOR_TYPES:
# keep backward compatibility
self._state = getattr(self.device, self.variable).capitalize()
else:
self._state = getattr(self.device, self.variable)
2016-01-14 21:19:35 +00:00
class NestTempSensor(NestSensorDevice):
2016-03-08 15:46:34 +00:00
"""Representation of a Nest Temperature sensor."""
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
return self._state
2018-04-20 13:38:27 +00:00
@property
def device_class(self):
"""Return the device class of the sensor."""
return DEVICE_CLASS_TEMPERATURE
2018-04-20 13:38:27 +00:00
def update(self):
"""Retrieve latest state."""
if self.device.temperature_scale == 'C':
self._unit = TEMP_CELSIUS
else:
self._unit = TEMP_FAHRENHEIT
temp = getattr(self.device, self.variable)
if temp is None:
self._state = None
if isinstance(temp, tuple):
low, high = temp
self._state = "%s-%s" % (int(low), int(high))
else:
self._state = round(temp, 1)