core/homeassistant/components/camera/generic.py

57 lines
1.8 KiB
Python
Raw Normal View History

2015-06-05 12:51:29 +00:00
"""
homeassistant.components.camera.generic
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-06-05 12:51:29 +00:00
Support for IP Cameras.
2015-10-13 19:00:28 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/camera.generic.html
2015-06-05 12:51:29 +00:00
"""
import logging
2015-07-10 08:03:46 +00:00
from requests.auth import HTTPBasicAuth
2015-06-05 12:51:29 +00:00
from homeassistant.helpers import validate_config
from homeassistant.components.camera import DOMAIN
from homeassistant.components.camera import Camera
import requests
_LOGGER = logging.getLogger(__name__)
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2015-07-11 06:17:12 +00:00
""" Adds a generic IP Camera. """
2015-07-11 06:17:26 +00:00
if not validate_config({DOMAIN: config}, {DOMAIN: ['still_image_url']},
_LOGGER):
2015-06-05 12:51:29 +00:00
return None
2015-07-11 06:17:12 +00:00
add_devices_callback([GenericCamera(config)])
2015-06-05 12:51:29 +00:00
# pylint: disable=too-many-instance-attributes
class GenericCamera(Camera):
"""
2015-07-11 06:17:12 +00:00
A generic implementation of an IP camera that is reachable over a URL.
2015-06-05 12:51:29 +00:00
"""
2015-07-11 06:17:12 +00:00
def __init__(self, device_info):
super().__init__()
self._name = device_info.get('name', 'Generic Camera')
2015-06-05 12:51:29 +00:00
self._username = device_info.get('username')
self._password = device_info.get('password')
2015-07-11 06:17:26 +00:00
self._still_image_url = device_info['still_image_url']
2015-07-11 06:17:12 +00:00
def camera_image(self):
""" Return a still image reponse from the camera. """
2015-07-11 06:17:12 +00:00
if self._username and self._password:
2015-07-10 08:03:46 +00:00
response = requests.get(
2015-07-11 06:17:12 +00:00
self._still_image_url,
2015-07-11 06:17:26 +00:00
auth=HTTPBasicAuth(self._username, self._password))
2015-07-10 08:03:46 +00:00
else:
2015-07-11 06:17:12 +00:00
response = requests.get(self._still_image_url)
2015-06-05 12:51:29 +00:00
return response.content
@property
def name(self):
""" Return the name of this device. """
2015-07-11 06:17:12 +00:00
return self._name