Use default clientsession to stream synology video (#9959)

This commit is contained in:
Joe Lu 2017-10-18 22:02:43 -07:00 committed by Pascal Vizeli
parent 6ea866c7f7
commit 42f450d4e6

View File

@ -16,8 +16,8 @@ from homeassistant.const import (
from homeassistant.components.camera import ( from homeassistant.components.camera import (
Camera, PLATFORM_SCHEMA) Camera, PLATFORM_SCHEMA)
from homeassistant.helpers.aiohttp_client import ( from homeassistant.helpers.aiohttp_client import (
async_create_clientsession, async_aiohttp_proxy_web,
async_aiohttp_proxy_web) async_get_clientsession)
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['py-synology==0.1.5'] REQUIREMENTS = ['py-synology==0.1.5']
@ -58,13 +58,12 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
return False return False
cameras = surveillance.get_all_cameras() cameras = surveillance.get_all_cameras()
websession = async_create_clientsession(hass, verify_ssl)
# add cameras # add cameras
devices = [] devices = []
for camera in cameras: for camera in cameras:
if not config.get(CONF_WHITELIST): if not config.get(CONF_WHITELIST):
device = SynologyCamera(websession, surveillance, camera.camera_id) device = SynologyCamera(surveillance, camera.camera_id, verify_ssl)
devices.append(device) devices.append(device)
async_add_devices(devices) async_add_devices(devices)
@ -73,12 +72,12 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class SynologyCamera(Camera): class SynologyCamera(Camera):
"""An implementation of a Synology NAS based IP camera.""" """An implementation of a Synology NAS based IP camera."""
def __init__(self, websession, surveillance, camera_id): def __init__(self, surveillance, camera_id, verify_ssl):
"""Initialize a Synology Surveillance Station camera.""" """Initialize a Synology Surveillance Station camera."""
super().__init__() super().__init__()
self._websession = websession
self._surveillance = surveillance self._surveillance = surveillance
self._camera_id = camera_id self._camera_id = camera_id
self._verify_ssl = verify_ssl
self._camera = self._surveillance.get_camera(camera_id) self._camera = self._surveillance.get_camera(camera_id)
self._motion_setting = self._surveillance.get_motion_setting(camera_id) self._motion_setting = self._surveillance.get_motion_setting(camera_id)
self.is_streaming = self._camera.is_enabled self.is_streaming = self._camera.is_enabled
@ -91,7 +90,9 @@ class SynologyCamera(Camera):
def handle_async_mjpeg_stream(self, request): def handle_async_mjpeg_stream(self, request):
"""Return a MJPEG stream image response directly from the camera.""" """Return a MJPEG stream image response directly from the camera."""
streaming_url = self._camera.video_stream_url streaming_url = self._camera.video_stream_url
stream_coro = self._websession.get(streaming_url)
websession = async_get_clientsession(self.hass, self._verify_ssl)
stream_coro = websession.get(streaming_url)
yield from async_aiohttp_proxy_web(self.hass, request, stream_coro) yield from async_aiohttp_proxy_web(self.hass, request, stream_coro)