Voluptuous arest (#3558)

* Migrate to voluptuous

* Adjust sensor.arest for new template system

* Use items() to align the var section with the pins
This commit is contained in:
Paulus Schoutsen 2016-10-01 09:45:43 -07:00 committed by Fabian Affolter
parent 15ed8c6332
commit bb03960ba5
3 changed files with 118 additions and 93 deletions

View File

@ -1,5 +1,5 @@
""" """
Support for exposed aREST RESTful API of a device. Support for an exposed aREST RESTful API of a device.
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/binary_sensor.arest/ https://home-assistant.io/components/binary_sensor.arest/
@ -8,31 +8,32 @@ import logging
from datetime import timedelta from datetime import timedelta
import requests import requests
import voluptuous as vol
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDevice, SENSOR_CLASSES) BinarySensorDevice, PLATFORM_SCHEMA, SENSOR_CLASSES_SCHEMA)
from homeassistant.const import CONF_RESOURCE, CONF_PIN from homeassistant.const import (
CONF_RESOURCE, CONF_PIN, CONF_NAME, CONF_SENSOR_CLASS)
from homeassistant.util import Throttle from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_PIN): cv.string,
vol.Optional(CONF_SENSOR_CLASS): SENSOR_CLASSES_SCHEMA,
})
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the aREST binary sensor.""" """Setup the aREST binary sensor."""
resource = config.get(CONF_RESOURCE) resource = config.get(CONF_RESOURCE)
pin = config.get(CONF_PIN) pin = config.get(CONF_PIN)
sensor_class = config.get(CONF_SENSOR_CLASS)
sensor_class = config.get('sensor_class')
if sensor_class not in SENSOR_CLASSES:
_LOGGER.warning('Unknown sensor class: %s', sensor_class)
sensor_class = None
if None in (resource, pin):
_LOGGER.error('Not all required config keys present: %s',
', '.join((CONF_RESOURCE, CONF_PIN)))
return False
try: try:
response = requests.get(resource, timeout=10).json() response = requests.get(resource, timeout=10).json()
@ -49,11 +50,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
arest = ArestData(resource, pin) arest = ArestData(resource, pin)
add_devices([ArestBinarySensor( add_devices([ArestBinarySensor(
arest, arest, resource, config.get(CONF_NAME, response[CONF_NAME]),
resource, sensor_class, pin)])
config.get('name', response['name']),
sensor_class,
pin)])
# pylint: disable=too-many-instance-attributes, too-many-arguments # pylint: disable=too-many-instance-attributes, too-many-arguments

View File

@ -1,5 +1,5 @@
""" """
The arest sensor will consume an exposed aREST API of a device. Support for an exposed aREST RESTful API of a device.
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.arest/ https://home-assistant.io/components/sensor.arest/
@ -8,30 +8,48 @@ import logging
from datetime import timedelta from datetime import timedelta
import requests import requests
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ( from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, DEVICE_DEFAULT_NAME, CONF_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, CONF_RESOURCE,
CONF_RESOURCE, CONF_MONITORED_VARIABLES) CONF_MONITORED_VARIABLES, CONF_NAME, STATE_UNKNOWN)
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers import template
from homeassistant.util import Throttle from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
CONF_FUNCTIONS = 'functions'
CONF_PINS = 'pins'
DEFAULT_NAME = 'aREST sensor'
PIN_VARIABLE_SCHEMA = vol.Schema({
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PINS, default={}):
vol.Schema({cv.string: PIN_VARIABLE_SCHEMA}),
vol.Optional(CONF_MONITORED_VARIABLES, default={}):
vol.Schema({cv.string: PIN_VARIABLE_SCHEMA}),
})
# pylint: disable=too-many-locals
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the aREST sensor.""" """Setup the aREST sensor."""
resource = config.get(CONF_RESOURCE) resource = config.get(CONF_RESOURCE)
var_conf = config.get(CONF_MONITORED_VARIABLES) var_conf = config.get(CONF_MONITORED_VARIABLES)
pins = config.get('pins', None) pins = config.get(CONF_PINS)
if resource is None:
_LOGGER.error('Not all required config keys present: %s',
CONF_RESOURCE)
return False
try: try:
response = requests.get(resource, timeout=10).json() response = requests.get(resource, timeout=10).json()
@ -52,7 +70,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if value_template is None: if value_template is None:
return lambda value: value return lambda value: value
value_template = template.Template(value_template, hass) value_template.hass = hass
def _render(value): def _render(value):
try: try:
@ -66,33 +84,26 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
dev = [] dev = []
if var_conf is not None: if var_conf is not None:
for variable in var_conf: for variable, var_data in var_conf.items():
if variable['name'] not in response['variables']: if variable not in response['variables']:
_LOGGER.error('Variable: "%s" does not exist', _LOGGER.error("Variable: '%s' does not exist", variable)
variable['name'])
continue continue
renderer = make_renderer(variable.get(CONF_VALUE_TEMPLATE)) renderer = make_renderer(var_data.get(CONF_VALUE_TEMPLATE))
dev.append(ArestSensor(arest, dev.append(ArestSensor(
resource, arest, resource, config.get(CONF_NAME, response[CONF_NAME]),
config.get('name', response['name']), variable, variable=variable,
variable['name'], unit_of_measurement=var_data.get(CONF_UNIT_OF_MEASUREMENT),
variable=variable['name'], renderer=renderer))
unit_of_measurement=variable.get(
ATTR_UNIT_OF_MEASUREMENT),
renderer=renderer))
if pins is not None: if pins is not None:
for pinnum, pin in pins.items(): for pinnum, pin in pins.items():
renderer = make_renderer(pin.get(CONF_VALUE_TEMPLATE)) renderer = make_renderer(pin.get(CONF_VALUE_TEMPLATE))
dev.append(ArestSensor(ArestData(resource, pinnum), dev.append(ArestSensor(
resource, ArestData(resource, pinnum), resource,
config.get('name', response['name']), config.get(CONF_NAME, response[CONF_NAME]), pin.get(CONF_NAME),
pin.get('name'), pin=pinnum, unit_of_measurement=pin.get(
pin=pinnum, CONF_UNIT_OF_MEASUREMENT), renderer=renderer))
unit_of_measurement=pin.get(
ATTR_UNIT_OF_MEASUREMENT),
renderer=renderer))
add_devices(dev) add_devices(dev)
@ -106,18 +117,17 @@ class ArestSensor(Entity):
"""Initialize the sensor.""" """Initialize the sensor."""
self.arest = arest self.arest = arest
self._resource = resource self._resource = resource
self._name = '{} {}'.format(location.title(), name.title()) \ self._name = '{} {}'.format(location.title(), name.title())
or DEVICE_DEFAULT_NAME
self._variable = variable self._variable = variable
self._pin = pin self._pin = pin
self._state = 'n/a' self._state = STATE_UNKNOWN
self._unit_of_measurement = unit_of_measurement self._unit_of_measurement = unit_of_measurement
self._renderer = renderer self._renderer = renderer
self.update() self.update()
if self._pin is not None: if self._pin is not None:
request = requests.get('{}/mode/{}/i'.format request = requests.get(
(self._resource, self._pin), timeout=10) '{}/mode/{}/i'.format(self._resource, self._pin), timeout=10)
if request.status_code is not 200: if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?") _LOGGER.error("Can't set mode. Is device offline?")
@ -139,9 +149,8 @@ class ArestSensor(Entity):
if 'error' in values: if 'error' in values:
return values['error'] return values['error']
value = self._renderer(values.get('value', value = self._renderer(
values.get(self._variable, values.get('value', values.get(self._variable, STATE_UNKNOWN)))
'N/A')))
return value return value
def update(self): def update(self):

