2019-04-03 15:40:03 +00:00
|
|
|
"""Support for an exposed aREST RESTful API of a device."""
|
2015-11-20 22:39:39 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
from datetime import timedelta
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2015-11-20 22:39:39 +00:00
|
|
|
import requests
|
2016-10-01 16:45:43 +00:00
|
|
|
import voluptuous as vol
|
2015-11-20 22:39:39 +00:00
|
|
|
|
2016-09-11 07:24:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2017-02-11 04:46:15 +00:00
|
|
|
BinarySensorDevice, PLATFORM_SCHEMA, DEVICE_CLASSES_SCHEMA)
|
2016-10-01 16:45:43 +00:00
|
|
|
from homeassistant.const import (
|
2017-07-29 23:46:27 +00:00
|
|
|
CONF_RESOURCE, CONF_PIN, CONF_NAME, CONF_DEVICE_CLASS)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import Throttle
|
2016-10-01 16:45:43 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-11-20 22:39:39 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
|
|
|
|
2016-10-01 16:45:43 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_RESOURCE): cv.url,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_PIN): cv.string,
|
2017-02-11 04:46:15 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
2016-10-01 16:45:43 +00:00
|
|
|
})
|
|
|
|
|
2015-11-20 22:39:39 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-01-24 18:54:14 +00:00
|
|
|
"""Set up the aREST binary sensor."""
|
2015-11-20 22:39:39 +00:00
|
|
|
resource = config.get(CONF_RESOURCE)
|
|
|
|
pin = config.get(CONF_PIN)
|
2017-07-29 23:46:27 +00:00
|
|
|
device_class = config.get(CONF_DEVICE_CLASS)
|
2015-11-20 22:39:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
response = requests.get(resource, timeout=10).json()
|
|
|
|
except requests.exceptions.MissingSchema:
|
2017-01-24 18:54:14 +00:00
|
|
|
_LOGGER.error("Missing resource or schema in configuration. "
|
|
|
|
"Add http:// to your URL")
|
2015-11-20 22:39:39 +00:00
|
|
|
return False
|
|
|
|
except requests.exceptions.ConnectionError:
|
2017-01-24 18:54:14 +00:00
|
|
|
_LOGGER.error("No route to device at %s", resource)
|
2015-11-20 22:39:39 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
arest = ArestData(resource, pin)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([ArestBinarySensor(
|
2016-10-01 16:45:43 +00:00
|
|
|
arest, resource, config.get(CONF_NAME, response[CONF_NAME]),
|
2017-06-29 14:21:29 +00:00
|
|
|
device_class, pin)], True)
|
2015-11-20 22:39:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ArestBinarySensor(BinarySensorDevice):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Implement an aREST binary sensor for a pin."""
|
2015-11-20 22:39:39 +00:00
|
|
|
|
2017-02-11 04:46:15 +00:00
|
|
|
def __init__(self, arest, resource, name, device_class, pin):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Initialize the aREST device."""
|
2015-11-20 22:39:39 +00:00
|
|
|
self.arest = arest
|
|
|
|
self._resource = resource
|
|
|
|
self._name = name
|
2017-02-11 04:46:15 +00:00
|
|
|
self._device_class = device_class
|
2015-11-20 22:39:39 +00:00
|
|
|
self._pin = pin
|
|
|
|
|
|
|
|
if self._pin is not None:
|
2017-01-24 18:54:14 +00:00
|
|
|
request = requests.get(
|
|
|
|
'{}/mode/{}/i'.format(self._resource, self._pin), timeout=10)
|
2017-07-06 03:02:16 +00:00
|
|
|
if request.status_code != 200:
|
2017-01-24 18:54:14 +00:00
|
|
|
_LOGGER.error("Can't set mode of %s", self._resource)
|
2015-11-20 22:39:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return the name of the binary sensor."""
|
2015-11-20 22:39:39 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
2015-11-20 22:39:39 +00:00
|
|
|
return bool(self.arest.data.get('state'))
|
|
|
|
|
2016-03-26 22:39:56 +00:00
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
2016-03-26 22:39:56 +00:00
|
|
|
"""Return the class of this sensor."""
|
2017-02-11 04:46:15 +00:00
|
|
|
return self._device_class
|
2016-03-26 22:39:56 +00:00
|
|
|
|
2015-11-20 22:39:39 +00:00
|
|
|
def update(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Get the latest data from aREST API."""
|
2015-11-20 22:39:39 +00:00
|
|
|
self.arest.update()
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class ArestData:
|
2016-02-22 09:11:46 +00:00
|
|
|
"""Class for handling the data retrieval for pins."""
|
2016-03-07 19:21:08 +00:00
|
|
|
|
2015-11-20 22:39:39 +00:00
|
|
|
def __init__(self, resource, pin):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Initialize the aREST data object."""
|
2015-11-20 22:39:39 +00:00
|
|
|
self._resource = resource
|
|
|
|
self._pin = pin
|
|
|
|
self.data = {}
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Get the latest data from aREST device."""
|
2015-11-20 22:39:39 +00:00
|
|
|
try:
|
|
|
|
response = requests.get('{}/digital/{}'.format(
|
|
|
|
self._resource, self._pin), timeout=10)
|
|
|
|
self.data = {'state': response.json()['return_value']}
|
|
|
|
except requests.exceptions.ConnectionError:
|
2017-01-24 18:54:14 +00:00
|
|
|
_LOGGER.error("No route to device '%s'", self._resource)
|