From 9d848731d903a8ee2296bbfc6746f4eb5143a446 Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Tue, 12 Apr 2016 00:52:19 -0400 Subject: [PATCH] Nest config validation (#1810) * Config validation for Nest platforms. --- .../components/binary_sensor/nest.py | 32 ++++----- homeassistant/components/nest.py | 20 +++++- homeassistant/components/sensor/nest.py | 68 +++++++++---------- homeassistant/components/thermostat/nest.py | 25 +++---- 4 files changed, 73 insertions(+), 72 deletions(-) diff --git a/homeassistant/components/binary_sensor/nest.py b/homeassistant/components/binary_sensor/nest.py index 79530bad52f..9f963b730b5 100644 --- a/homeassistant/components/binary_sensor/nest.py +++ b/homeassistant/components/binary_sensor/nest.py @@ -4,12 +4,14 @@ Support for Nest Thermostat Binary Sensors. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/binary_sensor.nest/ """ -import logging -import socket +import voluptuous as vol import homeassistant.components.nest as nest from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.sensor.nest import NestSensor +from homeassistant.const import ( + CONF_PLATFORM, CONF_SCAN_INTERVAL, CONF_MONITORED_CONDITIONS +) DEPENDENCIES = ['nest'] BINARY_TYPES = ['fan', @@ -23,25 +25,19 @@ BINARY_TYPES = ['fan', 'hvac_emer_heat_state', 'online'] +PLATFORM_SCHEMA = vol.Schema({ + vol.Required(CONF_PLATFORM): nest.DOMAIN, + vol.Optional(CONF_SCAN_INTERVAL): + vol.All(vol.Coerce(int), vol.Range(min=1)), + vol.Required(CONF_MONITORED_CONDITIONS): [vol.In(BINARY_TYPES)], +}) + def setup_platform(hass, config, add_devices, discovery_info=None): """Setup Nest binary sensors.""" - 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 BINARY_TYPES: - add_devices([NestBinarySensor(structure, - device, - variable)]) - else: - logger.error('Nest sensor type: "%s" does not exist', - variable) - except socket.error: - logger.error( - "Connection error logging into the nest web service." - ) + for structure, device in nest.devices(): + add_devices([NestBinarySensor(structure, device, variable) + for variable in config[CONF_MONITORED_CONDITIONS]]) class NestBinarySensor(NestSensor, BinarySensorDevice): diff --git a/homeassistant/components/nest.py b/homeassistant/components/nest.py index a55fa6e2af3..7b866919b73 100644 --- a/homeassistant/components/nest.py +++ b/homeassistant/components/nest.py @@ -4,6 +4,9 @@ Support for Nest thermostats. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/thermostat.nest/ """ +import logging +import socket + import voluptuous as vol from homeassistant.const import CONF_PASSWORD, CONF_USERNAME @@ -20,14 +23,27 @@ CONFIG_SCHEMA = vol.Schema({ }) }, extra=vol.ALLOW_EXTRA) +_LOGGER = logging.getLogger(__name__) + + +def devices(): + """Generator returning list of devices and their location.""" + try: + for structure in NEST.structures: + for device in structure.devices: + yield (structure, device) + except socket.error: + _LOGGER.error("Connection error logging into the nest web service.") + # pylint: disable=unused-argument def setup(hass, config): """Setup the Nest thermostat component.""" global NEST - username = config[DOMAIN].get(CONF_USERNAME) - password = config[DOMAIN].get(CONF_PASSWORD) + conf = config[DOMAIN] + username = conf[CONF_USERNAME] + password = conf[CONF_PASSWORD] import nest diff --git a/homeassistant/components/sensor/nest.py b/homeassistant/components/sensor/nest.py index 1466839202b..a59d72c1fb4 100644 --- a/homeassistant/components/sensor/nest.py +++ b/homeassistant/components/sensor/nest.py @@ -4,12 +4,13 @@ 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 -import socket +import voluptuous as vol import homeassistant.components.nest as nest -from homeassistant.const import TEMP_CELCIUS from homeassistant.helpers.entity import Entity +from homeassistant.const import ( + TEMP_CELCIUS, CONF_PLATFORM, CONF_SCAN_INTERVAL, CONF_MONITORED_CONDITIONS +) DEPENDENCIES = ['nest'] SENSOR_TYPES = ['humidity', @@ -19,49 +20,42 @@ SENSOR_TYPES = ['humidity', 'last_connection', 'battery_level'] -WEATHER_VARIABLES = ['weather_condition', 'weather_temperature', - 'weather_humidity', - 'wind_speed', 'wind_direction'] - -JSON_VARIABLE_NAMES = {'weather_humidity': 'humidity', - 'weather_temperature': 'temperature', - 'weather_condition': 'condition', - 'wind_speed': 'kph', - 'wind_direction': 'direction'} +WEATHER_VARS = {'weather_humidity': 'humidity', + 'weather_temperature': 'temperature', + 'weather_condition': 'condition', + 'wind_speed': 'kph', + 'wind_direction': 'direction'} SENSOR_UNITS = {'humidity': '%', 'battery_level': 'V', 'kph': 'kph', 'temperature': '°C'} SENSOR_TEMP_TYPES = ['temperature', 'target'] +_VALID_SENSOR_TYPES = SENSOR_TYPES + SENSOR_TEMP_TYPES + \ + list(WEATHER_VARS.keys()) + +PLATFORM_SCHEMA = vol.Schema({ + vol.Required(CONF_PLATFORM): nest.DOMAIN, + vol.Optional(CONF_SCAN_INTERVAL): + vol.All(vol.Coerce(int), vol.Range(min=1)), + vol.Required(CONF_MONITORED_CONDITIONS): [vol.In(_VALID_SENSOR_TYPES)], +}) + def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Nest Sensor.""" - 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: - add_devices([NestBasicSensor(structure, - device, - variable)]) - elif variable in SENSOR_TEMP_TYPES: - add_devices([NestTempSensor(structure, - device, - variable)]) - elif variable in WEATHER_VARIABLES: - json_variable = JSON_VARIABLE_NAMES.get(variable, None) - add_devices([NestWeatherSensor(structure, - device, - json_variable)]) - else: - logger.error('Nest sensor type: "%s" does not exist', - variable) - except socket.error: - logger.error( - "Connection error logging into the nest web service." - ) + for structure, device in nest.devices(): + sensors = [NestBasicSensor(structure, device, variable) + for variable in config[CONF_MONITORED_CONDITIONS] + if variable in SENSOR_TYPES] + sensors += [NestTempSensor(structure, device, variable) + for variable in config[CONF_MONITORED_CONDITIONS] + if variable in SENSOR_TEMP_TYPES] + sensors += [NestWeatherSensor(structure, device, + WEATHER_VARS[variable]) + for variable in config[CONF_MONITORED_CONDITIONS] + if variable in WEATHER_VARS] + add_devices(sensors) class NestSensor(Entity): diff --git a/homeassistant/components/thermostat/nest.py b/homeassistant/components/thermostat/nest.py index 1891fa79c70..9a52f58c1d9 100644 --- a/homeassistant/components/thermostat/nest.py +++ b/homeassistant/components/thermostat/nest.py @@ -4,31 +4,26 @@ Support for Nest thermostats. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/thermostat.nest/ """ -import logging -import socket +import voluptuous as vol import homeassistant.components.nest as nest from homeassistant.components.thermostat import ( STATE_COOL, STATE_HEAT, STATE_IDLE, ThermostatDevice) -from homeassistant.const import TEMP_CELCIUS +from homeassistant.const import TEMP_CELCIUS, CONF_PLATFORM, CONF_SCAN_INTERVAL DEPENDENCIES = ['nest'] +PLATFORM_SCHEMA = vol.Schema({ + vol.Required(CONF_PLATFORM): nest.DOMAIN, + vol.Optional(CONF_SCAN_INTERVAL): + vol.All(vol.Coerce(int), vol.Range(min=1)), +}) + def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Nest thermostat.""" - logger = logging.getLogger(__name__) - - try: - add_devices([ - NestThermostat(structure, device) - for structure in nest.NEST.structures - for device in structure.devices - ]) - except socket.error: - logger.error( - "Connection error logging into the nest web service." - ) + add_devices([NestThermostat(structure, device) + for structure, device in nest.devices()]) class NestThermostat(ThermostatDevice):