Enable/Disable Motion detection for Foscam Cameras (#8582)
* Added support to enable/disable motion detection for foscam cameras. This support was added in 0.48.1 as a generic service for cameras. Motion detection can be enabled/disabled for foscam cameras with this code-set. * Fixed the violation identified by hound-bot * Fixed the comment posted by HoundCI-Bot regarding using imperative mood statement for pydocstyle * Fixed the error that travis-ci bot found. * As per comment from @balloob, Instead of directly using the URL to talk to foscam, used a 3rd party foscam library to communicate with it. This library already has support to enable/disable motion detection and also APIs to change the motion detection schedule etc. Need to add more support in the pyfoscam 3rd party library for checking if motion was detected or even if sound was detected. Once that is done, we can add that into HASS as well. * Lint * Removed the requests library import which is not used anymore * Updating requirements_all.txt based on the code-base of home assistant that i have. Generated using the gen_requirements_all.py script * Updating requirements_all.txt and requirements_test_all.txt generated by gen_requirements_all.py after latest pull from origin/dev * Updated requirements_all.txt with script * Updated the foscam camera code to fix lint errors * Fixed houndci violation
This commit is contained in:
parent
cb292a0b18
commit
e40388e7ad
2 changed files with 41 additions and 21 deletions
|
@ -6,7 +6,6 @@ https://home-assistant.io/components/camera.foscam/
|
|||
"""
|
||||
import logging
|
||||
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.camera import (Camera, PLATFORM_SCHEMA)
|
||||
|
@ -16,11 +15,15 @@ from homeassistant.helpers import config_validation as cv
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
REQUIREMENTS = ['pyfoscam==1.2']
|
||||
|
||||
CONF_IP = 'ip'
|
||||
|
||||
DEFAULT_NAME = 'Foscam Camera'
|
||||
DEFAULT_PORT = 88
|
||||
|
||||
FOSCAM_COMM_ERROR = -8
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_IP): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
|
@ -33,46 +36,60 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up a Foscam IP Camera."""
|
||||
add_devices([FoscamCamera(config)])
|
||||
add_devices([FoscamCam(config)])
|
||||
|
||||
|
||||
class FoscamCamera(Camera):
|
||||
class FoscamCam(Camera):
|
||||
"""An implementation of a Foscam IP camera."""
|
||||
|
||||
def __init__(self, device_info):
|
||||
"""Initialize a Foscam camera."""
|
||||
super(FoscamCamera, self).__init__()
|
||||
super(FoscamCam, 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 \
|
||||
+ 'cgi-bin/CGIProxy.fcgi?' \
|
||||
+ 'cmd=snapPicture2&usr={}&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)
|
||||
self._motion_status = False
|
||||
|
||||
_LOGGER.info("Using the following URL for %s: %s",
|
||||
self._name, uri_template.format('***', '***'))
|
||||
from foscam import FoscamCamera
|
||||
|
||||
self._foscam_session = FoscamCamera(ip_address, port, self._username,
|
||||
self._password)
|
||||
|
||||
def camera_image(self):
|
||||
"""Return a still image reponse from the camera."""
|
||||
# Send the request to snap a picture and return raw jpg data
|
||||
# Handle exception if host is not reachable or url failed
|
||||
try:
|
||||
response = requests.get(self._snap_picture_url, timeout=10)
|
||||
except requests.exceptions.ConnectionError:
|
||||
result, response = self._foscam_session.snap_picture_2()
|
||||
if result == FOSCAM_COMM_ERROR:
|
||||
return None
|
||||
|
||||
return response
|
||||
|
||||
@property
|
||||
def motion_detection_enabled(self):
|
||||
"""Camera Motion Detection Status."""
|
||||
return self._motion_status
|
||||
|
||||
def enable_motion_detection(self):
|
||||
"""Enable motion detection in camera."""
|
||||
ret, err = self._foscam_session.enable_motion_detection()
|
||||
if ret == FOSCAM_COMM_ERROR:
|
||||
_LOGGER.debug("Unable to communicate with Foscam Camera: %s", err)
|
||||
self._motion_status = True
|
||||
else:
|
||||
return response.content
|
||||
self._motion_status = False
|
||||
|
||||
def disable_motion_detection(self):
|
||||
"""Disable motion detection."""
|
||||
ret, err = self._foscam_session.disable_motion_detection()
|
||||
if ret == FOSCAM_COMM_ERROR:
|
||||
_LOGGER.debug("Unable to communicate with Foscam Camera: %s", err)
|
||||
self._motion_status = True
|
||||
else:
|
||||
self._motion_status = False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
|
|
@ -577,6 +577,9 @@ pyfido==1.0.1
|
|||
# homeassistant.components.climate.flexit
|
||||
pyflexit==0.3
|
||||
|
||||
# homeassistant.components.camera.foscam
|
||||
pyfoscam==1.2
|
||||
|
||||
# homeassistant.components.ifttt
|
||||
pyfttt==0.3
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue