async def mock_rtsp_to_web_rtc_fixture(hass): """Fixture that registers a mock rtsp to web_rtc provider.""" mock_provider = Mock(side_effect=provide_web_rtc_answer) unsub = camera.async_register_rtsp_to_web_rtc_provider( hass, "mock_domain", mock_provider) yield mock_provider unsub()
async def test_rtsp_to_web_rtc_offer_not_accepted( hass, hass_ws_client, mock_camera, mock_stream_source, ): """Test a provider that can't satisfy the rtsp to webrtc offer.""" async def provide_none(stream_source: str, offer: str) -> str: """Simulate a provider that can't accept the offer.""" return None mock_provider = Mock(side_effect=provide_none) unsub = camera.async_register_rtsp_to_web_rtc_provider( hass, "mock_domain", mock_provider) client = await hass_ws_client(hass) # Registered provider can handle the WebRTC offer await client.send_json({ "id": 11, "type": "camera/web_rtc_offer", "entity_id": "camera.demo_camera", "offer": WEBRTC_OFFER, }) response = await client.receive_json() assert response["id"] == 11 assert response.get("type") == TYPE_RESULT assert "success" in response assert not response["success"] assert mock_provider.called unsub()
async def test_rtsp_to_web_rtc_provider_unregistered( hass, hass_ws_client, mock_camera, mock_stream_source, ): """Test creating a web_rtc offer from an rstp provider.""" mock_provider = Mock(side_effect=provide_web_rtc_answer) unsub = camera.async_register_rtsp_to_web_rtc_provider( hass, "mock_domain", mock_provider ) client = await hass_ws_client(hass) # Registered provider can handle the WebRTC offer await client.send_json( { "id": 11, "type": "camera/web_rtc_offer", "entity_id": "camera.demo_camera", "offer": WEBRTC_OFFER, } ) response = await client.receive_json() assert response["id"] == 11 assert response["type"] == TYPE_RESULT assert response["success"] assert response["result"]["answer"] == WEBRTC_ANSWER assert mock_provider.called mock_provider.reset_mock() # Unregister provider, then verify the WebRTC offer cannot be handled unsub() await client.send_json( { "id": 12, "type": "camera/web_rtc_offer", "entity_id": "camera.demo_camera", "offer": WEBRTC_OFFER, } ) response = await client.receive_json() assert response.get("id") == 12 assert response.get("type") == TYPE_RESULT assert "success" in response assert not response["success"] assert not mock_provider.called
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up RTSPtoWebRTC from a config entry.""" hass.data.setdefault(DOMAIN, {}) client: WebRTCClientInterface try: async with async_timeout.timeout(TIMEOUT): client = await get_adaptive_client(async_get_clientsession(hass), entry.data[DATA_SERVER_URL]) except ResponseError as err: raise ConfigEntryNotReady from err except (TimeoutError, ClientError) as err: raise ConfigEntryNotReady from err async def async_offer_for_stream_source( stream_source: str, offer_sdp: str, stream_id: str, ) -> str: """Handle the signal path for a WebRTC stream. This signal path is used to route the offer created by the client to the proxy server that translates a stream to WebRTC. The communication for the stream itself happens directly between the client and proxy. """ try: async with async_timeout.timeout(TIMEOUT): return await client.offer_stream_id(stream_id, offer_sdp, stream_source) except TimeoutError as err: raise HomeAssistantError( "Timeout talking to RTSPtoWebRTC server") from err except ClientError as err: raise HomeAssistantError(str(err)) from err entry.async_on_unload( camera.async_register_rtsp_to_web_rtc_provider( hass, DOMAIN, async_offer_for_stream_source)) return True