diff --git a/homeassistant/components/binary_sensor/rest.py b/homeassistant/components/binary_sensor/rest.py index ac82ab126fd..304ed701148 100644 --- a/homeassistant/components/binary_sensor/rest.py +++ b/homeassistant/components/binary_sensor/rest.py @@ -14,7 +14,7 @@ from homeassistant.components.binary_sensor import ( from homeassistant.components.sensor.rest import RestData from homeassistant.const import ( CONF_PAYLOAD, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_METHOD, CONF_RESOURCE, - CONF_VERIFY_SSL, CONF_USERNAME, CONF_PASSWORD, + CONF_VERIFY_SSL, CONF_USERNAME, CONF_PASSWORD, CONF_TIMEOUT, CONF_HEADERS, CONF_AUTHENTICATION, HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION, CONF_DEVICE_CLASS) import homeassistant.helpers.config_validation as cv @@ -25,6 +25,7 @@ _LOGGER = logging.getLogger(__name__) DEFAULT_METHOD = 'GET' DEFAULT_NAME = 'REST Binary Sensor' DEFAULT_VERIFY_SSL = True +DEFAULT_TIMEOUT = 10 PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_RESOURCE): cv.url, @@ -39,6 +40,7 @@ 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_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, }) @@ -49,6 +51,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): method = config.get(CONF_METHOD) payload = config.get(CONF_PAYLOAD) verify_ssl = config.get(CONF_VERIFY_SSL) + timeout = config.get(CONF_TIMEOUT) username = config.get(CONF_USERNAME) password = config.get(CONF_PASSWORD) headers = config.get(CONF_HEADERS) @@ -65,7 +68,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): else: auth = None - rest = RestData(method, resource, auth, headers, payload, verify_ssl) + rest = RestData(method, resource, auth, headers, payload, verify_ssl, + timeout) rest.update() if rest.data is None: raise PlatformNotReady diff --git a/homeassistant/components/sensor/rest.py b/homeassistant/components/sensor/rest.py index 4eb4b940095..a9446ee3503 100644 --- a/homeassistant/components/sensor/rest.py +++ b/homeassistant/components/sensor/rest.py @@ -16,7 +16,7 @@ from homeassistant.components.sensor import ( from homeassistant.const import ( CONF_AUTHENTICATION, CONF_FORCE_UPDATE, CONF_HEADERS, CONF_NAME, CONF_METHOD, CONF_PASSWORD, CONF_PAYLOAD, CONF_RESOURCE, - CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME, + CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME, CONF_TIMEOUT, CONF_VALUE_TEMPLATE, CONF_VERIFY_SSL, CONF_DEVICE_CLASS, HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION) from homeassistant.exceptions import PlatformNotReady @@ -29,6 +29,7 @@ DEFAULT_METHOD = 'GET' DEFAULT_NAME = 'REST Sensor' DEFAULT_VERIFY_SSL = True DEFAULT_FORCE_UPDATE = False +DEFAULT_TIMEOUT = 10 CONF_JSON_ATTRS = 'json_attributes' METHODS = ['POST', 'GET'] @@ -49,6 +50,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, vol.Optional(CONF_FORCE_UPDATE, default=DEFAULT_FORCE_UPDATE): cv.boolean, + vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, }) @@ -67,6 +69,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): value_template = config.get(CONF_VALUE_TEMPLATE) json_attrs = config.get(CONF_JSON_ATTRS) force_update = config.get(CONF_FORCE_UPDATE) + timeout = config.get(CONF_TIMEOUT) if value_template is not None: value_template.hass = hass @@ -78,7 +81,8 @@ def setup_platform(hass, config, add_entities, 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, + timeout) rest.update() if rest.data is None: raise PlatformNotReady @@ -174,11 +178,13 @@ class RestSensor(Entity): class RestData: """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, + timeout=DEFAULT_TIMEOUT): """Initialize the data object.""" self._request = requests.Request( method, resource, headers=headers, auth=auth, data=data).prepare() self._verify_ssl = verify_ssl + self._timeout = timeout self.data = None def update(self): @@ -187,7 +193,8 @@ class RestData: try: with requests.Session() as sess: response = sess.send( - self._request, timeout=10, verify=self._verify_ssl) + self._request, timeout=self._timeout, + verify=self._verify_ssl) self.data = response.text except requests.exceptions.RequestException as ex: diff --git a/tests/components/sensor/test_rest.py b/tests/components/sensor/test_rest.py index 3e71be8a6f6..343cc696763 100644 --- a/tests/components/sensor/test_rest.py +++ b/tests/components/sensor/test_rest.py @@ -89,6 +89,7 @@ class TestRestSensorSetup(unittest.TestCase): 'name': 'foo', 'unit_of_measurement': 'MB', 'verify_ssl': 'true', + 'timeout': 30, 'authentication': 'basic', 'username': 'my username', 'password': 'my password', @@ -112,6 +113,7 @@ class TestRestSensorSetup(unittest.TestCase): 'name': 'foo', 'unit_of_measurement': 'MB', 'verify_ssl': 'true', + 'timeout': 30, 'authentication': 'basic', 'username': 'my username', 'password': 'my password', @@ -280,8 +282,10 @@ class TestRestData(unittest.TestCase): self.method = "GET" self.resource = "http://localhost" self.verify_ssl = True + self.timeout = 10 self.rest = rest.RestData( - self.method, self.resource, None, None, None, self.verify_ssl) + self.method, self.resource, None, None, None, self.verify_ssl, + self.timeout) @requests_mock.Mocker() def test_update(self, mock_req):