Skip to content

Commit f5c0c80

Browse files
authored
Merge pull request #619 from fuatakgun/image
feat: add image entity support for poster
2 parents f620e42 + c02c850 commit f5c0c80

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

custom_components/webrtc/__init__.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import time
44
import uuid
55
from pathlib import Path
6-
from typing import Union
6+
from typing import Union, cast
77
from urllib.parse import urlencode, urljoin
88

99
import homeassistant.helpers.config_validation as cv
1010
import voluptuous as vol
1111
from aiohttp import web
1212
from aiohttp.web_exceptions import HTTPUnauthorized, HTTPGone, HTTPNotFound
13-
from homeassistant.components.camera import async_get_image
13+
from homeassistant.components.image import ImageEntity
14+
from homeassistant.components.camera import async_get_image as camera_get_image
15+
from homeassistant.components.image import _async_get_image as image_get_image
1416
from homeassistant.components.hassio.ingress import _websocket_forward
1517
from homeassistant.components.http import HomeAssistantView
1618
from homeassistant.config_entries import ConfigEntry
@@ -173,6 +175,16 @@ async def ws_connect(hass: HomeAssistantType, params: dict) -> str:
173175
raise Exception("Missing url or entity")
174176

175177
return urljoin("ws" + server[4:], "api/ws") + "?" + urlencode(query)
178+
179+
def _get_image_from_entity_id(hass: HomeAssistantType, entity_id: str) -> ImageEntity:
180+
"""Get camera component from entity_id."""
181+
if (component := hass.data.get("image")) is None:
182+
raise Exception("Image integration not set up")
183+
184+
if (image := component.get_entity(entity_id)) is None:
185+
raise Exception("Image not found")
186+
187+
return cast(ImageEntity, image)
176188

177189

178190
async def ws_poster(hass: HomeAssistantType, params: dict) -> web.Response:
@@ -184,8 +196,15 @@ async def ws_poster(hass: HomeAssistantType, params: dict) -> web.Response:
184196

185197
if poster.startswith("camera."):
186198
# support entity_id as poster
187-
image = await async_get_image(hass, poster)
199+
image = await camera_get_image(hass, poster)
188200
return web.Response(body=image.content, content_type=image.content_type)
201+
202+
if poster.startswith("image."):
203+
# support entity_id as poster
204+
image_entity = _get_image_from_entity_id(hass, poster)
205+
image = await image_entity.async_image()
206+
_LOGGER.debug(f"webrtc image_entity: {image_entity} - {len(image)}")
207+
return web.Response(body=image, content_type="image/jpeg")
189208

190209
# support poster from go2rtc stream name
191210
entry = hass.data[DOMAIN]

0 commit comments

Comments
 (0)