Skip to content

Commit 982884f

Browse files
committed
Add /api/webrtc/camera_stream_source
1 parent 7bddb2d commit 982884f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

custom_components/webrtc/__init__.py

+26
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import voluptuous as vol
1010
from aiohttp import web
1111
from aiohttp.web_exceptions import HTTPUnauthorized, HTTPGone, HTTPNotFound
12+
from homeassistant.components.camera import Camera, CameraView
13+
from homeassistant.components.camera.const import DOMAIN as CAMERA_DOMAIN
1214
from homeassistant.components.hassio.ingress import _websocket_forward
1315
from homeassistant.components.http import HomeAssistantView
1416
from homeassistant.config_entries import ConfigEntry
@@ -18,6 +20,7 @@
1820
CONF_URL,
1921
)
2022
from homeassistant.helpers.aiohttp_client import async_get_clientsession
23+
from homeassistant.helpers.entity_component import EntityComponent
2124
from homeassistant.helpers.network import get_url
2225
from homeassistant.helpers.template import Template
2326
from homeassistant.helpers.typing import HomeAssistantType, ConfigType, ServiceCallType
@@ -102,6 +105,15 @@ async def dash_cast(call: ServiceCallType):
102105
hass.services.async_register(DOMAIN, "create_link", create_link, CREATE_LINK_SCHEMA)
103106
hass.services.async_register(DOMAIN, "dash_cast", dash_cast, DASH_CAST_SCHEMA)
104107

108+
# 6. Register get_camera_stream_source endpoint
109+
component: EntityComponent[Camera] = hass.data[CAMERA_DOMAIN]
110+
111+
if component is None:
112+
_LOGGER.error("Camera integration not initialized")
113+
return False
114+
115+
hass.http.register_view(CameraStreamSourceView(component))
116+
105117
return True
106118

107119

@@ -235,3 +247,17 @@ async def get(self, request: web.Request):
235247
await ws_server.send_json({"type": "error", "value": str(e)})
236248

237249
return ws_server
250+
251+
class CameraStreamSourceView(CameraView):
252+
"""Camera view to get stream source."""
253+
254+
url = "/api/webrtc/camera_stream_source/{entity_id}"
255+
name = "api:webrtc:camera_stream_source"
256+
requires_auth = True
257+
258+
async def handle(self, request: web.Request, camera: Camera) -> web.Response:
259+
"""Return the camera stream source as plain text."""
260+
stream_source = await camera.stream_source()
261+
if stream_source is None:
262+
raise web.HTTPNotFound()
263+
return web.Response(text=stream_source)

custom_components/webrtc/manifest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
],
1010
"dependencies": [
1111
"http",
12-
"lovelace"
12+
"lovelace",
13+
"camera"
1314
],
1415
"requirements": [],
1516
"version": "v3.1.0",

0 commit comments

Comments
 (0)