From 85e0db6ade537e2f910d3e4274edc414ebf3109a Mon Sep 17 00:00:00 2001 From: Nolan Gilley Date: Fri, 13 Nov 2015 13:55:22 -0500 Subject: [PATCH 1/3] add exception handling to generic camera requests function. --- homeassistant/components/camera/__init__.py | 30 ++++++++++++--------- homeassistant/components/camera/generic.py | 16 ++++++++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index 01c6c1b6e03..c44f56ed9f5 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -87,7 +87,10 @@ def setup(hass, config): if camera: response = camera.camera_image() - handler.wfile.write(response) + if response is not None: + handler.wfile.write(response) + else: + handler.send_response(HTTP_NOT_FOUND) else: handler.send_response(HTTP_NOT_FOUND) @@ -129,20 +132,21 @@ def setup(hass, config): while True: img_bytes = camera.camera_image() + if img_bytes is not None: + headers_str = '\r\n'.join(( + 'Content-length: {}'.format(len(img_bytes)), + 'Content-type: image/jpeg', + )) + '\r\n\r\n' - headers_str = '\r\n'.join(( - 'Content-length: {}'.format(len(img_bytes)), - 'Content-type: image/jpeg', - )) + '\r\n\r\n' - - handler.request.sendall( - bytes(headers_str, 'utf-8') + - img_bytes + - bytes('\r\n', 'utf-8')) - - handler.request.sendall( - bytes('--jpgboundary\r\n', 'utf-8')) + handler.request.sendall( + bytes(headers_str, 'utf-8') + + img_bytes + + bytes('\r\n', 'utf-8')) + handler.request.sendall( + bytes('--jpgboundary\r\n', 'utf-8')) + else: + break except (requests.RequestException, IOError): camera.is_streaming = False camera.update_ha_state() diff --git a/homeassistant/components/camera/generic.py b/homeassistant/components/camera/generic.py index 55fa4ec913f..b8be51292bf 100644 --- a/homeassistant/components/camera/generic.py +++ b/homeassistant/components/camera/generic.py @@ -42,11 +42,19 @@ class GenericCamera(Camera): def camera_image(self): """ Return a still image reponse from the camera. """ if self._username and self._password: - response = requests.get( - self._still_image_url, - auth=HTTPBasicAuth(self._username, self._password)) + try: + response = requests.get( + self._still_image_url, + auth=HTTPBasicAuth(self._username, self._password)) + except requests.exceptions.RequestException as error: + _LOGGER.error('Error getting camera image: %s', error) + return None else: - response = requests.get(self._still_image_url) + try: + response = requests.get(self._still_image_url) + except requests.exceptions.RequestException as error: + _LOGGER.error('Error getting camera image: %s', error) + return None return response.content From 9acb341b9607dd1dd404378a324fdcd10b6e2452 Mon Sep 17 00:00:00 2001 From: Nolan Gilley Date: Sat, 14 Nov 2015 10:51:07 -0500 Subject: [PATCH 2/3] remove break --- homeassistant/components/camera/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index c44f56ed9f5..77a8eb83ce1 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -132,7 +132,9 @@ def setup(hass, config): while True: img_bytes = camera.camera_image() - if img_bytes is not None: + if img_bytes is None: + continue + else: headers_str = '\r\n'.join(( 'Content-length: {}'.format(len(img_bytes)), 'Content-type: image/jpeg', @@ -145,8 +147,7 @@ def setup(hass, config): handler.request.sendall( bytes('--jpgboundary\r\n', 'utf-8')) - else: - break + except (requests.RequestException, IOError): camera.is_streaming = False camera.update_ha_state() From df264f2ec00dc84a0a7ca637c568a3273f55fd03 Mon Sep 17 00:00:00 2001 From: Nolan Gilley Date: Sat, 14 Nov 2015 15:49:39 -0500 Subject: [PATCH 3/3] remove unnecessary else --- homeassistant/components/camera/__init__.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index 77a8eb83ce1..ff5198b7ab1 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -134,19 +134,18 @@ def setup(hass, config): img_bytes = camera.camera_image() if img_bytes is None: continue - else: - headers_str = '\r\n'.join(( - 'Content-length: {}'.format(len(img_bytes)), - 'Content-type: image/jpeg', - )) + '\r\n\r\n' + headers_str = '\r\n'.join(( + 'Content-length: {}'.format(len(img_bytes)), + 'Content-type: image/jpeg', + )) + '\r\n\r\n' - handler.request.sendall( - bytes(headers_str, 'utf-8') + - img_bytes + - bytes('\r\n', 'utf-8')) + handler.request.sendall( + bytes(headers_str, 'utf-8') + + img_bytes + + bytes('\r\n', 'utf-8')) - handler.request.sendall( - bytes('--jpgboundary\r\n', 'utf-8')) + handler.request.sendall( + bytes('--jpgboundary\r\n', 'utf-8')) except (requests.RequestException, IOError): camera.is_streaming = False