Add timeout option to sensor.rest and binary_sensor.rest. (#20065)
parent
90d3f517d8
commit
5a5c97acb3
|
@ -14,7 +14,7 @@ from homeassistant.components.binary_sensor import (
|
||||||
from homeassistant.components.sensor.rest import RestData
|
from homeassistant.components.sensor.rest import RestData
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_PAYLOAD, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_METHOD, CONF_RESOURCE,
|
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,
|
CONF_HEADERS, CONF_AUTHENTICATION, HTTP_BASIC_AUTHENTICATION,
|
||||||
HTTP_DIGEST_AUTHENTICATION, CONF_DEVICE_CLASS)
|
HTTP_DIGEST_AUTHENTICATION, CONF_DEVICE_CLASS)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
@ -25,6 +25,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
DEFAULT_METHOD = 'GET'
|
DEFAULT_METHOD = 'GET'
|
||||||
DEFAULT_NAME = 'REST Binary Sensor'
|
DEFAULT_NAME = 'REST Binary Sensor'
|
||||||
DEFAULT_VERIFY_SSL = True
|
DEFAULT_VERIFY_SSL = True
|
||||||
|
DEFAULT_TIMEOUT = 10
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_RESOURCE): cv.url,
|
vol.Required(CONF_RESOURCE): cv.url,
|
||||||
|
@ -39,6 +40,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_USERNAME): cv.string,
|
vol.Optional(CONF_USERNAME): cv.string,
|
||||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
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)
|
method = config.get(CONF_METHOD)
|
||||||
payload = config.get(CONF_PAYLOAD)
|
payload = config.get(CONF_PAYLOAD)
|
||||||
verify_ssl = config.get(CONF_VERIFY_SSL)
|
verify_ssl = config.get(CONF_VERIFY_SSL)
|
||||||
|
timeout = config.get(CONF_TIMEOUT)
|
||||||
username = config.get(CONF_USERNAME)
|
username = config.get(CONF_USERNAME)
|
||||||
password = config.get(CONF_PASSWORD)
|
password = config.get(CONF_PASSWORD)
|
||||||
headers = config.get(CONF_HEADERS)
|
headers = config.get(CONF_HEADERS)
|
||||||
|
@ -65,7 +68,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
else:
|
else:
|
||||||
auth = None
|
auth = None
|
||||||
|
|
||||||
rest = RestData(method, resource, auth, headers, payload, verify_ssl)
|
rest = RestData(method, resource, auth, headers, payload, verify_ssl,
|
||||||
|
timeout)
|
||||||
rest.update()
|
rest.update()
|
||||||
if rest.data is None:
|
if rest.data is None:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
|
|
@ -16,7 +16,7 @@ from homeassistant.components.sensor import (
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_AUTHENTICATION, CONF_FORCE_UPDATE, CONF_HEADERS, CONF_NAME,
|
CONF_AUTHENTICATION, CONF_FORCE_UPDATE, CONF_HEADERS, CONF_NAME,
|
||||||
CONF_METHOD, CONF_PASSWORD, CONF_PAYLOAD, CONF_RESOURCE,
|
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,
|
CONF_VALUE_TEMPLATE, CONF_VERIFY_SSL, CONF_DEVICE_CLASS,
|
||||||
HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION)
|
HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION)
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
|
@ -29,6 +29,7 @@ DEFAULT_METHOD = 'GET'
|
||||||
DEFAULT_NAME = 'REST Sensor'
|
DEFAULT_NAME = 'REST Sensor'
|
||||||
DEFAULT_VERIFY_SSL = True
|
DEFAULT_VERIFY_SSL = True
|
||||||
DEFAULT_FORCE_UPDATE = False
|
DEFAULT_FORCE_UPDATE = False
|
||||||
|
DEFAULT_TIMEOUT = 10
|
||||||
|
|
||||||
CONF_JSON_ATTRS = 'json_attributes'
|
CONF_JSON_ATTRS = 'json_attributes'
|
||||||
METHODS = ['POST', 'GET']
|
METHODS = ['POST', 'GET']
|
||||||
|
@ -49,6 +50,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
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_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)
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||||
json_attrs = config.get(CONF_JSON_ATTRS)
|
json_attrs = config.get(CONF_JSON_ATTRS)
|
||||||
force_update = config.get(CONF_FORCE_UPDATE)
|
force_update = config.get(CONF_FORCE_UPDATE)
|
||||||
|
timeout = config.get(CONF_TIMEOUT)
|
||||||
|
|
||||||
if value_template is not None:
|
if value_template is not None:
|
||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
|
@ -78,7 +81,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
auth = HTTPBasicAuth(username, password)
|
auth = HTTPBasicAuth(username, password)
|
||||||
else:
|
else:
|
||||||
auth = None
|
auth = None
|
||||||
rest = RestData(method, resource, auth, headers, payload, verify_ssl)
|
rest = RestData(method, resource, auth, headers, payload, verify_ssl,
|
||||||
|
timeout)
|
||||||
rest.update()
|
rest.update()
|
||||||
if rest.data is None:
|
if rest.data is None:
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
@ -174,11 +178,13 @@ class RestSensor(Entity):
|
||||||
class RestData:
|
class RestData:
|
||||||
"""Class for handling the data retrieval."""
|
"""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."""
|
"""Initialize the data object."""
|
||||||
self._request = requests.Request(
|
self._request = requests.Request(
|
||||||
method, resource, headers=headers, auth=auth, data=data).prepare()
|
method, resource, headers=headers, auth=auth, data=data).prepare()
|
||||||
self._verify_ssl = verify_ssl
|
self._verify_ssl = verify_ssl
|
||||||
|
self._timeout = timeout
|
||||||
self.data = None
|
self.data = None
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
@ -187,7 +193,8 @@ class RestData:
|
||||||
try:
|
try:
|
||||||
with requests.Session() as sess:
|
with requests.Session() as sess:
|
||||||
response = sess.send(
|
response = sess.send(
|
||||||
self._request, timeout=10, verify=self._verify_ssl)
|
self._request, timeout=self._timeout,
|
||||||
|
verify=self._verify_ssl)
|
||||||
|
|
||||||
self.data = response.text
|
self.data = response.text
|
||||||
except requests.exceptions.RequestException as ex:
|
except requests.exceptions.RequestException as ex:
|
||||||
|
|
|
@ -89,6 +89,7 @@ class TestRestSensorSetup(unittest.TestCase):
|
||||||
'name': 'foo',
|
'name': 'foo',
|
||||||
'unit_of_measurement': 'MB',
|
'unit_of_measurement': 'MB',
|
||||||
'verify_ssl': 'true',
|
'verify_ssl': 'true',
|
||||||
|
'timeout': 30,
|
||||||
'authentication': 'basic',
|
'authentication': 'basic',
|
||||||
'username': 'my username',
|
'username': 'my username',
|
||||||
'password': 'my password',
|
'password': 'my password',
|
||||||
|
@ -112,6 +113,7 @@ class TestRestSensorSetup(unittest.TestCase):
|
||||||
'name': 'foo',
|
'name': 'foo',
|
||||||
'unit_of_measurement': 'MB',
|
'unit_of_measurement': 'MB',
|
||||||
'verify_ssl': 'true',
|
'verify_ssl': 'true',
|
||||||
|
'timeout': 30,
|
||||||
'authentication': 'basic',
|
'authentication': 'basic',
|
||||||
'username': 'my username',
|
'username': 'my username',
|
||||||
'password': 'my password',
|
'password': 'my password',
|
||||||
|
@ -280,8 +282,10 @@ class TestRestData(unittest.TestCase):
|
||||||
self.method = "GET"
|
self.method = "GET"
|
||||||
self.resource = "http://localhost"
|
self.resource = "http://localhost"
|
||||||
self.verify_ssl = True
|
self.verify_ssl = True
|
||||||
|
self.timeout = 10
|
||||||
self.rest = rest.RestData(
|
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()
|
@requests_mock.Mocker()
|
||||||
def test_update(self, mock_req):
|
def test_update(self, mock_req):
|
||||||
|
|
Loading…
Reference in New Issue