I have not tried this but would this work?
diff --git a/homeassistant/components/sensor/rest.py b/homeassistant/components/sensor/rest.py
index 16b50f8..1ed981e 100644
--- a/homeassistant/components/sensor/rest.py
+++ b/homeassistant/components/sensor/rest.py
@@ -17,6 +17,7 @@ from homeassistant.const import (
CONF_PASSWORD, CONF_AUTHENTICATION, HTTP_BASIC_AUTHENTICATION,
HTTP_DIGEST_AUTHENTICATION, CONF_HEADERS)
from homeassistant.helpers.entity import Entity
+from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
@@ -24,6 +25,7 @@ _LOGGER = logging.getLogger(__name__)
DEFAULT_METHOD = 'GET'
DEFAULT_NAME = 'REST Sensor'
DEFAULT_VERIFY_SSL = True
+CONF_UPDATE_INTERVAL = 'update_interval'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
@@ -38,6 +40,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
+ vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=60)): (
+ vol.All(cv.time_period, cv.positive_timedelta)),
+
})
@@ -63,7 +68,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
auth = HTTPBasicAuth(username, password)
else:
auth = None
- rest = RestData(method, resource, auth, headers, payload, verify_ssl)
+ rest = RestData(method, resource, auth, headers, payload, verify_ssl,
+ interval=config.get(CONF_UPDATE_INTERVAL))
rest.update()
if rest.data is None:
@@ -118,13 +124,17 @@ class RestSensor(Entity):
class RestData(object):
"""Class for handling the data retrieval."""
- def __init__(self, method, resource, auth, headers, data, verify_ssl):
+ def __init__(self, method, resource, auth, headers, data, verify_ssl, interval):
"""Initialize the data object."""
self._request = requests.Request(
method, resource, headers=headers, auth=auth, data=data).prepare()
self._verify_ssl = verify_ssl
self.data = None
+ # Apply throttling to methods using configured interval
+ self.update = Throttle(interval)(self._update)
+
+
def update(self):
"""Get the latest data from REST service with provided method."""
try: