Skip to content

Instantly share code, notes, and snippets.

@xlcnd xlcnd/foscamsd.py
Last active Dec 19, 2018

Embed
What would you like to do?
A component for Home Assistant (home-assistant.io) to support old SD FOSCAM IP Cameras.
"""
This component provides basic support for Foscam (SD) IP cameras.
NOTE: Now (2018) it is better to use these cameras as generic ip cameras!
Use as still_image_url: http://YOUR_CAM_IP:88/snapshot.cgi?user=USER&pwd=PASSWORD
TIP: You can record locally by using the following shell command:
timeout 120 wget http://YOUR_CAM_IP:88/videostream.asf?user=USER\&pwd=PASSWORD -O cam1_$(date +%F_%T).asf
it records 120 seconds of video.
The .asf format is a Windows Media Player format and can be played by almost every media player.
Use VLC or ffmpeg to transform it into another format, e.g., .mp4.
INSTALL:
1. Create the file <config dir>/custom_components/camera/foscamsd.py and copy
the code below to there.
2. In your <config dir>/configuration.yaml enter:
camera:
platform: foscamsd
ip: IP_ADDRESS
username: USERNAME
password: PASSWORD
CONFIGURATION VARIABLES:
-----------------------
ip
(string)(Required) The IP address your camera.
port
(integer)(Optional) The port that the camera is running on.
Default value: 88
username
(string)(Required) The username for accessing your camera.
password
(string)(Required) The password for accessing your camera.
name
(string)(Optional) This parameter allows you to override the name of your camera.
"""
import logging
import requests
import voluptuous as vol
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
_LOGGER = logging.getLogger(__name__)
CONF_IP = 'ip'
DEFAULT_NAME = 'Foscam Camera (SD)'
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
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup a Foscam IP Camera (SD)."""
add_devices([FoscamSDCamera(config)])
class FoscamSDCamera(Camera):
"""An implementation of a Foscam IP camera (SD)."""
def __init__(self, device_info):
"""Initialize a Foscam camera."""
super(FoscamSDCamera, self).__init__()
ip_address = device_info.get(CONF_IP)
port = device_info.get(CONF_PORT)
self._base_url = 'http://{}:{}/'.format(ip_address, port)
uri_template = self._base_url \
+ 'snapshot.cgi?user={}&pwd={}'
self._username = device_info.get(CONF_USERNAME)
self._password = device_info.get(CONF_PASSWORD)
self._snap_picture_url = uri_template.format(
self._username,
self._password
)
self._name = device_info.get(CONF_NAME)
_LOGGER.info('Using the following URL for %s: %s',
self._name, uri_template.format('***', '***'))
def camera_image(self):
"""Return a still image reponse from the camera."""
# Send the request to snap a picture and return raw jpg data
try:
response = requests.get(self._snap_picture_url, timeout=10)
except requests.exceptions.ConnectionError:
return None
else:
return response.content
@property
def name(self):
"""Return the name of this camera."""
return self._name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.