ONVIF Camera added Error handling and rtsp authentication. (#11129)

Bugfixes for several issues with hass.io and non-venv installations

Added passing of credentials to RTSP stream

Changed from ONVIFService to ONVIFCamera as ONVIFService didn't contain
the same error handling.

Changed method to get Stream URL from camera to a more compatible method

Added extra Error handling
This commit is contained in:
Matthew Hilton 2018-02-17 13:57:05 +00:00 committed by Phil Frost
parent fab991bbf6
commit 66dcb6c947

View File

@ -6,7 +6,6 @@ https://home-assistant.io/components/camera.onvif/
""" """
import asyncio import asyncio
import logging import logging
import os
import voluptuous as vol import voluptuous as vol
@ -48,30 +47,40 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up a ONVIF camera.""" """Set up a ONVIF camera."""
if not hass.data[DATA_FFMPEG].async_run_test(config.get(CONF_HOST)): if not hass.data[DATA_FFMPEG].async_run_test(config.get(CONF_HOST)):
return return
async_add_devices([ONVIFCamera(hass, config)]) async_add_devices([ONVIFHassCamera(hass, config)])
class ONVIFCamera(Camera): class ONVIFHassCamera(Camera):
"""An implementation of an ONVIF camera.""" """An implementation of an ONVIF camera."""
def __init__(self, hass, config): def __init__(self, hass, config):
"""Initialize a ONVIF camera.""" """Initialize a ONVIF camera."""
from onvif import ONVIFService from onvif import ONVIFCamera
import onvif
super().__init__() super().__init__()
self._name = config.get(CONF_NAME) self._name = config.get(CONF_NAME)
self._ffmpeg_arguments = config.get(CONF_EXTRA_ARGUMENTS) self._ffmpeg_arguments = config.get(CONF_EXTRA_ARGUMENTS)
media = ONVIFService( self._input = None
'http://{}:{}/onvif/device_service'.format( try:
config.get(CONF_HOST), config.get(CONF_PORT)), _LOGGER.debug("Connecting with ONVIF Camera: %s on port %s",
config.get(CONF_USERNAME), config.get(CONF_HOST), config.get(CONF_PORT))
config.get(CONF_PASSWORD), media_service = ONVIFCamera(
'{}/wsdl/media.wsdl'.format(os.path.dirname(onvif.__file__)) config.get(CONF_HOST), config.get(CONF_PORT),
) config.get(CONF_USERNAME), config.get(CONF_PASSWORD)
self._input = media.GetStreamUri().Uri ).create_media_service()
_LOGGER.debug("ONVIF Camera Using the following URL for %s: %s", stream_uri = media_service.GetStreamUri(
self._name, self._input) {'StreamSetup': {'Stream': 'RTP-Unicast', 'Transport': 'RTSP'}}
)
self._input = stream_uri.Uri.replace(
'rtsp://', 'rtsp://{}:{}@'.format(
config.get(CONF_USERNAME),
config.get(CONF_PASSWORD)), 1)
_LOGGER.debug(
"ONVIF Camera Using the following URL for %s: %s",
self._name, self._input)
except Exception as err:
_LOGGER.error("Unable to communicate with ONVIF Camera: %s", err)
raise
@asyncio.coroutine @asyncio.coroutine
def async_camera_image(self): def async_camera_image(self):