core/homeassistant/components/binary_sensor/arest.py

107 lines
3.4 KiB
Python
Raw Normal View History

2015-11-20 22:39:39 +00:00
"""
2016-03-07 19:21:08 +00:00
Support for exposed aREST RESTful API of a device.
2015-11-20 22:39:39 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.arest/
"""
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
from homeassistant.components.binary_sensor import BinarySensorDevice
2016-02-19 05:27:50 +00:00
from homeassistant.util import Throttle
2015-11-20 22:39:39 +00:00
_LOGGER = logging.getLogger(__name__)
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
CONF_RESOURCE = 'resource'
CONF_PIN = 'pin'
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-07 19:21:08 +00:00
"""Setup the aREST binary sensor."""
2015-11-20 22:39:39 +00:00
resource = config.get(CONF_RESOURCE)
pin = config.get(CONF_PIN)
if None in (resource, pin):
_LOGGER.error('Not all required config keys present: %s',
', '.join((CONF_RESOURCE, CONF_PIN)))
return False
try:
response = requests.get(resource, timeout=10).json()
except requests.exceptions.MissingSchema:
_LOGGER.error('Missing resource or schema in configuration. '
'Add http:// to your URL.')
return False
except requests.exceptions.ConnectionError:
_LOGGER.error('No route to device at %s. '
'Please check the IP address in the configuration file.',
resource)
return False
arest = ArestData(resource, pin)
add_devices([ArestBinarySensor(arest,
resource,
config.get('name', response['name']),
pin)])
# pylint: disable=too-many-instance-attributes, too-many-arguments
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
def __init__(self, arest, resource, name, 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
self._pin = pin
self.update()
if self._pin is not None:
request = requests.get('{}/mode/{}/i'.format
(self._resource, self._pin), timeout=10)
if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?")
@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'))
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()
# pylint: disable=too-few-public-methods
class ArestData(object):
"""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:
_LOGGER.error("No route to device '%s'. Is device offline?",
self._resource)