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