mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Merge pull request #652 from bachp/arest-function
Function support for aREST backend
This commit is contained in:
commit
65f3e7ccf4
@ -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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user