2015-06-05 22:51:29 +10:00
|
|
|
"""
|
|
|
|
Support for IP Cameras.
|
|
|
|
|
2015-10-13 21:00:28 +02:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/camera.generic/
|
2015-06-05 22:51:29 +10:00
|
|
|
"""
|
|
|
|
import logging
|
2015-11-29 13:49:05 -08:00
|
|
|
|
|
|
|
import requests
|
2016-08-17 19:08:47 +02:00
|
|
|
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
2015-11-29 13:49:05 -08:00
|
|
|
|
|
|
|
from homeassistant.components.camera import DOMAIN, Camera
|
2016-02-18 21:27:50 -08:00
|
|
|
from homeassistant.helpers import validate_config
|
2015-06-05 22:51:29 +10:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-17 19:08:47 +02:00
|
|
|
BASIC_AUTHENTICATION = 'basic'
|
|
|
|
DIGEST_AUTHENTICATION = 'digest'
|
|
|
|
|
2015-06-05 22:51:29 +10:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2016-03-07 17:45:06 +01:00
|
|
|
"""Setup a generic IP Camera."""
|
2015-07-10 23:17:26 -07:00
|
|
|
if not validate_config({DOMAIN: config}, {DOMAIN: ['still_image_url']},
|
|
|
|
_LOGGER):
|
2015-06-05 22:51:29 +10:00
|
|
|
return None
|
|
|
|
|
2015-07-10 23:17:12 -07:00
|
|
|
add_devices_callback([GenericCamera(config)])
|
2015-06-05 22:51:29 +10:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
class GenericCamera(Camera):
|
2016-03-07 20:29:54 +01:00
|
|
|
"""A generic implementation of an IP camera."""
|
|
|
|
|
2015-07-10 23:17:12 -07:00
|
|
|
def __init__(self, device_info):
|
2016-03-07 20:29:54 +01:00
|
|
|
"""Initialize a generic camera."""
|
2015-07-10 23:17:12 -07:00
|
|
|
super().__init__()
|
|
|
|
self._name = device_info.get('name', 'Generic Camera')
|
2016-08-17 19:08:47 +02:00
|
|
|
self._authentication = device_info.get('authentication',
|
|
|
|
BASIC_AUTHENTICATION)
|
2015-06-05 22:51:29 +10:00
|
|
|
self._username = device_info.get('username')
|
|
|
|
self._password = device_info.get('password')
|
2015-07-10 23:17:26 -07:00
|
|
|
self._still_image_url = device_info['still_image_url']
|
2015-07-10 23:17:12 -07:00
|
|
|
|
|
|
|
def camera_image(self):
|
2016-03-07 17:45:06 +01:00
|
|
|
"""Return a still image response from the camera."""
|
2015-07-10 23:17:12 -07:00
|
|
|
if self._username and self._password:
|
2016-08-17 19:08:47 +02:00
|
|
|
if self._authentication == DIGEST_AUTHENTICATION:
|
|
|
|
auth = HTTPDigestAuth(self._username, self._password)
|
|
|
|
else:
|
|
|
|
auth = HTTPBasicAuth(self._username, self._password)
|
2015-11-13 13:55:22 -05:00
|
|
|
try:
|
|
|
|
response = requests.get(
|
|
|
|
self._still_image_url,
|
2016-08-17 19:08:47 +02:00
|
|
|
auth=auth,
|
2016-06-18 13:06:14 -07:00
|
|
|
timeout=10)
|
2015-11-13 13:55:22 -05:00
|
|
|
except requests.exceptions.RequestException as error:
|
|
|
|
_LOGGER.error('Error getting camera image: %s', error)
|
|
|
|
return None
|
2015-07-10 18:03:46 +10:00
|
|
|
else:
|
2015-11-13 13:55:22 -05:00
|
|
|
try:
|
2016-06-18 13:06:14 -07:00
|
|
|
response = requests.get(self._still_image_url, timeout=10)
|
2015-11-13 13:55:22 -05:00
|
|
|
except requests.exceptions.RequestException as error:
|
|
|
|
_LOGGER.error('Error getting camera image: %s', error)
|
|
|
|
return None
|
2015-06-05 22:51:29 +10:00
|
|
|
|
|
|
|
return response.content
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 17:45:06 +01:00
|
|
|
"""Return the name of this device."""
|
2015-07-10 23:17:12 -07:00
|
|
|
return self._name
|