-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Extract WebRTC integration #157648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
balloob
merged 8 commits into
dev
from
claude/extract-webrtc-integration-01AHXVj6dh19FvgdL7rh439P
Dec 4, 2025
Merged
Extract WebRTC integration #157648
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b043392
Exract WebRTC integration
balloob c7367a8
Update tests
balloob 759ae3e
Rename to web_rtc
balloob ee0340f
Add __all__ and move async_setup
balloob ee89a59
Allow configuring ice servers in web_rtc integration
balloob 43ca826
Fix test
balloob aece46d
Clean up __all__
balloob a294215
Apply STUN order change to extracted integration
balloob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """The WebRTC integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable, Iterable | ||
| from typing import Any | ||
|
|
||
| import voluptuous as vol | ||
| from webrtc_models import RTCIceServer | ||
|
|
||
| from homeassistant.components import websocket_api | ||
| from homeassistant.const import CONF_URL, CONF_USERNAME | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.core_config import ( | ||
| CONF_CREDENTIAL, | ||
| CONF_ICE_SERVERS, | ||
| validate_stun_or_turn_url, | ||
| ) | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.typing import ConfigType | ||
| from homeassistant.util.hass_dict import HassKey | ||
|
|
||
| __all__ = [ | ||
| "async_get_ice_servers", | ||
| "async_register_ice_servers", | ||
| ] | ||
|
|
||
| DOMAIN = "web_rtc" | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema( | ||
| { | ||
| DOMAIN: vol.Schema( | ||
| { | ||
| vol.Required(CONF_ICE_SERVERS): vol.All( | ||
| cv.ensure_list, | ||
| [ | ||
| vol.Schema( | ||
| { | ||
| vol.Required(CONF_URL): vol.All( | ||
| cv.ensure_list, [validate_stun_or_turn_url] | ||
| ), | ||
| vol.Optional(CONF_USERNAME): cv.string, | ||
| vol.Optional(CONF_CREDENTIAL): cv.string, | ||
| } | ||
| ) | ||
| ], | ||
| ) | ||
| } | ||
| ) | ||
| }, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
|
|
||
| DATA_ICE_SERVERS_USER: HassKey[Iterable[RTCIceServer]] = HassKey( | ||
| "web_rtc_ice_servers_user" | ||
| ) | ||
| DATA_ICE_SERVERS: HassKey[list[Callable[[], Iterable[RTCIceServer]]]] = HassKey( | ||
| "web_rtc_ice_servers" | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: | ||
| """Set up the WebRTC integration.""" | ||
| servers = [ | ||
| RTCIceServer( | ||
| server[CONF_URL], | ||
| server.get(CONF_USERNAME), | ||
| server.get(CONF_CREDENTIAL), | ||
| ) | ||
| for server in config.get(DOMAIN, {}).get(CONF_ICE_SERVERS, []) | ||
| ] | ||
| if servers: | ||
| hass.data[DATA_ICE_SERVERS_USER] = servers | ||
|
|
||
| hass.data[DATA_ICE_SERVERS] = [] | ||
| websocket_api.async_register_command(hass, ws_ice_servers) | ||
| return True | ||
|
|
||
|
|
||
| @callback | ||
| def async_register_ice_servers( | ||
| hass: HomeAssistant, | ||
| get_ice_server_fn: Callable[[], Iterable[RTCIceServer]], | ||
| ) -> Callable[[], None]: | ||
| """Register an ICE server. | ||
|
|
||
| The registering integration is responsible to implement caching if needed. | ||
| """ | ||
| servers = hass.data[DATA_ICE_SERVERS] | ||
|
|
||
| def remove() -> None: | ||
| servers.remove(get_ice_server_fn) | ||
|
|
||
| servers.append(get_ice_server_fn) | ||
| return remove | ||
|
|
||
|
|
||
| @callback | ||
| def async_get_ice_servers(hass: HomeAssistant) -> list[RTCIceServer]: | ||
| """Return all registered ICE servers.""" | ||
| servers: list[RTCIceServer] = [] | ||
|
|
||
| if hass.config.webrtc.ice_servers: | ||
| servers.extend(hass.config.webrtc.ice_servers) | ||
|
edenhaus marked this conversation as resolved.
|
||
|
|
||
| if DATA_ICE_SERVERS_USER in hass.data: | ||
| servers.extend(hass.data[DATA_ICE_SERVERS_USER]) | ||
|
|
||
| if not servers: | ||
| servers = [ | ||
| RTCIceServer( | ||
| urls=[ | ||
| "stun:stun.home-assistant.io:3478", | ||
| "stun:stun.home-assistant.io:80", | ||
| ] | ||
| ), | ||
| ] | ||
|
edenhaus marked this conversation as resolved.
|
||
|
|
||
| for gen_servers in hass.data[DATA_ICE_SERVERS]: | ||
| servers.extend(gen_servers()) | ||
|
|
||
| return servers | ||
|
|
||
|
|
||
| @websocket_api.websocket_command( | ||
| { | ||
| "type": "web_rtc/ice_servers", | ||
| } | ||
| ) | ||
| @callback | ||
| def ws_ice_servers( | ||
| hass: HomeAssistant, | ||
| connection: websocket_api.ActiveConnection, | ||
| msg: dict[str, Any], | ||
| ) -> None: | ||
| """Handle get WebRTC ICE servers websocket command.""" | ||
| ice_servers = [server.to_dict() for server in async_get_ice_servers(hass)] | ||
| connection.send_result(msg["id"], ice_servers) | ||
|
arturpragacz marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "domain": "web_rtc", | ||
| "name": "WebRTC", | ||
| "codeowners": ["@home-assistant/core"], | ||
| "documentation": "https://www.home-assistant.io/integrations/web_rtc", | ||
| "integration_type": "system", | ||
| "quality_scale": "internal" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.