core/homeassistant/components/camera/foscam.py

98 lines
3.0 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
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__)
Fixing foscam library dependency/requirements (#9387) * 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 * Updating the foscam library dependency/requirements. * Fixing the requirements_all file. Somehow when i generated, it generated duplicate entry for the same dependency
2017-09-12 04:43:55 +00:00
REQUIREMENTS = ['libpyfoscam==1.0']
2016-08-27 20:43:33 +00:00
CONF_IP = 'ip'
DEFAULT_NAME = 'Foscam Camera'
DEFAULT_PORT = 88
FOSCAM_COMM_ERROR = -8
2016-08-27 20:43:33 +00:00
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):
"""Set up a Foscam IP Camera."""
add_devices([FoscamCam(config)])
class FoscamCam(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."""
from libpyfoscam import FoscamCamera
super(FoscamCam, 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)
self._username = device_info.get(CONF_USERNAME)
self._password = device_info.get(CONF_PASSWORD)
self._name = device_info.get(CONF_NAME)
self._motion_status = False
2015-09-16 05:09:16 +00:00
self._foscam_session = FoscamCamera(
ip_address, port, self._username, self._password, verbose=False)
def camera_image(self):
"""Return a still image response 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
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."""
try:
ret = self._foscam_session.enable_motion_detection()
self._motion_status = ret == FOSCAM_COMM_ERROR
except TypeError:
_LOGGER.debug("Communication problem")
self._motion_status = False
def disable_motion_detection(self):
"""Disable motion detection."""
try:
ret = self._foscam_session.disable_motion_detection()
self._motion_status = ret == FOSCAM_COMM_ERROR
except TypeError:
_LOGGER.debug("Communication problem")
self._motion_status = False
@property
def name(self):
2016-03-07 16:45:06 +00:00
"""Return the name of this camera."""
return self._name