Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image entity support for poster #619

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions custom_components/webrtc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import time
import uuid
from pathlib import Path
from typing import Union
from typing import Union, cast
from urllib.parse import urlencode, urljoin

import homeassistant.helpers.config_validation as cv
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.image import ImageEntity
from homeassistant.components.camera import async_get_image as camera_get_image
from homeassistant.components.image import _async_get_image as image_get_image
from homeassistant.components.hassio.ingress import _websocket_forward
from homeassistant.components.http import HomeAssistantView
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -173,6 +175,16 @@ async def ws_connect(hass: HomeAssistantType, params: dict) -> str:
raise Exception("Missing url or entity")

return urljoin("ws" + server[4:], "api/ws") + "?" + urlencode(query)

def _get_image_from_entity_id(hass: HomeAssistantType, entity_id: str) -> ImageEntity:
"""Get camera component from entity_id."""
if (component := hass.data.get("image")) is None:
raise Exception("Image integration not set up")

if (image := component.get_entity(entity_id)) is None:
raise Exception("Image not found")

return cast(ImageEntity, image)


async def ws_poster(hass: HomeAssistantType, params: dict) -> web.Response:
Expand All @@ -184,8 +196,15 @@ 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_get_image(hass, poster)
return web.Response(body=image.content, content_type=image.content_type)

if poster.startswith("image."):
# support entity_id as poster
image_entity = _get_image_from_entity_id(hass, poster)
image = await image_entity.async_image()
_LOGGER.debug(f"webrtc image_entity: {image_entity} - {len(image)}")
return web.Response(body=image, content_type="image/jpeg")

# support poster from go2rtc stream name
entry = hass.data[DOMAIN]
Expand Down
Loading