Skip to content

Commit

Permalink
Use custom function instead mashumaro in WebRTC dataclasses (#128099)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Oct 10, 2024
1 parent 5e38bb7 commit dd856a9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
60 changes: 36 additions & 24 deletions homeassistant/components/camera/webrtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
from dataclasses import dataclass, field
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

from homeassistant.components import websocket_api
Expand All @@ -32,19 +29,8 @@
)


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
class RTCIceServer(_RTCBaseModel):
class RTCIceServer:
"""RTC Ice Server.
See https://www.w3.org/TR/webrtc/#rtciceserver-dictionary
Expand All @@ -54,30 +40,56 @@ class RTCIceServer(_RTCBaseModel):
username: 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
class RTCConfiguration(_RTCBaseModel):
class RTCConfiguration:
"""RTC Configuration.
See https://www.w3.org/TR/webrtc/#rtcconfiguration-dictionary
"""

ice_servers: list[RTCIceServer] = field(
metadata=field_options(alias="iceServers"), default_factory=list
)
ice_servers: list[RTCIceServer] = field(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)
class WebRTCClientConfiguration(_RTCBaseModel):
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 = field(
metadata=field_options(alias="dataChannel"), default=None
)
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_frontend_dict(),
}
if self.data_channel is not None:
data["dataChannel"] = self.data_channel
return data


class CameraWebRTCProvider(Protocol):
Expand Down Expand Up @@ -153,7 +165,7 @@ async def ws_get_client_config(
)
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(
msg["id"],
config,
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/package_constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ httpx==0.27.2
ifaddr==0.2.0
Jinja2==3.1.4
lru-dict==1.3.0
mashumaro==3.13.1
mutagen==1.47.0
orjson==3.10.7
packaging>=23.1
Expand Down Expand Up @@ -123,6 +122,9 @@ backoff>=2.0
# v2 has breaking changes (#99218).
pydantic==1.10.18

# Required for Python 3.12.4 compatibility (#119223).
mashumaro>=3.13.1

# Breaks asyncio
# https://github.com/pubnub/python/issues/130
pubnub!=6.4.0
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ dependencies = [
"ifaddr==0.2.0",
"Jinja2==3.1.4",
"lru-dict==1.3.0",
"mashumaro==3.13.1",
"PyJWT==2.9.0",
# PyJWT has loose dependency. We want the latest one.
"cryptography==43.0.1",
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ home-assistant-bluetooth==1.13.0
ifaddr==0.2.0
Jinja2==3.1.4
lru-dict==1.3.0
mashumaro==3.13.1
PyJWT==2.9.0
cryptography==43.0.1
Pillow==10.4.0
Expand Down
3 changes: 3 additions & 0 deletions script/gen_requirements_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
# v2 has breaking changes (#99218).
pydantic==1.10.18
# Required for Python 3.12.4 compatibility (#119223).
mashumaro>=3.13.1
# Breaks asyncio
# https://github.com/pubnub/python/issues/130
pubnub!=6.4.0
Expand Down

0 comments on commit dd856a9

Please sign in to comment.