2016-11-30 21:07:17 +00:00
|
|
|
"""
|
|
|
|
This component provides basic support for Amcrest IP cameras.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/camera.amcrest/
|
|
|
|
"""
|
2017-01-15 19:37:24 +00:00
|
|
|
import asyncio
|
2016-11-30 21:07:17 +00:00
|
|
|
import logging
|
2016-12-03 19:46:04 +00:00
|
|
|
|
2017-07-11 08:10:10 +00:00
|
|
|
from homeassistant.components.amcrest import (
|
|
|
|
STREAM_SOURCE_LIST, TIMEOUT)
|
|
|
|
from homeassistant.components.camera import Camera
|
2017-05-18 08:06:24 +00:00
|
|
|
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
2017-01-19 17:55:27 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import (
|
2017-05-18 08:06:24 +00:00
|
|
|
async_get_clientsession, async_aiohttp_proxy_web,
|
|
|
|
async_aiohttp_proxy_stream)
|
2016-11-30 21:07:17 +00:00
|
|
|
|
2017-07-11 08:10:10 +00:00
|
|
|
DEPENDENCIES = ['amcrest', 'ffmpeg']
|
2016-11-30 21:07:17 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-01-15 19:37:24 +00:00
|
|
|
|
2017-07-11 08:10:10 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
2016-12-03 19:46:04 +00:00
|
|
|
"""Set up an Amcrest IP Camera."""
|
2017-07-11 08:10:10 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
device = discovery_info['device']
|
|
|
|
authentication = discovery_info['authentication']
|
|
|
|
ffmpeg_arguments = discovery_info['ffmpeg_arguments']
|
|
|
|
name = discovery_info['name']
|
|
|
|
resolution = discovery_info['resolution']
|
|
|
|
stream_source = discovery_info['stream_source']
|
|
|
|
|
|
|
|
async_add_devices([
|
|
|
|
AmcrestCam(hass,
|
|
|
|
name,
|
|
|
|
device,
|
|
|
|
authentication,
|
|
|
|
ffmpeg_arguments,
|
|
|
|
stream_source,
|
|
|
|
resolution)], True)
|
2016-11-30 21:07:17 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class AmcrestCam(Camera):
|
|
|
|
"""An implementation of an Amcrest IP camera."""
|
|
|
|
|
2017-07-11 08:10:10 +00:00
|
|
|
def __init__(self, hass, name, camera, authentication,
|
|
|
|
ffmpeg_arguments, stream_source, resolution):
|
2016-11-30 21:07:17 +00:00
|
|
|
"""Initialize an Amcrest camera."""
|
|
|
|
super(AmcrestCam, self).__init__()
|
2017-07-11 08:10:10 +00:00
|
|
|
self._name = name
|
2017-01-22 13:22:25 +00:00
|
|
|
self._camera = camera
|
|
|
|
self._base_url = self._camera.get_base_url()
|
2017-05-18 08:06:24 +00:00
|
|
|
self._ffmpeg = hass.data[DATA_FFMPEG]
|
2017-07-11 08:10:10 +00:00
|
|
|
self._ffmpeg_arguments = ffmpeg_arguments
|
|
|
|
self._stream_source = stream_source
|
|
|
|
self._resolution = resolution
|
|
|
|
self._token = self._auth = authentication
|
2016-11-30 21:07:17 +00:00
|
|
|
|
|
|
|
def camera_image(self):
|
|
|
|
"""Return a still image reponse from the camera."""
|
|
|
|
# Send the request to snap a picture and return raw jpg data
|
2017-01-22 13:22:25 +00:00
|
|
|
response = self._camera.snapshot(channel=self._resolution)
|
2016-11-30 21:07:17 +00:00
|
|
|
return response.data
|
|
|
|
|
2017-01-15 19:37:24 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def handle_async_mjpeg_stream(self, request):
|
|
|
|
"""Return an MJPEG stream."""
|
|
|
|
# The snapshot implementation is handled by the parent class
|
|
|
|
if self._stream_source == STREAM_SOURCE_LIST['snapshot']:
|
|
|
|
yield from super().handle_async_mjpeg_stream(request)
|
|
|
|
return
|
|
|
|
|
2017-05-18 08:06:24 +00:00
|
|
|
elif self._stream_source == STREAM_SOURCE_LIST['mjpeg']:
|
|
|
|
# stream an MJPEG image stream directly from the camera
|
|
|
|
websession = async_get_clientsession(self.hass)
|
|
|
|
streaming_url = self._camera.mjpeg_url(typeno=self._resolution)
|
|
|
|
stream_coro = websession.get(
|
|
|
|
streaming_url, auth=self._token, timeout=TIMEOUT)
|
2017-01-15 19:37:24 +00:00
|
|
|
|
2017-05-18 08:06:24 +00:00
|
|
|
yield from async_aiohttp_proxy_web(self.hass, request, stream_coro)
|
2017-01-19 17:55:27 +00:00
|
|
|
|
2017-05-18 08:06:24 +00:00
|
|
|
else:
|
|
|
|
# streaming via fmpeg
|
|
|
|
from haffmpeg import CameraMjpeg
|
|
|
|
|
|
|
|
streaming_url = self._camera.rtsp_url(typeno=self._resolution)
|
|
|
|
stream = CameraMjpeg(self._ffmpeg.binary, loop=self.hass.loop)
|
|
|
|
yield from stream.open_camera(
|
|
|
|
streaming_url, extra_cmd=self._ffmpeg_arguments)
|
|
|
|
|
|
|
|
yield from async_aiohttp_proxy_stream(
|
|
|
|
self.hass, request, stream,
|
|
|
|
'multipart/x-mixed-replace;boundary=ffserver')
|
|
|
|
yield from stream.close()
|
2017-01-15 19:37:24 +00:00
|
|
|
|
2016-11-30 21:07:17 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._name
|