mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
add exception handling to generic camera requests function.
This commit is contained in:
parent
d993f4014e
commit
85e0db6ade
@ -87,9 +87,12 @@ def setup(hass, config):
|
|||||||
|
|
||||||
if camera:
|
if camera:
|
||||||
response = camera.camera_image()
|
response = camera.camera_image()
|
||||||
|
if response is not None:
|
||||||
handler.wfile.write(response)
|
handler.wfile.write(response)
|
||||||
else:
|
else:
|
||||||
handler.send_response(HTTP_NOT_FOUND)
|
handler.send_response(HTTP_NOT_FOUND)
|
||||||
|
else:
|
||||||
|
handler.send_response(HTTP_NOT_FOUND)
|
||||||
|
|
||||||
hass.http.register_path(
|
hass.http.register_path(
|
||||||
'GET',
|
'GET',
|
||||||
@ -129,7 +132,7 @@ def setup(hass, config):
|
|||||||
while True:
|
while True:
|
||||||
|
|
||||||
img_bytes = camera.camera_image()
|
img_bytes = camera.camera_image()
|
||||||
|
if img_bytes is not None:
|
||||||
headers_str = '\r\n'.join((
|
headers_str = '\r\n'.join((
|
||||||
'Content-length: {}'.format(len(img_bytes)),
|
'Content-length: {}'.format(len(img_bytes)),
|
||||||
'Content-type: image/jpeg',
|
'Content-type: image/jpeg',
|
||||||
@ -142,7 +145,8 @@ def setup(hass, config):
|
|||||||
|
|
||||||
handler.request.sendall(
|
handler.request.sendall(
|
||||||
bytes('--jpgboundary\r\n', 'utf-8'))
|
bytes('--jpgboundary\r\n', 'utf-8'))
|
||||||
|
else:
|
||||||
|
break
|
||||||
except (requests.RequestException, IOError):
|
except (requests.RequestException, IOError):
|
||||||
camera.is_streaming = False
|
camera.is_streaming = False
|
||||||
camera.update_ha_state()
|
camera.update_ha_state()
|
||||||
|
@ -42,11 +42,19 @@ class GenericCamera(Camera):
|
|||||||
def camera_image(self):
|
def camera_image(self):
|
||||||
""" Return a still image reponse from the camera. """
|
""" Return a still image reponse from the camera. """
|
||||||
if self._username and self._password:
|
if self._username and self._password:
|
||||||
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
self._still_image_url,
|
self._still_image_url,
|
||||||
auth=HTTPBasicAuth(self._username, self._password))
|
auth=HTTPBasicAuth(self._username, self._password))
|
||||||
|
except requests.exceptions.RequestException as error:
|
||||||
|
_LOGGER.error('Error getting camera image: %s', error)
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
response = requests.get(self._still_image_url)
|
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
|
return response.content
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user