core/homeassistant/components/foscam/camera.py

118 lines
3.7 KiB
Python
Raw Normal View History

"""This component provides basic support for Foscam IP cameras."""
import logging
2015-11-29 22:04:44 +00:00
2016-08-27 20:43:33 +00:00
import voluptuous as vol
2019-07-31 19:25:30 +00:00
from homeassistant.components.camera import Camera, PLATFORM_SCHEMA, SUPPORT_STREAM
from homeassistant.const import CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_PORT
2016-08-27 20:43:33 +00:00
from homeassistant.helpers import config_validation as cv
2015-11-29 22:04:44 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_IP = "ip"
CONF_RTSP_PORT = "rtsp_port"
2016-08-27 20:43:33 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Foscam Camera"
2016-08-27 20:43:33 +00:00
DEFAULT_PORT = 88
FOSCAM_COMM_ERROR = -8
2019-07-31 19:25:30 +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,
vol.Optional(CONF_RTSP_PORT): cv.port,
}
)
2016-08-27 20:43:33 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up a Foscam IP Camera."""
add_entities([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().__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(
2019-07-31 19:25:30 +00:00
ip_address, port, self._username, self._password, verbose=False
)
self._rtsp_port = device_info.get(CONF_RTSP_PORT)
if not self._rtsp_port:
result, response = self._foscam_session.get_port_info()
if result == 0:
2019-07-31 19:25:30 +00:00
self._rtsp_port = response.get("rtspPort") or response.get("mediaPort")
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 supported_features(self):
"""Return supported features."""
if self._rtsp_port:
return SUPPORT_STREAM
return 0
async def stream_source(self):
"""Return the stream source."""
if self._rtsp_port:
2019-07-31 19:25:30 +00:00
return "rtsp://{}:{}@{}:{}/videoMain".format(
self._username,
self._password,
self._foscam_session.host,
2019-07-31 19:25:30 +00:00
self._rtsp_port,
)
return None
@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