Update go2rtc stream if stream_source is not matching (#129804)

This commit is contained in:
Robert Resch 2024-11-04 13:58:12 +01:00 committed by GitHub
parent 57eeaf1f75
commit df35c8e707
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 9 deletions

View file

@ -203,15 +203,17 @@ class WebRTCProvider(CameraWebRTCProvider):
self._session, self._url, source=camera.entity_id
)
if not (stream_source := await camera.stream_source()):
send_message(
WebRTCError("go2rtc_webrtc_offer_failed", "Camera has no stream source")
)
return
streams = await self._rest_client.streams.list()
if camera.entity_id not in streams:
if not (stream_source := await camera.stream_source()):
send_message(
WebRTCError(
"go2rtc_webrtc_offer_failed", "Camera has no stream source"
)
)
return
if (stream := streams.get(camera.entity_id)) is None or not any(
stream_source == producer.url for producer in stream.producers
):
await self._rest_client.streams.add(camera.entity_id, stream_source)
@callback

View file

@ -21,7 +21,8 @@ def rest_client() -> Generator[AsyncMock]:
patch("homeassistant.components.go2rtc.server.Go2RtcRestClient", mock_client),
):
client = mock_client.return_value
client.streams = Mock(spec_set=_StreamClient)
client.streams = streams = Mock(spec_set=_StreamClient)
streams.list.return_value = {}
client.webrtc = Mock(spec_set=_WebRTCClient)
yield client

View file

@ -239,6 +239,18 @@ async def _test_setup_and_signaling(
rest_client.streams.add.assert_called_once_with(entity_id, "rtsp://stream")
# Stream exists but the source is different
rest_client.streams.add.reset_mock()
rest_client.streams.list.return_value = {
entity_id: Stream([Producer("rtsp://different")])
}
receive_message_callback.reset_mock()
ws_client.reset_mock()
await test()
rest_client.streams.add.assert_called_once_with(entity_id, "rtsp://stream")
# If the stream is already added, the stream should not be added again.
rest_client.streams.add.reset_mock()
rest_client.streams.list.return_value = {