2015-09-09 14:22:08 +02:00
|
|
|
"""
|
2016-03-08 13:35:39 +01:00
|
|
|
Support for device running with the aREST RESTful framework.
|
2015-09-09 14:22:08 +02:00
|
|
|
|
2015-10-09 14:44:59 +02:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/switch.arest/
|
2015-09-09 14:22:08 +02:00
|
|
|
"""
|
2016-06-24 21:27:40 -07:00
|
|
|
# pylint: disable=abstract-method
|
2015-09-09 14:22:08 +02:00
|
|
|
import logging
|
2016-02-18 21:27:50 -08:00
|
|
|
|
2015-10-15 18:20:46 +02:00
|
|
|
import requests
|
2015-09-09 14:22:08 +02:00
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2016-09-11 09:24:07 +02:00
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_DEFAULT_NAME, CONF_NAME, CONF_RESOURCE)
|
2015-09-09 14:22:08 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Setup the aREST switches."""
|
2016-09-11 09:24:07 +02:00
|
|
|
resource = config.get(CONF_RESOURCE, None)
|
2015-09-09 14:22:08 +02:00
|
|
|
|
|
|
|
try:
|
2015-10-15 18:20:46 +02:00
|
|
|
response = requests.get(resource, timeout=10)
|
|
|
|
except requests.exceptions.MissingSchema:
|
2015-09-09 14:22:08 +02:00
|
|
|
_LOGGER.error("Missing resource or schema in configuration. "
|
|
|
|
"Add http:// to your URL.")
|
|
|
|
return False
|
2015-10-15 18:20:46 +02:00
|
|
|
except requests.exceptions.ConnectionError:
|
2015-10-18 01:25:36 +02:00
|
|
|
_LOGGER.error("No route to device at %s. "
|
|
|
|
"Please check the IP address in the configuration file.",
|
|
|
|
resource)
|
2015-09-09 14:22:08 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
dev = []
|
2015-11-26 20:45:07 +01:00
|
|
|
pins = config.get('pins', {})
|
2015-09-09 14:22:08 +02:00
|
|
|
for pinnum, pin in pins.items():
|
2016-09-11 09:24:07 +02:00
|
|
|
dev.append(ArestSwitchPin(
|
|
|
|
resource, config.get(CONF_NAME, response.json()['name']),
|
|
|
|
pin.get('name'), pinnum))
|
2015-11-26 20:45:07 +01:00
|
|
|
|
|
|
|
functions = config.get('functions', {})
|
|
|
|
for funcname, func in functions.items():
|
2016-09-11 09:24:07 +02:00
|
|
|
dev.append(ArestSwitchFunction(
|
|
|
|
resource, config.get(CONF_NAME, response.json()['name']),
|
|
|
|
func.get('name'), funcname))
|
2015-11-26 20:45:07 +01:00
|
|
|
|
2015-09-09 14:22:08 +02:00
|
|
|
add_devices(dev)
|
|
|
|
|
|
|
|
|
2015-11-26 20:45:07 +01:00
|
|
|
class ArestSwitchBase(SwitchDevice):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""representation of an aREST switch."""
|
2015-09-09 14:22:08 +02:00
|
|
|
|
2015-11-26 20:45:07 +01:00
|
|
|
def __init__(self, resource, location, name):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the switch."""
|
2015-09-09 14:22:08 +02:00
|
|
|
self._resource = resource
|
2015-09-11 08:07:16 +02:00
|
|
|
self._name = '{} {}'.format(location.title(), name.title()) \
|
2015-09-09 14:22:08 +02:00
|
|
|
or DEVICE_DEFAULT_NAME
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return the name of the switch."""
|
2015-09-09 14:22:08 +02:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Return true if device is on."""
|
2015-09-09 14:22:08 +02:00
|
|
|
return self._state
|
|
|
|
|
2015-11-26 20:45:07 +01:00
|
|
|
|
|
|
|
class ArestSwitchFunction(ArestSwitchBase):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of an aREST switch."""
|
2015-11-26 20:45:07 +01:00
|
|
|
|
|
|
|
def __init__(self, resource, location, name, func):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the switch."""
|
2015-11-26 20:45:07 +01:00
|
|
|
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):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device on."""
|
2015-11-26 20:45:07 +01:00
|
|
|
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):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device off."""
|
2015-11-26 20:45:07 +01:00
|
|
|
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):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Get the latest data from aREST API and update the state."""
|
2015-11-26 20:45:07 +01:00
|
|
|
request = requests.get('{}/{}'.format(self._resource,
|
|
|
|
self._func), timeout=10)
|
|
|
|
self._state = request.json()['return_value'] != 0
|
|
|
|
|
|
|
|
|
|
|
|
class ArestSwitchPin(ArestSwitchBase):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Representation of an aREST switch. Based on digital I/O."""
|
2015-11-26 20:45:07 +01:00
|
|
|
|
|
|
|
def __init__(self, resource, location, name, pin):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Initialize the switch."""
|
2015-11-26 20:45:07 +01:00
|
|
|
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?")
|
|
|
|
|
2015-09-09 14:22:08 +02:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device on."""
|
2015-10-15 18:20:46 +02:00
|
|
|
request = requests.get('{}/digital/{}/1'.format(self._resource,
|
|
|
|
self._pin), timeout=10)
|
2015-09-10 21:07:06 +02:00
|
|
|
if request.status_code == 200:
|
|
|
|
self._state = True
|
|
|
|
else:
|
|
|
|
_LOGGER.error("Can't turn on pin %s at %s. Is device offline?",
|
2015-11-23 21:46:47 +01:00
|
|
|
self._pin, self._resource)
|
2015-09-09 14:22:08 +02:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Turn the device off."""
|
2015-10-15 18:20:46 +02:00
|
|
|
request = requests.get('{}/digital/{}/0'.format(self._resource,
|
|
|
|
self._pin), timeout=10)
|
2015-09-10 21:07:06 +02:00
|
|
|
if request.status_code == 200:
|
|
|
|
self._state = False
|
|
|
|
else:
|
2015-09-11 08:07:16 +02:00
|
|
|
_LOGGER.error("Can't turn off pin %s at %s. Is device offline?",
|
2015-11-23 21:46:47 +01:00
|
|
|
self._pin, self._resource)
|
2015-09-10 19:11:20 +02:00
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 13:35:39 +01:00
|
|
|
"""Get the latest data from aREST API and update the state."""
|
2015-10-15 18:20:46 +02:00
|
|
|
request = requests.get('{}/digital/{}'.format(self._resource,
|
|
|
|
self._pin), timeout=10)
|
2015-09-10 19:11:20 +02:00
|
|
|
self._state = request.json()['return_value'] != 0
|