diff --git a/homeassistant/components/camera/generic.py b/homeassistant/components/camera/generic.py index 91f44a2a230..91712931c07 100644 --- a/homeassistant/components/camera/generic.py +++ b/homeassistant/components/camera/generic.py @@ -7,13 +7,16 @@ https://home-assistant.io/components/camera.generic/ import logging import requests -from requests.auth import HTTPBasicAuth +from requests.auth import HTTPBasicAuth, HTTPDigestAuth from homeassistant.components.camera import DOMAIN, Camera from homeassistant.helpers import validate_config _LOGGER = logging.getLogger(__name__) +BASIC_AUTHENTICATION = 'basic' +DIGEST_AUTHENTICATION = 'digest' + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices_callback, discovery_info=None): @@ -33,6 +36,8 @@ class GenericCamera(Camera): """Initialize a generic camera.""" super().__init__() self._name = device_info.get('name', 'Generic Camera') + self._authentication = device_info.get('authentication', + BASIC_AUTHENTICATION) self._username = device_info.get('username') self._password = device_info.get('password') self._still_image_url = device_info['still_image_url'] @@ -40,10 +45,14 @@ class GenericCamera(Camera): def camera_image(self): """Return a still image response from the camera.""" if self._username and self._password: + if self._authentication == DIGEST_AUTHENTICATION: + auth = HTTPDigestAuth(self._username, self._password) + else: + auth = HTTPBasicAuth(self._username, self._password) try: response = requests.get( self._still_image_url, - auth=HTTPBasicAuth(self._username, self._password), + auth=auth, timeout=10) except requests.exceptions.RequestException as error: _LOGGER.error('Error getting camera image: %s', error) diff --git a/homeassistant/components/camera/mjpeg.py b/homeassistant/components/camera/mjpeg.py index dce8ac30440..16897df315b 100644 --- a/homeassistant/components/camera/mjpeg.py +++ b/homeassistant/components/camera/mjpeg.py @@ -8,7 +8,7 @@ import logging from contextlib import closing import requests -from requests.auth import HTTPBasicAuth +from requests.auth import HTTPBasicAuth, HTTPDigestAuth from homeassistant.components.camera import DOMAIN, Camera from homeassistant.helpers import validate_config @@ -17,6 +17,9 @@ CONTENT_TYPE_HEADER = 'Content-Type' _LOGGER = logging.getLogger(__name__) +BASIC_AUTHENTICATION = 'basic' +DIGEST_AUTHENTICATION = 'digest' + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices_callback, discovery_info=None): @@ -48,6 +51,8 @@ class MjpegCamera(Camera): """Initialize a MJPEG camera.""" super().__init__() self._name = device_info.get('name', 'Mjpeg Camera') + self._authentication = device_info.get('authentication', + BASIC_AUTHENTICATION) self._username = device_info.get('username') self._password = device_info.get('password') self._mjpeg_url = device_info['mjpeg_url'] @@ -55,9 +60,12 @@ class MjpegCamera(Camera): def camera_stream(self): """Return a MJPEG stream image response directly from the camera.""" if self._username and self._password: + if self._authentication == DIGEST_AUTHENTICATION: + auth = HTTPDigestAuth(self._username, self._password) + else: + auth = HTTPBasicAuth(self._username, self._password) return requests.get(self._mjpeg_url, - auth=HTTPBasicAuth(self._username, - self._password), + auth=auth, stream=True, timeout=10) else: return requests.get(self._mjpeg_url, stream=True, timeout=10)