hass-core/homeassistant/components/camera/generic.py

95 lines
2.6 KiB
Python
Raw Normal View History

2015-06-05 22:51:29 +10:00
"""
Support for IP Cameras.
2015-07-10 23:17:12 -07: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 22:51:29 +10:00
As part of the basic support the following features will be provided:
-MJPEG video streaming
-Saving a snapshot
-Recording(JPEG frame capture)
2015-07-10 23:17:12 -07:00
To use this component, add the following to your config/configuration.yaml:
2015-06-05 22:51:29 +10:00
camera:
platform: generic
name: Door Camera
username: YOUR_USERNAME
password: YOUR_PASSWORD
2015-07-10 23:17:12 -07:00
still_image_url: http://YOUR_CAMERA_IP_AND_PORT/image.jpg
2015-06-05 22:51:29 +10:00
VARIABLES:
These are the variables for the device_data array:
2015-07-10 23:17:12 -07:00
still_image_url
2015-06-05 22:51:29 +10:00
*Required
2015-07-10 23:17:12 -07:00
The URL your camera serves the image on.
2015-06-05 22:51:29 +10:00
Example: http://192.168.1.21:2112/
name
*Optional
This parameter allows you to override the name of your camera in homeassistant
username
2015-07-10 23:17:12 -07:00
*Optional
2015-06-05 22:51:29 +10:00
THe username for acessing your camera
password
2015-07-10 23:17:12 -07:00
*Optional
2015-06-05 22:51:29 +10:00
the password for accessing your camera
"""
import logging
2015-07-10 18:03:46 +10:00
from requests.auth import HTTPBasicAuth
2015-06-05 22:51:29 +10: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-10 23:17:12 -07:00
""" Adds 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):
"""
2015-07-10 23:17:12 -07:00
A generic implementation of an IP camera that is reachable over a URL.
2015-06-05 22:51:29 +10:00
"""
2015-07-10 23:17:12 -07:00
def __init__(self, device_info):
super().__init__()
self._name = device_info.get('name', 'Generic Camera')
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):
2015-06-05 22:51:29 +10:00
""" Return a still image reponse from the camera """
2015-07-10 23:17:12 -07:00
if self._username and self._password:
2015-07-10 18:03:46 +10:00
response = requests.get(
2015-07-10 23:17:12 -07:00
self._still_image_url,
2015-07-10 23:17:26 -07:00
auth=HTTPBasicAuth(self._username, self._password))
2015-07-10 18:03:46 +10:00
else:
2015-07-10 23:17:12 -07:00
response = requests.get(self._still_image_url)
2015-06-05 22:51:29 +10:00
return response.content
@property
def name(self):
""" Return the name of this device """
2015-07-10 23:17:12 -07:00
return self._name