core/homeassistant/components/camera/foscam.py

70 lines
2.3 KiB
Python
Raw Normal View History

"""
2015-09-17 06:34:10 +00:00
homeassistant.components.camera.foscam
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-09-16 05:09:16 +00:00
This component provides basic support for Foscam IP cameras.
2015-09-17 06:34:10 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/camera.foscam.html
"""
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):
2015-09-16 04:45:12 +00:00
""" Adds a Foscam IP Camera. """
2015-09-16 05:09:16 +00:00
if not validate_config({DOMAIN: config},
{DOMAIN: ['username', 'password', 'ip']}, _LOGGER):
return None
add_devices_callback([FoscamCamera(config)])
# pylint: disable=too-many-instance-attributes
class FoscamCamera(Camera):
2015-09-17 06:34:10 +00:00
""" An implementation of a Foscam IP camera. """
def __init__(self, device_info):
2015-09-16 05:40:51 +00:00
super(FoscamCamera, self).__init__()
2015-09-16 05:09:16 +00:00
2015-09-16 05:40:51 +00:00
ip_address = device_info.get('ip')
2015-09-16 04:45:12 +00:00
port = device_info.get('port', 88)
2015-09-16 05:09:16 +00:00
2015-09-16 05:40:51 +00:00
self._base_url = 'http://' + ip_address + ':' + str(port) + '/'
self._username = device_info.get('username')
self._password = device_info.get('password')
2015-09-16 05:40:51 +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:45:12 +00:00
self._name = device_info.get('name', 'Foscam Camera')
2015-09-16 05:09:16 +00:00
_LOGGER.info('Using the following URL for %s: %s',
self._name, self._snap_picture_url)
def camera_image(self):
2015-09-17 06:34:10 +00:00
""" Return a still image reponse from the camera. """
# send the request to snap a picture
response = requests.get(self._snap_picture_url)
# parse the response to find the image file name
2015-09-16 05:40:51 +00:00
pattern = re.compile('src="[.][.]/(.*[.]jpg)"')
2015-09-16 05:09:16 +00:00
filename = pattern.search(response.content.decode("utf-8")).group(1)
# send request for the image
response = requests.get(self._base_url + filename)
return response.content
@property
def name(self):
2015-09-17 06:34:10 +00:00
""" Return the name of this device. """
return self._name