Use voluptuous for REST platforms (#2887)

* Initial step to migrate to voluptuous

* Migrate to voluptuous

* Add schema for sensor_classes
This commit is contained in:
Fabian Affolter 2016-08-21 01:28:45 +02:00 committed by Paulus Schoutsen
parent b62c3ac56c
commit 5f508b6afa
6 changed files with 122 additions and 76 deletions

View File

@ -6,6 +6,8 @@ https://home-assistant.io/components/binary_sensor/
""" """
import logging import logging
import voluptuous as vol
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.const import (STATE_ON, STATE_OFF) from homeassistant.const import (STATE_ON, STATE_OFF)
@ -33,6 +35,8 @@ SENSOR_CLASSES = [
'vibration', # On means vibration detected, Off means no vibration 'vibration', # On means vibration detected, Off means no vibration
] ]
SENSOR_CLASSES_SCHEMA = vol.All(vol.Lower, vol.In(SENSOR_CLASSES))
def setup(hass, config): def setup(hass, config):
"""Track states and offer events for binary sensors.""" """Track states and offer events for binary sensors."""

View File

@ -6,30 +6,42 @@ https://home-assistant.io/components/binary_sensor.rest/
""" """
import logging import logging
from homeassistant.components.binary_sensor import (BinarySensorDevice, import voluptuous as vol
SENSOR_CLASSES)
from homeassistant.components.binary_sensor import (
BinarySensorDevice, SENSOR_CLASSES_SCHEMA, PLATFORM_SCHEMA)
from homeassistant.components.sensor.rest import RestData from homeassistant.components.sensor.rest import RestData
from homeassistant.const import CONF_VALUE_TEMPLATE from homeassistant.const import (
CONF_PAYLOAD, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_METHOD, CONF_RESOURCE,
CONF_SENSOR_CLASS)
from homeassistant.helpers import template from homeassistant.helpers import template
import homeassistant.helpers.config_validation as cv
DEFAULT_METHOD = 'GET'
DEFAULT_NAME = 'REST Binary Sensor'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): vol.In(['POST', 'GET']),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PAYLOAD): cv.string,
vol.Optional(CONF_SENSOR_CLASS): SENSOR_CLASSES_SCHEMA,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'REST Binary Sensor'
DEFAULT_METHOD = 'GET'
# pylint: disable=unused-variable # pylint: disable=unused-variable
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the REST binary sensor.""" """Setup the REST binary sensor."""
resource = config.get('resource', None) name = config.get(CONF_NAME)
method = config.get('method', DEFAULT_METHOD) resource = config.get(CONF_RESOURCE)
payload = config.get('payload', None) method = config.get(CONF_METHOD)
payload = config.get(CONF_PAYLOAD)
verify_ssl = config.get('verify_ssl', True) verify_ssl = config.get('verify_ssl', True)
sensor_class = config.get(CONF_SENSOR_CLASS)
sensor_class = config.get('sensor_class') value_template = config.get(CONF_VALUE_TEMPLATE)
if sensor_class not in SENSOR_CLASSES:
_LOGGER.warning('Unknown sensor class: %s', sensor_class)
sensor_class = None
rest = RestData(method, resource, payload, verify_ssl) rest = RestData(method, resource, payload, verify_ssl)
rest.update() rest.update()
@ -39,11 +51,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False return False
add_devices([RestBinarySensor( add_devices([RestBinarySensor(
hass, hass, rest, name, sensor_class, value_template)])
rest,
config.get('name', DEFAULT_NAME),
sensor_class,
config.get(CONF_VALUE_TEMPLATE))])
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments

View File

@ -1,5 +1,5 @@
""" """
REST platform for notify component. RESTful platform for notify component.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.rest/ https://home-assistant.io/components/notify.rest/
@ -7,45 +7,56 @@ https://home-assistant.io/components/notify.rest/
import logging import logging
import requests import requests
import voluptuous as vol
from homeassistant.components.notify import ( from homeassistant.components.notify import (
ATTR_TARGET, ATTR_TITLE, DOMAIN, BaseNotificationService) ATTR_TARGET, ATTR_TITLE, BaseNotificationService, PLATFORM_SCHEMA)
from homeassistant.helpers import validate_config from homeassistant.const import (CONF_RESOURCE, CONF_METHOD, CONF_NAME)
import homeassistant.helpers.config_validation as cv
CONF_MESSAGE_PARAMETER_NAME = 'message_param_name'
CONF_TARGET_PARAMETER_NAME = 'target_param_name'
CONF_TITLE_PARAMETER_NAME = 'title_param_name'
DEFAULT_MESSAGE_PARAM_NAME = 'message'
DEFAULT_METHOD = 'GET'
DEFAULT_TARGET_PARAM_NAME = None
DEFAULT_TITLE_PARAM_NAME = None
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_MESSAGE_PARAMETER_NAME,
default=DEFAULT_MESSAGE_PARAM_NAME): cv.string,
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD):
vol.In(['POST', 'GET', 'POST_JSON']),
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_TARGET_PARAMETER_NAME,
default=DEFAULT_TARGET_PARAM_NAME): cv.string,
vol.Optional(CONF_TITLE_PARAMETER_NAME,
default=DEFAULT_TITLE_PARAM_NAME): cv.string,
})
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEFAULT_METHOD = 'GET'
DEFAULT_MESSAGE_PARAM_NAME = 'message'
DEFAULT_TITLE_PARAM_NAME = None
DEFAULT_TARGET_PARAM_NAME = None
def get_service(hass, config): def get_service(hass, config):
"""Get the REST notification service.""" """Get the RESTful notification service."""
if not validate_config({DOMAIN: config}, resource = config.get(CONF_RESOURCE)
{DOMAIN: ['resource', ]}, method = config.get(CONF_METHOD)
_LOGGER): message_param_name = config.get(CONF_MESSAGE_PARAMETER_NAME)
return None title_param_name = config.get(CONF_TITLE_PARAMETER_NAME)
target_param_name = config.get(CONF_TARGET_PARAMETER_NAME)
method = config.get('method', DEFAULT_METHOD) return RestNotificationService(
message_param_name = config.get('message_param_name', resource, method, message_param_name, title_param_name,
DEFAULT_MESSAGE_PARAM_NAME)
title_param_name = config.get('title_param_name',
DEFAULT_TITLE_PARAM_NAME)
target_param_name = config.get('target_param_name',
DEFAULT_TARGET_PARAM_NAME)
return RestNotificationService(config['resource'], method,
message_param_name, title_param_name,
target_param_name) target_param_name)
# pylint: disable=too-few-public-methods, too-many-arguments # pylint: disable=too-few-public-methods, too-many-arguments
class RestNotificationService(BaseNotificationService): class RestNotificationService(BaseNotificationService):
"""Implement the notification service for REST.""" """Implementation of a notification service for REST."""
def __init__(self, resource, method, message_param_name, def __init__(self, resource, method, message_param_name, title_param_name,
title_param_name, target_param_name): target_param_name):
"""Initialize the service.""" """Initialize the service."""
self._resource = resource self._resource = resource
self._method = method.upper() self._method = method.upper()

View File

@ -1,41 +1,56 @@
""" """
Support for REST API sensors.. Support for REST API sensors.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.rest/ https://home-assistant.io/components/sensor.rest/
""" """
import logging import logging
import voluptuous as vol
import requests import requests
from homeassistant.const import CONF_VALUE_TEMPLATE, STATE_UNKNOWN from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_PAYLOAD, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_METHOD, CONF_RESOURCE,
CONF_UNIT_OF_MEASUREMENT, STATE_UNKNOWN)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers import template from homeassistant.helpers import template
import homeassistant.helpers.config_validation as cv
DEFAULT_METHOD = 'GET'
DEFAULT_NAME = 'REST Sensor'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): vol.In(['POST', 'GET']),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PAYLOAD): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'REST Sensor'
DEFAULT_METHOD = 'GET'
# pylint: disable=unused-variable # pylint: disable=unused-variable
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the REST sensor.""" """Setup the RESTful sensor."""
resource = config.get('resource', None) name = config.get(CONF_NAME)
method = config.get('method', DEFAULT_METHOD) resource = config.get(CONF_RESOURCE)
payload = config.get('payload', None) method = config.get(CONF_METHOD)
payload = config.get(CONF_PAYLOAD)
verify_ssl = config.get('verify_ssl', True) verify_ssl = config.get('verify_ssl', True)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
value_template = config.get(CONF_VALUE_TEMPLATE)
rest = RestData(method, resource, payload, verify_ssl) rest = RestData(method, resource, payload, verify_ssl)
rest.update() rest.update()
if rest.data is None: if rest.data is None:
_LOGGER.error('Unable to fetch Rest data') _LOGGER.error('Unable to fetch REST data')
return False return False
add_devices([RestSensor( add_devices([RestSensor(hass, rest, name, unit, value_template)])
hass, rest, config.get('name', DEFAULT_NAME),
config.get('unit_of_measurement'), config.get(CONF_VALUE_TEMPLATE))])
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments

