Use custom function instead mashumaro in WebRTC dataclasses (#128099)

This commit is contained in:
Robert Resch 2024-10-10 16:15:09 +02:00 committed by GitHub
parent 5e38bb7a32
commit dd856a9116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 27 deletions

View File

@ -7,9 +7,6 @@ from collections.abc import Awaitable, Callable, Coroutine
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Protocol from typing import TYPE_CHECKING, Any, Protocol
from mashumaro import field_options
from mashumaro.config import BaseConfig
from mashumaro.mixins.dict import DataClassDictMixin
import voluptuous as vol import voluptuous as vol
from homeassistant.components import websocket_api from homeassistant.components import websocket_api
@ -32,19 +29,8 @@ DATA_ICE_SERVERS: HassKey[list[Callable[[], Coroutine[Any, Any, RTCIceServer]]]]
) )
class _RTCBaseModel(DataClassDictMixin):
"""Base class for RTC models."""
class Config(BaseConfig):
"""Mashumaro config."""
# Serialize to spec conform names and omit default values
omit_default = True
serialize_by_alias = True
@dataclass @dataclass
class RTCIceServer(_RTCBaseModel): class RTCIceServer:
"""RTC Ice Server. """RTC Ice Server.
See https://www.w3.org/TR/webrtc/#rtciceserver-dictionary See https://www.w3.org/TR/webrtc/#rtciceserver-dictionary
@ -54,30 +40,56 @@ class RTCIceServer(_RTCBaseModel):
username: str | None = None username: str | None = None
credential: str | None = None credential: str | None = None
def to_frontend_dict(self) -> dict[str, Any]:
"""Return a dict that can be used by the frontend."""
data = {
"urls": self.urls,
}
if self.username is not None:
data["username"] = self.username
if self.credential is not None:
data["credential"] = self.credential
return data
@dataclass @dataclass
class RTCConfiguration(_RTCBaseModel): class RTCConfiguration:
"""RTC Configuration. """RTC Configuration.
See https://www.w3.org/TR/webrtc/#rtcconfiguration-dictionary See https://www.w3.org/TR/webrtc/#rtcconfiguration-dictionary
""" """
ice_servers: list[RTCIceServer] = field( ice_servers: list[RTCIceServer] = field(default_factory=list)
metadata=field_options(alias="iceServers"), default_factory=list
) def to_frontend_dict(self) -> dict[str, Any]:
"""Return a dict that can be used by the frontend."""
if not self.ice_servers:
return {}
return {
"iceServers": [server.to_frontend_dict() for server in self.ice_servers]
}
@dataclass(kw_only=True) @dataclass(kw_only=True)
class WebRTCClientConfiguration(_RTCBaseModel): class WebRTCClientConfiguration:
"""WebRTC configuration for the client. """WebRTC configuration for the client.
Not part of the spec, but required to configure client. Not part of the spec, but required to configure client.
""" """
configuration: RTCConfiguration = field(default_factory=RTCConfiguration) configuration: RTCConfiguration = field(default_factory=RTCConfiguration)
data_channel: str | None = field( data_channel: str | None = None
metadata=field_options(alias="dataChannel"), default=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_frontend_dict(),
}
if self.data_channel is not None:
data["dataChannel"] = self.data_channel
return data
class CameraWebRTCProvider(Protocol): class CameraWebRTCProvider(Protocol):
@ -153,7 +165,7 @@ async def ws_get_client_config(
) )
return return
config = (await camera.async_get_webrtc_client_configuration()).to_dict() config = (await camera.async_get_webrtc_client_configuration()).to_frontend_dict()
connection.send_result( connection.send_result(
msg["id"], msg["id"],
config, config,

View File

@ -38,7 +38,6 @@ httpx==0.27.2
ifaddr==0.2.0 ifaddr==0.2.0
Jinja2==3.1.4 Jinja2==3.1.4
lru-dict==1.3.0 lru-dict==1.3.0
mashumaro==3.13.1
mutagen==1.47.0 mutagen==1.47.0
orjson==3.10.7 orjson==3.10.7
packaging>=23.1 packaging>=23.1
@ -123,6 +122,9 @@ backoff>=2.0
# v2 has breaking changes (#99218). # v2 has breaking changes (#99218).
pydantic==1.10.18 pydantic==1.10.18
# Required for Python 3.12.4 compatibility (#119223).
mashumaro>=3.13.1
# Breaks asyncio # Breaks asyncio
# https://github.com/pubnub/python/issues/130 # https://github.com/pubnub/python/issues/130
pubnub!=6.4.0 pubnub!=6.4.0

View File

@ -50,7 +50,6 @@ dependencies = [
"ifaddr==0.2.0", "ifaddr==0.2.0",
"Jinja2==3.1.4", "Jinja2==3.1.4",
"lru-dict==1.3.0", "lru-dict==1.3.0",
"mashumaro==3.13.1",
"PyJWT==2.9.0", "PyJWT==2.9.0",
# PyJWT has loose dependency. We want the latest one. # PyJWT has loose dependency. We want the latest one.
"cryptography==43.0.1", "cryptography==43.0.1",

View File

@ -24,7 +24,6 @@ home-assistant-bluetooth==1.13.0
ifaddr==0.2.0 ifaddr==0.2.0
Jinja2==3.1.4 Jinja2==3.1.4
lru-dict==1.3.0 lru-dict==1.3.0
mashumaro==3.13.1
PyJWT==2.9.0 PyJWT==2.9.0
cryptography==43.0.1 cryptography==43.0.1
Pillow==10.4.0 Pillow==10.4.0

View File

@ -140,6 +140,9 @@ backoff>=2.0
# v2 has breaking changes (#99218). # v2 has breaking changes (#99218).
pydantic==1.10.18 pydantic==1.10.18
# Required for Python 3.12.4 compatibility (#119223).
mashumaro>=3.13.1
# Breaks asyncio # Breaks asyncio
# https://github.com/pubnub/python/issues/130 # https://github.com/pubnub/python/issues/130
pubnub!=6.4.0 pubnub!=6.4.0