From 9c76b30e24a912e39a7b2f361a1757594088f2af Mon Sep 17 00:00:00 2001 From: Nathan Henrie Date: Mon, 25 Jul 2016 23:35:33 -0600 Subject: [PATCH] Add timeout kwarg to call_service() and API.__call__() (#2612) Fixes #2611 Adds a timeout kwarg to call_service and API.__call__ with default set to 5 (as per previous behavior). Will not change existing behavior but will allow remote Python API calls to specify a longer (or shorter) timeout if they know that a script takes longer than 5 seconds to return. --- homeassistant/remote.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/homeassistant/remote.py b/homeassistant/remote.py index 6c49decdff2..b2d67811f6e 100644 --- a/homeassistant/remote.py +++ b/homeassistant/remote.py @@ -75,7 +75,7 @@ class API(object): return self.status == APIStatus.OK - def __call__(self, method, path, data=None): + def __call__(self, method, path, data=None, timeout=5): """Make a call to the Home Assistant API.""" if data is not None: data = json.dumps(data, cls=JSONEncoder) @@ -85,10 +85,11 @@ class API(object): try: if method == METHOD_GET: return requests.get( - url, params=data, timeout=5, headers=self._headers) + url, params=data, timeout=timeout, headers=self._headers) else: return requests.request( - method, url, data=data, timeout=5, headers=self._headers) + method, url, data=data, timeout=timeout, + headers=self._headers) except requests.exceptions.ConnectionError: _LOGGER.exception("Error connecting to server") @@ -510,12 +511,12 @@ def get_services(api): return {} -def call_service(api, domain, service, service_data=None): +def call_service(api, domain, service, service_data=None, timeout=5): """Call a service at the remote API.""" try: req = api(METHOD_POST, URL_API_SERVICES_SERVICE.format(domain, service), - service_data) + service_data, timeout=timeout) if req.status_code != 200: _LOGGER.error("Error calling service: %d - %s",