core/homeassistant/components/camera/foscam.py

72 lines
2.2 KiB
Python
Raw Normal View History

"""
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
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/camera.foscam/
"""
import logging
2015-11-29 22:04:44 +00:00
import requests
2016-08-27 20:43:33 +00:00
import voluptuous as vol
2016-08-27 20:43:33 +00:00
from homeassistant.components.camera import (Camera, PLATFORM_SCHEMA)
from homeassistant.const import (
CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_PORT)
from homeassistant.helpers import config_validation as cv
2015-11-29 22:04:44 +00:00
_LOGGER = logging.getLogger(__name__)
2016-08-27 20:43:33 +00:00
CONF_IP = 'ip'
DEFAULT_NAME = 'Foscam Camera'
DEFAULT_PORT = 88
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_IP): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
})
# pylint: disable=unused-argument
2016-08-27 20:43:33 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-07 16:45:06 +00:00
"""Setup a Foscam IP Camera."""
2016-08-27 20:43:33 +00:00
add_devices([FoscamCamera(config)])
# pylint: disable=too-many-instance-attributes
class FoscamCamera(Camera):
2016-03-07 16:45:06 +00:00
"""An implementation of a Foscam IP camera."""
2016-03-07 19:29:54 +00:00
def __init__(self, device_info):
2016-03-07 19:29:54 +00:00
"""Initialize a Foscam camera."""
2015-09-16 05:40:51 +00:00
super(FoscamCamera, self).__init__()
2015-09-16 05:09:16 +00:00
2016-08-27 20:43:33 +00:00
ip_address = device_info.get(CONF_IP)
port = device_info.get(CONF_PORT)
2015-09-16 05:09:16 +00:00
2016-08-27 20:43:33 +00:00
self._base_url = 'http://{}:{}/'.format(ip_address, port)
self._username = device_info.get(CONF_USERNAME)
self._password = device_info.get(CONF_PASSWORD)
2015-09-16 05:40:51 +00:00
self._snap_picture_url = self._base_url \
+ 'cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=' \
2015-09-16 05:40:51 +00:00
+ self._username + '&pwd=' + self._password
2016-08-27 20:43:33 +00:00
self._name = device_info.get(CONF_NAME)
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):
2016-03-07 16:45:06 +00:00
"""Return a still image reponse from the camera."""
# Send the request to snap a picture and return raw jpg data
2016-06-18 20:06:14 +00:00
response = requests.get(self._snap_picture_url, timeout=10)
return response.content
@property
def name(self):
2016-03-07 16:45:06 +00:00
"""Return the name of this camera."""
return self._name