|
9 | 9 | import voluptuous as vol
|
10 | 10 | from aiohttp import web
|
11 | 11 | 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 |
12 | 14 | from homeassistant.components.hassio.ingress import _websocket_forward
|
13 | 15 | from homeassistant.components.http import HomeAssistantView
|
14 | 16 | from homeassistant.config_entries import ConfigEntry
|
|
18 | 20 | CONF_URL,
|
19 | 21 | )
|
20 | 22 | from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
| 23 | +from homeassistant.helpers.entity_component import EntityComponent |
21 | 24 | from homeassistant.helpers.network import get_url
|
22 | 25 | from homeassistant.helpers.template import Template
|
23 | 26 | from homeassistant.helpers.typing import HomeAssistantType, ConfigType, ServiceCallType
|
@@ -102,6 +105,15 @@ async def dash_cast(call: ServiceCallType):
|
102 | 105 | hass.services.async_register(DOMAIN, "create_link", create_link, CREATE_LINK_SCHEMA)
|
103 | 106 | hass.services.async_register(DOMAIN, "dash_cast", dash_cast, DASH_CAST_SCHEMA)
|
104 | 107 |
|
| 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 | + |
105 | 117 | return True
|
106 | 118 |
|
107 | 119 |
|
@@ -235,3 +247,17 @@ async def get(self, request: web.Request):
|
235 | 247 | await ws_server.send_json({"type": "error", "value": str(e)})
|
236 | 248 |
|
237 | 249 | 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) |
0 commit comments