2015-09-16 03:58:46 +00:00
|
|
|
"""
|
|
|
|
Support for Foscam IP Cameras.
|
|
|
|
|
|
|
|
This component provides basic support for Foscam IP cameras.
|
|
|
|
|
|
|
|
As part of the basic support the following features will be provided:
|
|
|
|
-MJPEG video streaming
|
|
|
|
|
|
|
|
To use this component, add the following to your config/configuration.yaml:
|
|
|
|
|
|
|
|
camera:
|
|
|
|
platform: foscam
|
|
|
|
name: Door Camera
|
2015-09-16 04:32:55 +00:00
|
|
|
ip: 192.168.0.123
|
|
|
|
port: 88
|
|
|
|
username: visitor
|
|
|
|
password: password
|
|
|
|
|
|
|
|
camera 2:
|
|
|
|
name: 'Second Camera'
|
|
|
|
...
|
|
|
|
camera 3:
|
|
|
|
name: 'Camera Three'
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
VARIABLES:
|
|
|
|
|
|
|
|
These are the variables for the device_data array:
|
|
|
|
|
|
|
|
ip
|
|
|
|
*Required
|
|
|
|
The IP address of your foscam device
|
|
|
|
|
|
|
|
username
|
|
|
|
*Required
|
|
|
|
THe username of a visitor or operator of your camera. Oddly admin accounts don't seem to have access to take snapshots
|
|
|
|
|
|
|
|
password
|
|
|
|
*Required
|
|
|
|
the password for accessing your camera
|
|
|
|
|
|
|
|
name
|
|
|
|
*Optional
|
|
|
|
This parameter allows you to override the name of your camera in homeassistant
|
|
|
|
|
|
|
|
port
|
|
|
|
*Optional
|
|
|
|
The port that the camera is running on. The default is 88.
|
2015-09-16 03:58:46 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.components.camera import DOMAIN
|
|
|
|
from homeassistant.components.camera import Camera
|
|
|
|
import requests
|
|
|
|
import re
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|
|
|
""" Adds a generic IP Camera. """
|
2015-09-16 04:32:55 +00:00
|
|
|
if not validate_config({DOMAIN: config}, {DOMAIN: ['username', 'password', 'ip']}, _LOGGER):
|
2015-09-16 03:58:46 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
add_devices_callback([FoscamCamera(config)])
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
class FoscamCamera(Camera):
|
|
|
|
"""
|
2015-09-16 04:32:55 +00:00
|
|
|
An implementation of a Foscam IP camera.
|
2015-09-16 03:58:46 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, device_info):
|
|
|
|
super().__init__()
|
|
|
|
self._name = device_info.get('name', 'Foscam Camera')
|
|
|
|
self._username = device_info.get('username')
|
|
|
|
self._password = device_info.get('password')
|
2015-09-16 04:32:55 +00:00
|
|
|
|
|
|
|
port = device_info.get('port', 88)
|
|
|
|
|
|
|
|
self._base_url = 'http://' + device_info.get('ip') + ':' + str(port) + '/'
|
2015-09-16 03:58:46 +00:00
|
|
|
self._snap_picture_url = self._base_url + 'cgi-bin/CGIProxy.fcgi?cmd=snapPicture&usr=' + self._username + '&pwd=' + self._password
|
2015-09-16 04:32:55 +00:00
|
|
|
_LOGGER.info('Using the following URL for Foscam camera: ' + self._snap_picture_url)
|
2015-09-16 03:58:46 +00:00
|
|
|
|
|
|
|
def camera_image(self):
|
|
|
|
""" Return a still image reponse from the camera """
|
|
|
|
|
2015-09-16 04:32:55 +00:00
|
|
|
# send the request to snap a picture
|
2015-09-16 03:58:46 +00:00
|
|
|
response = requests.get(self._snap_picture_url)
|
2015-09-16 04:32:55 +00:00
|
|
|
|
|
|
|
# parse the response to find the image file name
|
2015-09-16 03:58:46 +00:00
|
|
|
pattern = re.compile('src="\.\.\/(.*\.jpg)"')
|
|
|
|
filename = pattern.search(response.content.decode("utf-8") ).group(1)
|
|
|
|
|
2015-09-16 04:32:55 +00:00
|
|
|
# send request for the image
|
2015-09-16 03:58:46 +00:00
|
|
|
response = requests.get(self._base_url + filename)
|
|
|
|
|
|
|
|
return response.content
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Return the name of this device """
|
|
|
|
return self._name
|