2022-02-08 22:32:02 +00:00
|
|
|
"""Expose cameras as media sources."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-12 18:06:27 +00:00
|
|
|
from homeassistant.components.media_player import BrowseError, MediaClass
|
2022-02-08 22:32:02 +00:00
|
|
|
from homeassistant.components.media_source.error import Unresolvable
|
|
|
|
from homeassistant.components.media_source.models import (
|
|
|
|
BrowseMediaSource,
|
|
|
|
MediaSource,
|
|
|
|
MediaSourceItem,
|
|
|
|
PlayMedia,
|
|
|
|
)
|
2022-05-15 15:58:57 +00:00
|
|
|
from homeassistant.components.stream import FORMAT_CONTENT_TYPE, HLS_PROVIDER
|
2022-02-08 22:32:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
|
|
|
|
|
|
|
from . import Camera, _async_stream_endpoint_url
|
2022-04-11 23:27:27 +00:00
|
|
|
from .const import DOMAIN, StreamType
|
2022-02-08 22:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_get_media_source(hass: HomeAssistant) -> CameraMediaSource:
|
|
|
|
"""Set up camera media source."""
|
|
|
|
return CameraMediaSource(hass)
|
|
|
|
|
|
|
|
|
|
|
|
class CameraMediaSource(MediaSource):
|
|
|
|
"""Provide camera feeds as media sources."""
|
|
|
|
|
|
|
|
name: str = "Camera"
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant) -> None:
|
|
|
|
"""Initialize CameraMediaSource."""
|
|
|
|
super().__init__(DOMAIN)
|
|
|
|
self.hass = hass
|
|
|
|
|
|
|
|
async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
|
|
|
|
"""Resolve media to a url."""
|
2022-09-15 09:53:00 +00:00
|
|
|
component: EntityComponent[Camera] = self.hass.data[DOMAIN]
|
|
|
|
camera = component.get_entity(item.identifier)
|
2022-02-08 22:32:02 +00:00
|
|
|
|
|
|
|
if not camera:
|
|
|
|
raise Unresolvable(f"Could not resolve media item: {item.identifier}")
|
|
|
|
|
2022-02-19 16:22:51 +00:00
|
|
|
if (stream_type := camera.frontend_stream_type) is None:
|
2022-02-14 16:04:19 +00:00
|
|
|
return PlayMedia(
|
|
|
|
f"/api/camera_proxy_stream/{camera.entity_id}", camera.content_type
|
|
|
|
)
|
|
|
|
|
|
|
|
if "stream" not in self.hass.config.components:
|
|
|
|
raise Unresolvable("Stream integration not loaded")
|
2022-02-08 22:32:02 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
url = await _async_stream_endpoint_url(self.hass, camera, HLS_PROVIDER)
|
|
|
|
except HomeAssistantError as err:
|
2022-10-26 18:37:31 +00:00
|
|
|
# Handle known error
|
|
|
|
if stream_type != StreamType.HLS:
|
|
|
|
raise Unresolvable(
|
|
|
|
"Camera does not support MJPEG or HLS streaming."
|
|
|
|
) from err
|
2022-02-08 22:32:02 +00:00
|
|
|
raise Unresolvable(str(err)) from err
|
|
|
|
|
|
|
|
return PlayMedia(url, FORMAT_CONTENT_TYPE[HLS_PROVIDER])
|
|
|
|
|
|
|
|
async def async_browse_media(
|
|
|
|
self,
|
|
|
|
item: MediaSourceItem,
|
|
|
|
) -> BrowseMediaSource:
|
|
|
|
"""Return media."""
|
|
|
|
if item.identifier:
|
|
|
|
raise BrowseError("Unknown item")
|
|
|
|
|
2022-02-26 09:02:13 +00:00
|
|
|
can_stream_hls = "stream" in self.hass.config.components
|
2022-02-08 22:32:02 +00:00
|
|
|
|
|
|
|
# Root. List cameras.
|
2022-09-15 09:53:00 +00:00
|
|
|
component: EntityComponent[Camera] = self.hass.data[DOMAIN]
|
2022-02-08 22:32:02 +00:00
|
|
|
children = []
|
2022-02-25 19:52:14 +00:00
|
|
|
not_shown = 0
|
2022-02-08 22:32:02 +00:00
|
|
|
for camera in component.entities:
|
2022-02-14 16:04:19 +00:00
|
|
|
stream_type = camera.frontend_stream_type
|
2022-02-08 22:32:02 +00:00
|
|
|
|
2022-02-26 09:02:13 +00:00
|
|
|
if stream_type is None:
|
|
|
|
content_type = camera.content_type
|
|
|
|
|
2022-04-11 23:27:27 +00:00
|
|
|
elif can_stream_hls and stream_type == StreamType.HLS:
|
2022-02-26 09:02:13 +00:00
|
|
|
content_type = FORMAT_CONTENT_TYPE[HLS_PROVIDER]
|
|
|
|
|
|
|
|
else:
|
2022-02-25 19:52:14 +00:00
|
|
|
not_shown += 1
|
2022-02-08 22:32:02 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
children.append(
|
|
|
|
BrowseMediaSource(
|
|
|
|
domain=DOMAIN,
|
|
|
|
identifier=camera.entity_id,
|
2022-09-12 18:06:27 +00:00
|
|
|
media_class=MediaClass.VIDEO,
|
2022-02-26 09:02:13 +00:00
|
|
|
media_content_type=content_type,
|
2022-02-08 22:32:02 +00:00
|
|
|
title=camera.name,
|
|
|
|
thumbnail=f"/api/camera_proxy/{camera.entity_id}",
|
|
|
|
can_play=True,
|
|
|
|
can_expand=False,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return BrowseMediaSource(
|
|
|
|
domain=DOMAIN,
|
|
|
|
identifier=None,
|
2022-09-12 18:06:27 +00:00
|
|
|
media_class=MediaClass.APP,
|
2022-02-08 22:32:02 +00:00
|
|
|
media_content_type="",
|
|
|
|
title="Camera",
|
|
|
|
can_play=False,
|
|
|
|
can_expand=True,
|
2022-09-12 18:06:27 +00:00
|
|
|
children_media_class=MediaClass.VIDEO,
|
2022-02-08 22:32:02 +00:00
|
|
|
children=children,
|
2022-02-25 19:52:14 +00:00
|
|
|
not_shown=not_shown,
|
2022-02-08 22:32:02 +00:00
|
|
|
)
|