View File

@ -7,24 +7,35 @@ https://home-assistant.io/components/switch.rest/
import logging import logging
import requests import requests
import voluptuous as vol
from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, CONF_RESOURCE)
import homeassistant.helpers.config_validation as cv
CONF_BODY_OFF = 'body_off'
CONF_BODY_ON = 'body_on'
DEFAULT_BODY_OFF = 'OFF'
DEFAULT_BODY_ON = 'ON'
DEFAULT_NAME = 'REST Switch'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_BODY_OFF, default=DEFAULT_BODY_OFF): cv.string,
vol.Optional(CONF_BODY_ON, default=DEFAULT_BODY_ON): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "REST Switch"
DEFAULT_BODY_ON = "ON"
DEFAULT_BODY_OFF = "OFF"
# pylint: disable=unused-argument, # pylint: disable=unused-argument,
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup the REST switch.""" """Setup the RESTful switch."""
resource = config.get('resource') name = config.get(CONF_NAME)
resource = config.get(CONF_RESOURCE)
if resource is None: body_on = config.get(CONF_BODY_ON)
_LOGGER.error("Missing required variable: resource") body_off = config.get(CONF_BODY_OFF)
return False
try: try:
requests.get(resource, timeout=10) requests.get(resource, timeout=10)
@ -36,12 +47,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
_LOGGER.error("No route to resource/endpoint: %s", resource) _LOGGER.error("No route to resource/endpoint: %s", resource)
return False return False
add_devices_callback([RestSwitch( add_devices_callback([RestSwitch(hass, name, resource, body_on, body_off)])
hass,
config.get('name', DEFAULT_NAME),
config.get('resource'),
config.get('body_on', DEFAULT_BODY_ON),
config.get('body_off', DEFAULT_BODY_OFF))])
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments

View File

@ -40,6 +40,7 @@ CONF_HOSTS = 'hosts'
CONF_ICON = 'icon' CONF_ICON = 'icon'
CONF_LATITUDE = 'latitude' CONF_LATITUDE = 'latitude'
CONF_LONGITUDE = 'longitude' CONF_LONGITUDE = 'longitude'
CONF_METHOD = 'method'
CONF_MONITORED_CONDITIONS = 'monitored_conditions' CONF_MONITORED_CONDITIONS = 'monitored_conditions'
CONF_MONITORED_VARIABLES = 'monitored_variables' CONF_MONITORED_VARIABLES = 'monitored_variables'
CONF_NAME = 'name' CONF_NAME = 'name'
@ -53,6 +54,7 @@ CONF_PORT = 'port'
CONF_RESOURCE = 'resource' CONF_RESOURCE = 'resource'
CONF_RESOURCES = 'resources' CONF_RESOURCES = 'resources'
CONF_SCAN_INTERVAL = 'scan_interval' CONF_SCAN_INTERVAL = 'scan_interval'
CONF_SENSOR_CLASS = 'sensor_class'
CONF_STATE = 'state' CONF_STATE = 'state'
CONF_TEMPERATURE_UNIT = 'temperature_unit' CONF_TEMPERATURE_UNIT = 'temperature_unit'
CONF_TIME_ZONE = 'time_zone' CONF_TIME_ZONE = 'time_zone'