From 4bcf827890ea82a7ef8bcd45451b2f8f11459b31 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Tue, 26 Nov 2024 21:37:26 -0300 Subject: [PATCH] Fix deprecated async_handle_web_rtc_offer --- custom_components/frigate/camera.py | 30 +++++++++++++---- requirements.txt | 2 +- requirements_dev.txt | 2 +- tests/test_camera.py | 52 ++++++++++++++++++++++------- 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/custom_components/frigate/camera.py b/custom_components/frigate/camera.py index a7d146e3..d3b1a642 100644 --- a/custom_components/frigate/camera.py +++ b/custom_components/frigate/camera.py @@ -4,7 +4,7 @@ import datetime import logging -from typing import Any, cast +from typing import Any import async_timeout from jinja2 import Template @@ -12,7 +12,13 @@ from yarl import URL from custom_components.frigate.api import FrigateApiClient -from homeassistant.components.camera import Camera, CameraEntityFeature, StreamType +from homeassistant.components.camera import ( + Camera, + CameraEntityFeature, + StreamType, + WebRTCAnswer, + WebRTCSendMessage, +) from homeassistant.components.mqtt import async_publish from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_URL @@ -301,14 +307,20 @@ async def async_enable_motion_detection(self) -> None: False, ) - async def async_handle_web_rtc_offer(self, offer_sdp: str) -> str | None: + async def async_handle_async_webrtc_offer( + self, offer_sdp: str, session_id: str, send_message: WebRTCSendMessage + ) -> None: """Handle the WebRTC offer and return an answer.""" websession = async_get_clientsession(self.hass) url = f"{self._url}/api/go2rtc/webrtc?src={self._cam_name}" payload = {"type": "offer", "sdp": offer_sdp} async with websession.post(url, json=payload) as resp: answer = await resp.json() - return cast(str, answer["sdp"]) + send_message(WebRTCAnswer(answer["sdp"])) + + async def async_on_webrtc_candidate(self, session_id: str, candidate: Any) -> None: + """Ignore WebRTC candidates for Frigate cameras.""" + return async def async_disable_motion_detection(self) -> None: """Disable motion detection for this camera.""" @@ -435,11 +447,17 @@ async def stream_source(self) -> str | None: """Return the source of the stream.""" return self._stream_source - async def async_handle_web_rtc_offer(self, offer_sdp: str) -> str | None: + async def async_handle_async_webrtc_offer( + self, offer_sdp: str, session_id: str, send_message: WebRTCSendMessage + ) -> None: """Handle the WebRTC offer and return an answer.""" websession = async_get_clientsession(self.hass) url = f"{self._url}/api/go2rtc/webrtc?src={self._cam_name}" payload = {"type": "offer", "sdp": offer_sdp} async with websession.post(url, json=payload) as resp: answer = await resp.json() - return cast(str, answer["sdp"]) + send_message(WebRTCAnswer(answer["sdp"])) + + async def async_on_webrtc_candidate(self, session_id: str, candidate: Any) -> None: + """Ignore WebRTC candidates for Frigate cameras.""" + return diff --git a/requirements.txt b/requirements.txt index 1fa1ba41..43d8013e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ aiohttp aiohttp_cors attr janus -homeassistant==2024.10.4 +homeassistant==2024.11.3 paho-mqtt python-dateutil yarl diff --git a/requirements_dev.txt b/requirements_dev.txt index 217edd08..9b1d25bc 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -4,7 +4,7 @@ flake8 mypy pre-commit pytest -pytest-homeassistant-custom-component==0.13.175 +pytest-homeassistant-custom-component==0.13.184 pylint-pytest pylint pytest-aiohttp diff --git a/tests/test_camera.py b/tests/test_camera.py index 2addbf69..1d650209 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -97,17 +97,31 @@ async def test_frigate_camera_setup( await client.send_json( { "id": 5, - "type": "camera/web_rtc_offer", + "type": "camera/webrtc/offer", "entity_id": TEST_CAMERA_FRONT_DOOR_ENTITY_ID, "offer": "send_sdp", } ) - msg = await client.receive_json() - assert msg["id"] == 5 - assert msg["type"] == TYPE_RESULT - assert msg["success"] - assert msg["result"]["answer"] == "return_sdp" + response = await client.receive_json() + assert response["id"] == 5 + assert response["type"] == TYPE_RESULT + assert response["success"] + + # Session id + response = await client.receive_json() + assert response["id"] == 5 + assert response["type"] == "event" + assert response["event"]["type"] == "session" + + # Answer + response = await client.receive_json() + assert response["id"] == 5 + assert response["type"] == "event" + assert response["event"] == { + "type": "answer", + "answer": "return_sdp", + } async def test_frigate_camera_setup_birdseye( @@ -150,17 +164,31 @@ async def test_frigate_camera_setup_birdseye( await client.send_json( { "id": 5, - "type": "camera/web_rtc_offer", + "type": "camera/webrtc/offer", "entity_id": TEST_CAMERA_BIRDSEYE_ENTITY_ID, "offer": "send_sdp", } ) - msg = await client.receive_json() - assert msg["id"] == 5 - assert msg["type"] == TYPE_RESULT - assert msg["success"] - assert msg["result"]["answer"] == "return_sdp" + response = await client.receive_json() + assert response["id"] == 5 + assert response["type"] == TYPE_RESULT + assert response["success"] + + # Session id + response = await client.receive_json() + assert response["id"] == 5 + assert response["type"] == "event" + assert response["event"]["type"] == "session" + + # Answer + response = await client.receive_json() + assert response["id"] == 5 + assert response["type"] == "event" + assert response["event"] == { + "type": "answer", + "answer": "return_sdp", + } async def test_frigate_extra_attributes(hass: HomeAssistant) -> None: