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 Oct 12, 2023
1 parent 69cbcda commit c3322b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 @@ -19,6 +21,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 @@ -113,6 +116,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)

# 7. Serve camera_stream_source API
camera_component: EntityComponent[Camera] = hass.data[CAMERA_DOMAIN]

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

hass.http.register_view(CameraStreamSourceView(camera_component))

return True


Expand Down Expand Up @@ -266,3 +278,18 @@ async def get(self, request: web.Request, filename: str):

body = await r.read()
return web.Response(body=body, content_type=r.content_type)


class CameraStreamSourceView(CameraView):
"""Camera view to get the 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.3.0",
Expand Down

0 comments on commit c3322b5

Please sign in to comment.