Stop refreshing Nest WebRTC streams that can't be extended (#184525)

This commit is contained in:
Franck Nijhof
2026-10-07 05:42:03 +00:00
parent d046379fec
commit 2c41884d65
2 changed files with 75 additions and 2 deletions
+12 -2
View File
@@ -16,7 +16,7 @@ from google_nest_sdm.camera_traits import (
WebRtcStream,
)
from google_nest_sdm.device import Device
from google_nest_sdm.exceptions import ApiException
from google_nest_sdm.exceptions import ApiException, FailedPreconditionException
from webrtc_models import RTCIceCandidateInit
from homeassistant.components.camera import (
@@ -266,7 +266,17 @@ class NestWebRTCEntity(NestCameraBaseEntity):
if not (webrtc_stream := self._webrtc_sessions.get(session_id)):
return None
_LOGGER.debug("Extending WebRTC stream %s", webrtc_stream.media_session_id)
webrtc_stream = await webrtc_stream.extend_stream()
try:
webrtc_stream = await webrtc_stream.extend_stream()
except FailedPreconditionException as err:
# The session is no longer valid or the device does not support
# extending it, retrying only runs into the API rate limit
_LOGGER.debug(
"Not refreshing WebRTC stream %s: %s",
webrtc_stream.media_session_id,
err,
)
return None
if session_id in self._webrtc_sessions:
self._webrtc_sessions[session_id] = webrtc_stream
return webrtc_stream.expires_at
+63
View File
@@ -969,3 +969,66 @@ async def test_webrtc_refresh_expired_stream(
auth.captured_requests[1][2].get("command")
== "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream"
)
@pytest.mark.usefixtures("webrtc_camera_device")
async def test_webrtc_refresh_failed_precondition(
hass: HomeAssistant,
setup_platform: PlatformSetup,
hass_ws_client: WebSocketGenerator,
auth: FakeAuth,
) -> None:
"""Test a WebRTC stream that can't be extended is not refreshed again."""
now = utcnow()
auth.responses = [
aiohttp.web.json_response(
{
"results": {
"answerSdp": "v=0\r\ns=-\r\n",
"mediaSessionId": "yP2grqz0Y1V_wgiX9KEbMWHoLd...",
"expiresAt": (now + datetime.timedelta(seconds=90)).isoformat(
timespec="seconds"
),
},
}
),
aiohttp.web.json_response(
{
"error": {
"code": 400,
"message": (
"WebRtc error caused by invalid session or user id mismatch."
),
"status": "FAILED_PRECONDITION",
}
},
status=HTTPStatus.BAD_REQUEST,
),
]
await setup_platform()
await hass.async_block_till_done()
client = await hass_ws_client(hass)
await client.send_json_auto_id(
{
"type": "camera/webrtc/offer",
"entity_id": "camera.my_camera",
"offer": "a=recvonly",
}
)
response = await client.receive_json()
assert response["success"]
# The stream extension fails because the session is no longer valid
await fire_alarm(hass, now + datetime.timedelta(seconds=60))
# Retrying the extension can't succeed, so it is not attempted again
await fire_alarm(hass, now + datetime.timedelta(minutes=10))
await fire_alarm(hass, now + datetime.timedelta(hours=1))
extend_requests = [
request
for request in auth.captured_requests
if request[2].get("command")
== "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream"
]
assert len(extend_requests) == 1