33import time
44import uuid
55from pathlib import Path
6- from typing import Union
6+ from typing import Union , cast
77from urllib .parse import urlencode , urljoin
88
99import homeassistant .helpers .config_validation as cv
1010import voluptuous as vol
1111from aiohttp import web
1212from 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
1416from homeassistant .components .hassio .ingress import _websocket_forward
1517from homeassistant .components .http import HomeAssistantView
1618from 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
178190async 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