function support for aREST backend

The implementation sends /<name>?params=1 for turn_on and /<name>?params=0 for turn_off
It uses the return value of the function to determine the current state.
0=Off, 1=On
pull/652/head
Pascal Bach 2015-11-26 20:45:07 +01:00
parent 3e60c4801c
commit 067011af15
1 changed files with 82 additions and 13 deletions

View File

@ -34,30 +34,33 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False return False
dev = [] dev = []
pins = config.get('pins') pins = config.get('pins', {})
for pinnum, pin in pins.items(): for pinnum, pin in pins.items():
dev.append(ArestSwitch(resource, dev.append(ArestSwitchPin(resource,
config.get('name', response.json()['name']), config.get('name', response.json()['name']),
pin.get('name'), pin.get('name'),
pinnum)) pinnum))
functions = config.get('functions', {})
for funcname, func in functions.items():
dev.append(ArestSwitchFunction(resource,
config.get('name',
response.json()['name']),
func.get('name'),
funcname))
add_devices(dev) add_devices(dev)
class ArestSwitch(SwitchDevice): class ArestSwitchBase(SwitchDevice):
""" Implements an aREST switch. """ """ Implements an aREST switch. """
def __init__(self, resource, location, name, pin): def __init__(self, resource, location, name):
self._resource = resource self._resource = resource
self._name = '{} {}'.format(location.title(), name.title()) \ self._name = '{} {}'.format(location.title(), name.title()) \
or DEVICE_DEFAULT_NAME or DEVICE_DEFAULT_NAME
self._pin = pin
self._state = None self._state = None
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?")
@property @property
def name(self): def name(self):
""" The name of the switch. """ """ The name of the switch. """
@ -68,6 +71,72 @@ class ArestSwitch(SwitchDevice):
""" True if device is on. """ """ True if device is on. """
return self._state return self._state
class ArestSwitchFunction(ArestSwitchBase):
""" Implements an aREST switch. Based on functions. """
def __init__(self, resource, location, name, func):
super().__init__(resource, location, name)
self._func = func
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?")
return
try:
request.json()['return_value']
except KeyError:
_LOGGER.error("No return_value received. "
"Is the function name correct.")
except ValueError:
_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"})
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)
def turn_off(self, **kwargs):
""" Turn the device off. """
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)
def update(self):
""" Gets the latest data from aREST API and updates the state. """
request = requests.get('{}/{}'.format(self._resource,
self._func), timeout=10)
self._state = request.json()['return_value'] != 0
class ArestSwitchPin(ArestSwitchBase):
""" Implements an aREST switch. Based on digital I/O """
def __init__(self, resource, location, name, pin):
super().__init__(resource, location, name)
self._pin = pin
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): def turn_on(self, **kwargs):
""" Turn the device on. """ """ Turn the device on. """
request = requests.get('{}/digital/{}/1'.format(self._resource, request = requests.get('{}/digital/{}/1'.format(self._resource,