Skip to content

Commit

Permalink
Use webrtc-models package (#129032)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Oct 25, 2024
1 parent ea164a2 commit 3512cb9
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 83 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import attr
from propcache import cached_property
import voluptuous as vol
from webrtc_models import RTCIceServer

from homeassistant.components import websocket_api
from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView
Expand Down Expand Up @@ -63,7 +64,6 @@
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, VolDictType
from homeassistant.loader import bind_hass
from homeassistant.util.webrtc import RTCIceServer, WebRTCClientConfiguration

from .const import ( # noqa: F401
_DEPRECATED_STREAM_TYPE_HLS,
Expand All @@ -87,6 +87,7 @@
from .webrtc import (
DATA_ICE_SERVERS,
CameraWebRTCProvider,
WebRTCClientConfiguration,
async_get_supported_providers,
async_register_ice_servers,
async_register_rtsp_to_web_rtc_provider, # noqa: F401
Expand Down
23 changes: 22 additions & 1 deletion homeassistant/components/camera/webrtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

import asyncio
from collections.abc import Awaitable, Callable, Iterable
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Protocol

import voluptuous as vol
from webrtc_models import RTCConfiguration, RTCIceServer

from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.util.hass_dict import HassKey
from homeassistant.util.webrtc import RTCIceServer

from .const import DATA_COMPONENT, DOMAIN, StreamType
from .helper import get_camera_from_entity_id
Expand All @@ -29,6 +30,26 @@
)


@dataclass(kw_only=True)
class WebRTCClientConfiguration:
"""WebRTC configuration for the client.
Not part of the spec, but required to configure client.
"""

configuration: RTCConfiguration = field(default_factory=RTCConfiguration)
data_channel: str | None = None

def to_frontend_dict(self) -> dict[str, Any]:
"""Return a dict that can be used by the frontend."""
data: dict[str, Any] = {
"configuration": self.configuration.to_dict(),
}
if self.data_channel is not None:
data["dataChannel"] = self.data_channel
return data


class CameraWebRTCProvider(Protocol):
"""WebRTC provider."""

Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/nest/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
from google_nest_sdm.device_manager import DeviceManager
from google_nest_sdm.exceptions import ApiException

from homeassistant.components.camera import Camera, CameraEntityFeature, StreamType
from homeassistant.components.camera import (
Camera,
CameraEntityFeature,
StreamType,
WebRTCClientConfiguration,
)
from homeassistant.components.stream import CONF_EXTRA_PART_WAIT_TIME
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.util.dt import utcnow
from homeassistant.util.webrtc import WebRTCClientConfiguration

from .const import DATA_DEVICE_MANAGER, DOMAIN
from .device_info import NestDeviceInfo
Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/rtsp_to_webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
from rtsp_to_webrtc.client import get_adaptive_client
from rtsp_to_webrtc.exceptions import ClientError, ResponseError
from rtsp_to_webrtc.interface import WebRTCClientInterface
from webrtc_models import RTCIceServer

from homeassistant.components import camera
from homeassistant.components.camera.webrtc import (
RTCIceServer,
async_register_ice_servers,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
Expand Down Expand Up @@ -66,7 +63,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
def get_servers() -> list[RTCIceServer]:
return [RTCIceServer(urls=[server])]

entry.async_on_unload(async_register_ice_servers(hass, get_servers))
entry.async_on_unload(camera.async_register_ice_servers(hass, get_servers))

async def async_offer_for_stream_source(
stream_source: str,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from propcache import cached_property, under_cached_property
from typing_extensions import TypeVar
import voluptuous as vol
from webrtc_models import RTCConfiguration
import yarl

from . import util
Expand Down Expand Up @@ -119,7 +120,6 @@
UnitSystem,
get_unit_system,
)
from .util.webrtc import RTCConfiguration

# Typing imports that create a circular dependency
if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/core_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from urllib.parse import urlparse

import voluptuous as vol
from webrtc_models import RTCIceServer

from . import auth
from .auth import mfa_modules as auth_mfa_modules, providers as auth_providers
Expand Down Expand Up @@ -54,7 +55,6 @@
from .util.hass_dict import HassKey
from .util.package import is_docker_env
from .util.unit_system import get_unit_system, validate_unit_system
from .util.webrtc import RTCIceServer

_LOGGER = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions homeassistant/package_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ uv==0.4.22
voluptuous-openapi==0.0.5
voluptuous-serialize==2.6.0
voluptuous==0.15.2
webrtc-models==0.0.0b2
yarl==1.16.0
zeroconf==0.135.0

Expand Down
69 changes: 0 additions & 69 deletions homeassistant/util/webrtc.py

This file was deleted.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ dependencies = [
"voluptuous-serialize==2.6.0",
"voluptuous-openapi==0.0.5",
"yarl==1.16.0",
"webrtc-models==0.0.0b2",
]

[project.urls]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ voluptuous==0.15.2
voluptuous-serialize==2.6.0
voluptuous-openapi==0.0.5
yarl==1.16.0
webrtc-models==0.0.0b2
6 changes: 3 additions & 3 deletions tests/test_core_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
from voluptuous import Invalid, MultipleInvalid
from webrtc_models import RTCConfiguration, RTCIceServer

from homeassistant.const import (
ATTR_ASSUMED_STATE,
Expand All @@ -28,7 +29,6 @@
)
from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.entity import Entity
from homeassistant.util import webrtc as webrtc_util
from homeassistant.util.unit_system import (
METRIC_SYSTEM,
US_CUSTOMARY_SYSTEM,
Expand Down Expand Up @@ -423,8 +423,8 @@ async def test_loading_configuration(hass: HomeAssistant) -> None:
assert hass.config.country == "SE"
assert hass.config.language == "sv"
assert hass.config.radius == 150
assert hass.config.webrtc == webrtc_util.RTCConfiguration(
[webrtc_util.RTCIceServer(urls=["stun:custom_stun_server:3478"])]
assert hass.config.webrtc == RTCConfiguration(
[RTCIceServer(urls=["stun:custom_stun_server:3478"])]
)


Expand Down

0 comments on commit 3512cb9

Please sign in to comment.