2015-12-16 23:47:12 +00:00
|
|
|
"""
|
2016-03-07 19:21:08 +00:00
|
|
|
Support for RESTful binary sensors.
|
2015-12-16 23:47:12 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/binary_sensor.rest/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-08-20 23:28:45 +00:00
|
|
|
import voluptuous as vol
|
2016-10-04 08:07:17 +00:00
|
|
|
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
2016-08-20 23:28:45 +00:00
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2017-02-11 04:46:15 +00:00
|
|
|
BinarySensorDevice, DEVICE_CLASSES_SCHEMA, PLATFORM_SCHEMA)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.sensor.rest import RestData
|
2016-08-20 23:28:45 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_PAYLOAD, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_METHOD, CONF_RESOURCE,
|
2016-10-04 08:07:17 +00:00
|
|
|
CONF_SENSOR_CLASS, CONF_VERIFY_SSL, CONF_USERNAME, CONF_PASSWORD,
|
|
|
|
CONF_HEADERS, CONF_AUTHENTICATION, HTTP_BASIC_AUTHENTICATION,
|
2017-02-11 04:46:15 +00:00
|
|
|
HTTP_DIGEST_AUTHENTICATION, CONF_DEVICE_CLASS)
|
2016-08-20 23:28:45 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-02-11 04:46:15 +00:00
|
|
|
from homeassistant.helpers.deprecation import get_deprecated
|
2015-12-16 23:47:12 +00:00
|
|
|
|
2016-09-03 23:45:49 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-12-16 23:47:12 +00:00
|
|
|
DEFAULT_METHOD = 'GET'
|
2016-08-20 23:28:45 +00:00
|
|
|
DEFAULT_NAME = 'REST Binary Sensor'
|
2016-09-03 23:45:49 +00:00
|
|
|
DEFAULT_VERIFY_SSL = True
|
2016-08-20 23:28:45 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_RESOURCE): cv.url,
|
2016-10-04 08:07:17 +00:00
|
|
|
vol.Optional(CONF_AUTHENTICATION):
|
|
|
|
vol.In([HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION]),
|
2016-10-13 16:20:49 +00:00
|
|
|
vol.Optional(CONF_HEADERS): {cv.string: cv.string},
|
2016-08-20 23:28:45 +00:00
|
|
|
vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): vol.In(['POST', 'GET']),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-10-04 08:07:17 +00:00
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
2016-08-20 23:28:45 +00:00
|
|
|
vol.Optional(CONF_PAYLOAD): cv.string,
|
2017-02-11 04:46:15 +00:00
|
|
|
vol.Optional(CONF_SENSOR_CLASS): DEVICE_CLASSES_SCHEMA,
|
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
2016-10-04 08:07:17 +00:00
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
2016-08-20 23:28:45 +00:00
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
2016-09-03 23:45:49 +00:00
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
2016-08-20 23:28:45 +00:00
|
|
|
})
|
|
|
|
|
2015-12-16 23:47:12 +00:00
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the REST binary sensor."""
|
2016-08-20 23:28:45 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
resource = config.get(CONF_RESOURCE)
|
|
|
|
method = config.get(CONF_METHOD)
|
|
|
|
payload = config.get(CONF_PAYLOAD)
|
2016-09-03 23:45:49 +00:00
|
|
|
verify_ssl = config.get(CONF_VERIFY_SSL)
|
2016-10-04 08:07:17 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
2016-10-13 16:20:49 +00:00
|
|
|
headers = config.get(CONF_HEADERS)
|
2017-02-11 04:46:15 +00:00
|
|
|
device_class = get_deprecated(config, CONF_DEVICE_CLASS, CONF_SENSOR_CLASS)
|
2016-08-20 23:28:45 +00:00
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
2016-09-25 20:33:01 +00:00
|
|
|
if value_template is not None:
|
2016-09-28 04:29:55 +00:00
|
|
|
value_template.hass = hass
|
2016-10-04 08:07:17 +00:00
|
|
|
|
|
|
|
if username and password:
|
|
|
|
if config.get(CONF_AUTHENTICATION) == HTTP_DIGEST_AUTHENTICATION:
|
|
|
|
auth = HTTPDigestAuth(username, password)
|
|
|
|
else:
|
|
|
|
auth = HTTPBasicAuth(username, password)
|
|
|
|
else:
|
|
|
|
auth = None
|
|
|
|
|
|
|
|
rest = RestData(method, resource, auth, headers, payload, verify_ssl)
|
2016-01-02 21:29:33 +00:00
|
|
|
rest.update()
|
2015-12-16 23:47:12 +00:00
|
|
|
|
2016-01-02 21:29:33 +00:00
|
|
|
if rest.data is None:
|
2016-10-13 16:20:49 +00:00
|
|
|
_LOGGER.error("Unable to fetch REST data from %s", resource)
|
2016-01-02 21:29:33 +00:00
|
|
|
return False
|
2015-12-16 23:47:12 +00:00
|
|
|
|
2016-01-02 21:29:33 +00:00
|
|
|
add_devices([RestBinarySensor(
|
2017-02-11 04:46:15 +00:00
|
|
|
hass, rest, name, device_class, value_template)])
|
2015-12-16 23:47:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RestBinarySensor(BinarySensorDevice):
|
2016-03-25 17:34:58 +00:00
|
|
|
"""Representation of a REST binary sensor."""
|
2015-12-16 23:47:12 +00:00
|
|
|
|
2017-02-11 04:46:15 +00:00
|
|
|
def __init__(self, hass, rest, name, device_class, value_template):
|
2016-02-22 09:11:46 +00:00
|
|
|
"""Initialize a REST binary sensor."""
|
2015-12-16 23:47:12 +00:00
|
|
|
self._hass = hass
|
|
|
|
self.rest = rest
|
|
|
|
self._name = name
|
2017-02-11 04:46:15 +00:00
|
|
|
self._device_class = device_class
|
2015-12-16 23:47:12 +00:00
|
|
|
self._state = False
|
2016-06-18 16:48:32 +00:00
|
|
|
self._previous_data = None
|
2015-12-16 23:47:12 +00:00
|
|
|
self._value_template = value_template
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return the name of the binary sensor."""
|
2015-12-16 23:47:12 +00:00
|
|
|
return self._name
|
|
|
|
|
2016-03-25 17:34:58 +00:00
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
2016-03-25 17:34:58 +00:00
|
|
|
"""Return the class of this sensor."""
|
2017-02-11 04:46:15 +00:00
|
|
|
return self._device_class
|
2016-03-25 17:34:58 +00:00
|
|
|
|
2015-12-16 23:47:12 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-02-22 09:11:46 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
2016-01-02 21:29:33 +00:00
|
|
|
if self.rest.data is None:
|
2015-12-16 23:47:12 +00:00
|
|
|
return False
|
|
|
|
|
2017-07-23 20:42:41 +00:00
|
|
|
response = self.rest.data
|
|
|
|
|
2016-01-02 21:29:33 +00:00
|
|
|
if self._value_template is not None:
|
2016-10-13 16:20:49 +00:00
|
|
|
response = self._value_template.\
|
|
|
|
async_render_with_possible_json_value(self.rest.data, False)
|
2016-06-18 16:48:32 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
return bool(int(response))
|
|
|
|
except ValueError:
|
2017-04-30 05:04:49 +00:00
|
|
|
return {'true': True, 'on': True, 'open': True,
|
|
|
|
'yes': True}.get(response.lower(), False)
|
2015-12-16 23:47:12 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-02-22 09:11:46 +00:00
|
|
|
"""Get the latest data from REST API and updates the state."""
|
2016-01-02 21:29:33 +00:00
|
|
|
self.rest.update()
|