Skip to content

Commit

Permalink
Add /api/webrtc/camera_stream_source
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Apr 23, 2023
1 parent 7bddb2d commit 982884f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 26 additions & 0 deletions custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import voluptuous as vol
from aiohttp import web
from aiohttp.web_exceptions import HTTPUnauthorized, HTTPGone, HTTPNotFound
from homeassistant.components.camera import Camera, CameraView
from homeassistant.components.camera.const import DOMAIN as CAMERA_DOMAIN
from homeassistant.components.hassio.ingress import _websocket_forward
from homeassistant.components.http import HomeAssistantView
from homeassistant.config_entries import ConfigEntry
Expand All @@ -18,6 +20,7 @@
CONF_URL,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.network import get_url
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import HomeAssistantType, ConfigType, ServiceCallType
Expand Down Expand Up @@ -102,6 +105,15 @@ async def dash_cast(call: ServiceCallType):
hass.services.async_register(DOMAIN, "create_link", create_link, CREATE_LINK_SCHEMA)
hass.services.async_register(DOMAIN, "dash_cast", dash_cast, DASH_CAST_SCHEMA)

# 6. Register get_camera_stream_source endpoint
component: EntityComponent[Camera] = hass.data[CAMERA_DOMAIN]

if component is None:
_LOGGER.error("Camera integration not initialized")
return False

hass.http.register_view(CameraStreamSourceView(component))

return True


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

return ws_server

class CameraStreamSourceView(CameraView):
"""Camera view to get stream source."""

url = "/api/webrtc/camera_stream_source/{entity_id}"
name = "api:webrtc:camera_stream_source"
requires_auth = True

async def handle(self, request: web.Request, camera: Camera) -> web.Response:
"""Return the camera stream source as plain text."""
stream_source = await camera.stream_source()
if stream_source is None:
raise web.HTTPNotFound()
return web.Response(text=stream_source)
3 changes: 2 additions & 1 deletion custom_components/webrtc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"dependencies": [
"http",
"lovelace"
"lovelace",
"camera"
],
"requirements": [],
"version": "v3.1.0",
Expand Down

0 comments on commit 982884f

Please sign in to comment.