From 39da0dc409347d6e84cec75b7b30d3c9be4b68b1 Mon Sep 17 00:00:00 2001 From: uvjustin <46082645+uvjustin@users.noreply.github.com> Date: Sun, 21 Feb 2021 02:11:50 +0800 Subject: [PATCH] Add rtsp transport options to generic camera (#46623) * Add rtsp transport options to generic camera --- homeassistant/components/generic/camera.py | 12 ++++++++++-- tests/components/generic/test_camera.py | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/generic/camera.py b/homeassistant/components/generic/camera.py index 28db66b4f3e..56b490e165a 100644 --- a/homeassistant/components/generic/camera.py +++ b/homeassistant/components/generic/camera.py @@ -34,6 +34,9 @@ CONF_LIMIT_REFETCH_TO_URL_CHANGE = "limit_refetch_to_url_change" CONF_STILL_IMAGE_URL = "still_image_url" CONF_STREAM_SOURCE = "stream_source" CONF_FRAMERATE = "framerate" +CONF_RTSP_TRANSPORT = "rtsp_transport" +FFMPEG_OPTION_MAP = {CONF_RTSP_TRANSPORT: "rtsp_transport"} +ALLOWED_RTSP_TRANSPORT_PROTOCOLS = {"tcp", "udp", "udp_multicast", "http"} DEFAULT_NAME = "Generic Camera" GET_IMAGE_TIMEOUT = 10 @@ -54,6 +57,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( cv.small_float, cv.positive_int ), vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean, + vol.Optional(CONF_RTSP_TRANSPORT): vol.In(ALLOWED_RTSP_TRANSPORT_PROTOCOLS), } ) @@ -85,15 +89,19 @@ class GenericCamera(Camera): self._supported_features = SUPPORT_STREAM if self._stream_source else 0 self.content_type = device_info[CONF_CONTENT_TYPE] self.verify_ssl = device_info[CONF_VERIFY_SSL] + if device_info.get(CONF_RTSP_TRANSPORT): + self.stream_options[FFMPEG_OPTION_MAP[CONF_RTSP_TRANSPORT]] = device_info[ + CONF_RTSP_TRANSPORT + ] username = device_info.get(CONF_USERNAME) password = device_info.get(CONF_PASSWORD) if username and password: if self._authentication == HTTP_DIGEST_AUTHENTICATION: - self._auth = httpx.DigestAuth(username, password) + self._auth = httpx.DigestAuth(username=username, password=password) else: - self._auth = httpx.BasicAuth(username, password=password) + self._auth = httpx.BasicAuth(username=username, password=password) else: self._auth = None diff --git a/tests/components/generic/test_camera.py b/tests/components/generic/test_camera.py index 7f5b3bb3b53..3e2f07b446b 100644 --- a/tests/components/generic/test_camera.py +++ b/tests/components/generic/test_camera.py @@ -250,6 +250,28 @@ async def test_stream_source_error(aioclient_mock, hass, hass_client, hass_ws_cl } +async def test_setup_alternative_options(hass, hass_ws_client): + """Test that the stream source is setup with different config options.""" + assert await async_setup_component( + hass, + "camera", + { + "camera": { + "name": "config_test", + "platform": "generic", + "still_image_url": "https://example.com", + "authentication": "digest", + "username": "user", + "password": "pass", + "stream_source": "rtsp://example.com:554/rtsp/", + "rtsp_transport": "udp", + }, + }, + ) + await hass.async_block_till_done() + assert hass.data["camera"].get_entity("camera.config_test") + + async def test_no_stream_source(aioclient_mock, hass, hass_client, hass_ws_client): """Test a stream request without stream source option set.""" assert await async_setup_component(