core/homeassistant/components/sensor/rest.py

108 lines
3.2 KiB
Python
Raw Normal View History

2015-09-13 20:41:16 +00:00
"""
2016-02-23 05:21:49 +00:00
Support for REST API sensors..
2015-09-13 20:41:16 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/sensor.rest/
2015-09-13 20:41:16 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
from datetime import timedelta
import requests
2015-09-13 20:41:16 +00:00
2016-02-19 05:27:50 +00:00
from homeassistant.const import CONF_VALUE_TEMPLATE, STATE_UNKNOWN
2015-09-13 20:41:16 +00:00
from homeassistant.helpers.entity import Entity
2016-02-19 05:27:50 +00:00
from homeassistant.util import Throttle, template
2015-09-13 20:41:16 +00:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'REST Sensor'
DEFAULT_METHOD = 'GET'
2015-09-13 20:41:16 +00:00
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
# pylint: disable=unused-variable
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-02-23 05:21:49 +00:00
"""Get the REST sensor."""
2015-09-13 20:41:16 +00:00
resource = config.get('resource', None)
method = config.get('method', DEFAULT_METHOD)
payload = config.get('payload', None)
2015-10-01 06:17:08 +00:00
verify_ssl = config.get('verify_ssl', True)
2016-01-02 21:29:33 +00:00
rest = RestData(method, resource, payload, verify_ssl)
rest.update()
2016-01-02 21:29:33 +00:00
if rest.data is None:
_LOGGER.error('Unable to fetch Rest data')
return False
2015-09-13 20:41:16 +00:00
2016-01-02 21:29:33 +00:00
add_devices([RestSensor(
hass, rest, config.get('name', DEFAULT_NAME),
config.get('unit_of_measurement'), config.get(CONF_VALUE_TEMPLATE))])
2015-09-13 20:41:16 +00:00
# pylint: disable=too-many-arguments
2015-09-13 20:41:16 +00:00
class RestSensor(Entity):
2016-02-23 05:21:49 +00:00
"""Implements a REST sensor."""
2015-09-13 20:41:16 +00:00
2015-12-11 22:19:49 +00:00
def __init__(self, hass, rest, name, unit_of_measurement, value_template):
self._hass = hass
2015-09-13 20:41:16 +00:00
self.rest = rest
self._name = name
2015-12-19 09:06:54 +00:00
self._state = STATE_UNKNOWN
2015-09-13 20:41:16 +00:00
self._unit_of_measurement = unit_of_measurement
2015-12-11 22:19:49 +00:00
self._value_template = value_template
2015-09-13 20:41:16 +00:00
self.update()
@property
def name(self):
2016-02-23 05:21:49 +00:00
"""The name of the sensor."""
2015-09-13 20:41:16 +00:00
return self._name
@property
def unit_of_measurement(self):
2016-02-23 05:21:49 +00:00
"""Unit the value is expressed in."""
2015-09-13 20:41:16 +00:00
return self._unit_of_measurement
@property
def state(self):
2016-02-23 05:21:49 +00:00
"""Returns the state of the device."""
2015-09-13 20:41:16 +00:00
return self._state
def update(self):
2016-02-23 05:21:49 +00:00
"""Gets the latest data from REST API and updates the state."""
2015-09-13 20:41:16 +00:00
self.rest.update()
value = self.rest.data
2015-12-25 21:34:30 +00:00
if value is None:
value = STATE_UNKNOWN
elif self._value_template is not None:
value = template.render_with_possible_json_value(
self._hass, self._value_template, value, STATE_UNKNOWN)
self._state = value
2015-09-13 20:41:16 +00:00
# pylint: disable=too-few-public-methods
2016-01-02 21:29:33 +00:00
class RestData(object):
"""Class for handling the data retrieval."""
2015-09-13 20:41:16 +00:00
2016-01-02 21:29:33 +00:00
def __init__(self, method, resource, data, verify_ssl):
self._request = requests.Request(method, resource, data=data).prepare()
self._verify_ssl = verify_ssl
2015-12-25 21:34:30 +00:00
self.data = None
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
2016-02-23 05:21:49 +00:00
"""Gets the latest data from REST service with GET method."""
try:
2016-01-02 21:29:33 +00:00
with requests.Session() as sess:
response = sess.send(self._request, timeout=10,
verify=self._verify_ssl)
2015-12-11 22:19:49 +00:00
self.data = response.text
2016-01-02 21:29:33 +00:00
except requests.exceptions.RequestException:
_LOGGER.error("Error fetching data: %s", self._request)
2015-12-25 21:34:30 +00:00
self.data = None