View File

@ -1,5 +1,5 @@
""" """
Support for device running with the aREST RESTful framework. Support for an exposed aREST RESTful API of a device.
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/switch.arest/ https://home-assistant.io/components/switch.arest/
@ -8,17 +8,36 @@ https://home-assistant.io/components/switch.arest/
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 ( from homeassistant.const import (CONF_NAME, CONF_RESOURCE)
DEVICE_DEFAULT_NAME, CONF_NAME, CONF_RESOURCE) import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_FUNCTIONS = 'functions'
CONF_PINS = 'pins'
DEFAULT_NAME = 'aREST switch'
PIN_FUNCTION_SCHEMA = vol.Schema({
vol.Optional(CONF_NAME): cv.string,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PINS, default={}):
vol.Schema({cv.string: PIN_FUNCTION_SCHEMA}),
vol.Optional(CONF_FUNCTIONS, default={}):
vol.Schema({cv.string: PIN_FUNCTION_SCHEMA}),
})
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the aREST switches.""" """Setup the aREST switches."""
resource = config.get(CONF_RESOURCE, None) resource = config.get(CONF_RESOURCE)
try: try:
response = requests.get(resource, timeout=10) response = requests.get(resource, timeout=10)
@ -33,29 +52,28 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False return False
dev = [] dev = []
pins = config.get('pins', {}) pins = config.get(CONF_PINS)
for pinnum, pin in pins.items(): for pinnum, pin in pins.items():
dev.append(ArestSwitchPin( dev.append(ArestSwitchPin(
resource, config.get(CONF_NAME, response.json()['name']), resource, config.get(CONF_NAME, response.json()[CONF_NAME]),
pin.get('name'), pinnum)) pin.get(CONF_NAME), pinnum))
functions = config.get('functions', {}) functions = config.get(CONF_FUNCTIONS)
for funcname, func in functions.items(): for funcname, func in functions.items():
dev.append(ArestSwitchFunction( dev.append(ArestSwitchFunction(
resource, config.get(CONF_NAME, response.json()['name']), resource, config.get(CONF_NAME, response.json()[CONF_NAME]),
func.get('name'), funcname)) func.get(CONF_NAME), funcname))
add_devices(dev) add_devices(dev)
class ArestSwitchBase(SwitchDevice): class ArestSwitchBase(SwitchDevice):
"""representation of an aREST switch.""" """Representation of an aREST switch."""
def __init__(self, resource, location, name): def __init__(self, resource, location, name):
"""Initialize the switch.""" """Initialize the switch."""
self._resource = resource self._resource = resource
self._name = '{} {}'.format(location.title(), name.title()) \ self._name = '{} {}'.format(location.title(), name.title())
or DEVICE_DEFAULT_NAME
self._state = None self._state = None
@property @property
@ -77,8 +95,8 @@ class ArestSwitchFunction(ArestSwitchBase):
super().__init__(resource, location, name) super().__init__(resource, location, name)
self._func = func self._func = func
request = requests.get('{}/{}'.format(self._resource, self._func), request = requests.get(
timeout=10) '{}/{}'.format(self._resource, self._func), timeout=10)
if request.status_code is not 200: if request.status_code is not 200:
_LOGGER.error("Can't find function. Is device offline?") _LOGGER.error("Can't find function. Is device offline?")
@ -90,36 +108,36 @@ class ArestSwitchFunction(ArestSwitchBase):
_LOGGER.error("No return_value received. " _LOGGER.error("No return_value received. "
"Is the function name correct.") "Is the function name correct.")
except ValueError: except ValueError:
_LOGGER.error("Response invalid. Is the function name correct.") _LOGGER.error("Response invalid. Is the function name correct?")
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
request = requests.get('{}/{}'.format(self._resource, self._func), request = requests.get(
timeout=10, params={"params": "1"}) '{}/{}'.format(self._resource, self._func), timeout=10,
params={'params': '1'})
if request.status_code == 200: if request.status_code == 200:
self._state = True self._state = True
else: else:
_LOGGER.error("Can't turn on function %s at %s. " _LOGGER.error("Can't turn on function %s at %s. "
"Is device offline?", "Is device offline?", self._func, self._resource)
self._func, self._resource)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
request = requests.get('{}/{}'.format(self._resource, self._func), request = requests.get(
timeout=10, params={"params": "0"}) '{}/{}'.format(self._resource, self._func), timeout=10,
params={'params': '0'})
if request.status_code == 200: if request.status_code == 200:
self._state = False self._state = False
else: else:
_LOGGER.error("Can't turn off function %s at %s. " _LOGGER.error("Can't turn off function %s at %s. "
"Is device offline?", "Is device offline?", self._func, self._resource)
self._func, self._resource)
def update(self): def update(self):
"""Get the latest data from aREST API and update the state.""" """Get the latest data from aREST API and update the state."""
request = requests.get('{}/{}'.format(self._resource, request = requests.get(
self._func), timeout=10) '{}/{}'.format(self._resource, self._func), timeout=10)
self._state = request.json()['return_value'] != 0 self._state = request.json()['return_value'] != 0
@ -131,15 +149,15 @@ class ArestSwitchPin(ArestSwitchBase):
super().__init__(resource, location, name) super().__init__(resource, location, name)
self._pin = pin self._pin = pin
request = requests.get('{}/mode/{}/o'.format(self._resource, request = requests.get(
self._pin), timeout=10) '{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
if request.status_code is not 200: if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?") _LOGGER.error("Can't set mode. Is device offline?")
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
request = requests.get('{}/digital/{}/1'.format(self._resource, request = requests.get(
self._pin), timeout=10) '{}/digital/{}/1'.format(self._resource, self._pin), timeout=10)
if request.status_code == 200: if request.status_code == 200:
self._state = True self._state = True
else: else:
@ -148,8 +166,8 @@ class ArestSwitchPin(ArestSwitchBase):
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
request = requests.get('{}/digital/{}/0'.format(self._resource, request = requests.get(
self._pin), timeout=10) '{}/digital/{}/0'.format(self._resource, self._pin), timeout=10)
if request.status_code == 200: if request.status_code == 200:
self._state = False self._state = False
else: else:
@ -158,6 +176,6 @@ class ArestSwitchPin(ArestSwitchBase):
def update(self): def update(self):
"""Get the latest data from aREST API and update the state.""" """Get the latest data from aREST API and update the state."""
request = requests.get('{}/digital/{}'.format(self._resource, request = requests.get(
self._pin), timeout=10) '{}/digital/{}'.format(self._resource, self._pin), timeout=10)
self._state = request.json()['return_value'] != 0 self._state = request.json()['return_value'] != 0