2016-01-14 04:05:47 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.nest
|
2016-01-27 08:34:14 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
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-01-14 18:37:17 +00:00
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
import homeassistant.components.nest as nest
|
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-01-14 17:48:24 +00:00
|
|
|
from homeassistant.const import TEMP_CELCIUS
|
2016-01-14 04:05:47 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['nest']
|
|
|
|
SENSOR_TYPES = ['humidity',
|
|
|
|
'mode',
|
|
|
|
'last_ip',
|
|
|
|
'local_ip',
|
|
|
|
'last_connection',
|
|
|
|
'battery_level']
|
|
|
|
|
2016-02-11 05:12:43 +00:00
|
|
|
SENSOR_UNITS = {'humidity': '%', 'battery_level': 'V'}
|
2016-01-14 04:05:47 +00:00
|
|
|
|
|
|
|
SENSOR_TEMP_TYPES = ['temperature',
|
|
|
|
'target',
|
|
|
|
'away_temperature[0]',
|
|
|
|
'away_temperature[1]']
|
|
|
|
|
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-01-27 08:34:14 +00:00
|
|
|
""" Setup Nest Sensor. """
|
2016-01-14 21:17:28 +00:00
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
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:
|
2016-01-16 19:56:38 +00:00
|
|
|
add_devices([NestBasicSensor(structure,
|
|
|
|
device,
|
|
|
|
variable)])
|
2016-01-14 04:05:47 +00:00
|
|
|
elif variable in SENSOR_TEMP_TYPES:
|
2016-01-14 21:17:28 +00:00
|
|
|
add_devices([NestTempSensor(structure,
|
|
|
|
device,
|
|
|
|
variable)])
|
2016-01-14 04:05:47 +00:00
|
|
|
else:
|
2016-01-14 21:17:28 +00:00
|
|
|
logger.error('Nest sensor type: "%s" does not exist',
|
|
|
|
variable)
|
2016-01-14 04:05:47 +00:00
|
|
|
except socket.error:
|
|
|
|
logger.error(
|
|
|
|
"Connection error logging into the nest web service."
|
|
|
|
)
|
|
|
|
|
2016-01-14 21:17:28 +00:00
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
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. """
|
2016-01-14 21:17:28 +00:00
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
location = self.device.where
|
|
|
|
name = self.device.name
|
|
|
|
if location is None:
|
2016-01-14 21:17:28 +00:00
|
|
|
return "{} {}".format(name, self.variable)
|
2016-01-14 04:05:47 +00:00
|
|
|
else:
|
|
|
|
if name == '':
|
2016-01-14 21:17:28 +00:00
|
|
|
return "{} {}".format(location.capitalize(), self.variable)
|
2016-01-14 04:05:47 +00:00
|
|
|
else:
|
2016-01-14 21:17:28 +00:00
|
|
|
return "{}({}){}".format(location.capitalize(),
|
|
|
|
name,
|
|
|
|
self.variable)
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2016-01-16 19:52:22 +00:00
|
|
|
|
|
|
|
class NestBasicSensor(NestSensor):
|
2016-01-16 19:56:38 +00:00
|
|
|
""" Represents a basic Nest sensor with state. """
|
|
|
|
|
2016-01-14 04:05:47 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
""" Returns the state of the sensor. """
|
|
|
|
return getattr(self.device, self.variable)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-01-27 08:34:14 +00:00
|
|
|
""" Unit the value is expressed in. """
|
2016-01-14 04:05:47 +00:00
|
|
|
return SENSOR_UNITS.get(self.variable, None)
|
|
|
|
|
2016-01-14 21:19:35 +00:00
|
|
|
|
2016-01-16 19:53:42 +00:00
|
|
|
class NestTempSensor(NestSensor):
|
2016-01-14 04:05:47 +00:00
|
|
|
""" Represents a Nest Temperature sensor. """
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-01-27 08:34:14 +00:00
|
|
|
""" Unit the value is expressed in. """
|
2016-01-14 21:19:35 +00:00
|
|
|
return TEMP_CELCIUS
|
2016-01-14 04:05:47 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-01-27 08:34:14 +00:00
|
|
|
""" Returns the state of the sensor. """
|
2016-01-14 04:05:47 +00:00
|
|
|
temp = getattr(self.device, self.variable)
|
|
|
|
if temp is None:
|
|
|
|
return None
|
|
|
|
|
2016-01-14 21:19:35 +00:00
|
|
|
return round(temp, 1)
|