Adding Digest Auth for webcam image retrieval (#2821)

* Adding Digest Auth for webcam image retrieval

* Update generic.py

* Update mjpeg.py

* Update generic.py

* Update mjpeg.py

* Update generic.py

* Update mjpeg.py
This commit is contained in:
Matthias Grawinkel 2016-08-17 19:08:47 +02:00 committed by Fabian Affolter
parent 2237189c86
commit 8a3c511a04
2 changed files with 22 additions and 5 deletions

View file

@ -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)