Skip to content

Commit

Permalink
Fix deprecated async_handle_web_rtc_offer
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Nov 27, 2024
1 parent 5692b61 commit 4bcf827
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
30 changes: 24 additions & 6 deletions custom_components/frigate/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

import datetime
import logging
from typing import Any, cast
from typing import Any

import async_timeout
from jinja2 import Template
import voluptuous as vol
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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ aiohttp
aiohttp_cors
attr
janus
homeassistant==2024.10.4
homeassistant==2024.11.3
paho-mqtt
python-dateutil
yarl
Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 40 additions & 12 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4bcf827

Please sign in to comment.