Skip to content

Commit

Permalink
Enable WebRTC for ring integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sdb9696 committed Oct 4, 2024
1 parent 8bbbaae commit 104d35e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions homeassistant/components/camera/webrtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
from collections.abc import Awaitable, Callable, Coroutine
from dataclasses import dataclass, field
from enum import StrEnum, auto
from typing import TYPE_CHECKING, Any, Protocol

from mashumaro import field_options
Expand Down Expand Up @@ -74,10 +75,18 @@ class WebRTCClientConfiguration(_RTCBaseModel):
Not part of the spec, but required to configure client.
"""

class TransportDirection(StrEnum):
"""Transport direction enum."""

RECVONLY = auto()
SENDONLY = auto()
SENDRECV = auto()

configuration: RTCConfiguration = field(default_factory=RTCConfiguration)
data_channel: str | None = field(
metadata=field_options(alias="dataChannel"), default=None
)
audio_direction: TransportDirection = TransportDirection.RECVONLY


class CameraWebRTCProvider(Protocol):
Expand Down
19 changes: 18 additions & 1 deletion homeassistant/components/ring/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from ring_doorbell import RingDoorBell

from homeassistant.components import ffmpeg
from homeassistant.components.camera import Camera
from homeassistant.components.camera import (
Camera,
CameraEntityFeature,
StreamType,
WebRTCClientConfiguration,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -69,6 +74,8 @@ def __init__(
self._attr_unique_id = str(device.id)
if device.has_capability(MOTION_DETECTION_CAPABILITY):
self._attr_motion_detection_enabled = device.motion_detection
self._attr_supported_features |= CameraEntityFeature.STREAM
self._attr_frontend_stream_type = StreamType.WEB_RTC

@callback
def _handle_coordinator_update(self) -> None:
Expand Down Expand Up @@ -136,6 +143,16 @@ async def handle_async_mjpeg_stream(
finally:
await stream.close()

async def async_handle_web_rtc_offer(self, offer_sdp: str) -> str | None:
"""Return the source of the stream."""
return await self._device.generate_webrtc_stream(offer_sdp)

async def _async_get_webrtc_client_configuration(self) -> WebRTCClientConfiguration:
"""Return the WebRTC client configuration."""
return WebRTCClientConfiguration(
audio_direction=WebRTCClientConfiguration.TransportDirection.SENDRECV,
)

async def async_update(self) -> None:
"""Update camera entity and refresh attributes."""
if (
Expand Down

0 comments on commit 104d35e

Please sign in to comment.