Skip to content

Commit

Permalink
Add support MJPEG camera as entity source
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 17, 2024
1 parent aa9b203 commit 42decc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
17 changes: 11 additions & 6 deletions custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import voluptuous as vol
from aiohttp import web
from aiohttp.web_exceptions import HTTPUnauthorized, HTTPGone, HTTPNotFound
from homeassistant.components.camera import async_get_image
from homeassistant.components import camera
from homeassistant.components.hassio.ingress import _websocket_forward
from homeassistant.components.http import HomeAssistantView
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -165,10 +165,15 @@ async def ws_connect(hass: HomeAssistantType, params: dict) -> str:
assert server.available, "WebRTC server not available"
server = "http://localhost:1984/"

if name := params.get("entity"):
src = await utils.get_stream_source(hass, name)
assert src, f"Can't get URL for {name}"
query = {"src": src, "name": name}
if entity_id := params.get("entity"):
src = await camera.async_get_stream_source(hass, entity_id)
if src is None:
# build link to MJPEG stream
if state := hass.states.get(entity_id):
if token := state.attributes.get("access_token"):
src = f"{get_url(hass)}/api/camera_proxy_stream/{entity_id}?token={token}"
assert src, f"Can't get URL for {entity_id}"
query = {"src": src, "name": entity_id}
elif src := params.get("url"):
if "{{" in src or "{%" in src:
src = Template(src, hass).async_render()
Expand Down Expand Up @@ -199,7 +204,7 @@ async def ws_poster(hass: HomeAssistantType, params: dict) -> web.Response:

if poster.startswith("camera."):
# support entity_id as poster
image = await async_get_image(hass, poster)
image = await camera.async_get_image(hass, poster)
return web.Response(body=image.content, content_type=image.content_type)

if poster.startswith("image."):
Expand Down
10 changes: 0 additions & 10 deletions custom_components/webrtc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,6 @@ async def validate_binary(hass: HomeAssistant) -> Optional[str]:
return filename


# noinspection PyTypeChecker
async def get_stream_source(hass: HomeAssistant, entity: str) -> str:
try:
component: EntityComponent = hass.data["camera"]
camera: Camera = next(e for e in component.entities if e.entity_id == entity)
return await camera.stream_source()
except Exception:
return None


async def init_resource(hass: HomeAssistant, url: str, ver: str) -> bool:
"""Add extra JS module for lovelace mode YAML and new lovelace resource
for mode GUI. It's better to add extra JS for all modes, because it has
Expand Down

0 comments on commit 42decc3

Please sign in to comment.