core/homeassistant/components/camera/generic.py

92 lines
2.6 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-07-11 06:17:12 +00:00
This component provides basic support for IP cameras. For the basic support to
work you camera must support accessing a JPEG snapshot via a URL and you will
need to specify the "still_image_url" parameter which should be the location of
the JPEG image.
2015-06-05 12:51:29 +00:00
As part of the basic support the following features will be provided:
- MJPEG video streaming
- Saving a snapshot
- Recording(JPEG frame capture)
2015-06-05 12:51:29 +00:00
To use this component, add the following to your configuration.yaml file.
2015-06-05 12:51:29 +00:00
camera:
platform: generic
name: Door Camera
username: YOUR_USERNAME
password: YOUR_PASSWORD
2015-07-11 06:17:12 +00:00
still_image_url: http://YOUR_CAMERA_IP_AND_PORT/image.jpg
2015-06-05 12:51:29 +00:00
Variables:
2015-06-05 12:51:29 +00:00
2015-07-11 06:17:12 +00:00
still_image_url
2015-06-05 12:51:29 +00:00
*Required
The URL your camera serves the image on, eg. http://192.168.1.21:2112/
2015-06-05 12:51:29 +00:00
name
*Optional
This parameter allows you to override the name of your camera in Home
Assistant.
2015-06-05 12:51:29 +00:00
username
2015-07-11 06:17:12 +00:00
*Optional
The username for accessing your camera.
2015-06-05 12:51:29 +00:00
password
2015-07-11 06:17:12 +00:00
*Optional
The password for accessing your camera.
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