-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Show WebRTC cameras that also support HLS in the media browser #108796
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
Changes from 3 commits
4c61d80
8f69dff
7b72172
c9d93a0
27929e1
ea791fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||||
| """Expose cameras as media sources.""" | ||||||||||||||
| from __future__ import annotations | ||||||||||||||
|
|
||||||||||||||
| import asyncio | ||||||||||||||
|
|
||||||||||||||
| from homeassistant.components.media_player import BrowseError, MediaClass | ||||||||||||||
| from homeassistant.components.media_source.error import Unresolvable | ||||||||||||||
| from homeassistant.components.media_source.models import ( | ||||||||||||||
|
|
@@ -71,36 +73,42 @@ async def async_browse_media( | |||||||||||||
|
|
||||||||||||||
| can_stream_hls = "stream" in self.hass.config.components | ||||||||||||||
|
|
||||||||||||||
| # Root. List cameras. | ||||||||||||||
| component: EntityComponent[Camera] = self.hass.data[DOMAIN] | ||||||||||||||
| children = [] | ||||||||||||||
| not_shown = 0 | ||||||||||||||
| for camera in component.entities: | ||||||||||||||
| async def _media_source_for_camera(camera: Camera) -> BrowseMediaSource | None: | ||||||||||||||
| stream_type = camera.frontend_stream_type | ||||||||||||||
|
|
||||||||||||||
| if stream_type is None: | ||||||||||||||
| content_type = camera.content_type | ||||||||||||||
|
|
||||||||||||||
| elif can_stream_hls and stream_type == StreamType.HLS: | ||||||||||||||
| elif can_stream_hls: | ||||||||||||||
| content_type = FORMAT_CONTENT_TYPE[HLS_PROVIDER] | ||||||||||||||
|
|
||||||||||||||
| if stream_type != StreamType.HLS: | ||||||||||||||
| source = await camera.stream_source() | ||||||||||||||
| if not source: | ||||||||||||||
| return None | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work for this to be a single statement? (Given this is 3 levels of nested ifs in an inline function)
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took advantage of the ability to return early in a successful case to refactor a bit differently. Let me know what you think. |
||||||||||||||
| else: | ||||||||||||||
| not_shown += 1 | ||||||||||||||
| continue | ||||||||||||||
|
|
||||||||||||||
| children.append( | ||||||||||||||
| BrowseMediaSource( | ||||||||||||||
| domain=DOMAIN, | ||||||||||||||
| identifier=camera.entity_id, | ||||||||||||||
| media_class=MediaClass.VIDEO, | ||||||||||||||
| media_content_type=content_type, | ||||||||||||||
| title=camera.name, | ||||||||||||||
| thumbnail=f"/api/camera_proxy/{camera.entity_id}", | ||||||||||||||
| can_play=True, | ||||||||||||||
| can_expand=False, | ||||||||||||||
| ) | ||||||||||||||
| return None | ||||||||||||||
|
|
||||||||||||||
| return BrowseMediaSource( | ||||||||||||||
| domain=DOMAIN, | ||||||||||||||
| identifier=camera.entity_id, | ||||||||||||||
| media_class=MediaClass.VIDEO, | ||||||||||||||
| media_content_type=content_type, | ||||||||||||||
| title=camera.name, | ||||||||||||||
| thumbnail=f"/api/camera_proxy/{camera.entity_id}", | ||||||||||||||
| can_play=True, | ||||||||||||||
| can_expand=False, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # Root. List cameras. | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just drop this while we're here
Suggested change
|
||||||||||||||
| component: EntityComponent[Camera] = self.hass.data[DOMAIN] | ||||||||||||||
| results = await asyncio.gather( | ||||||||||||||
| *(_media_source_for_camera(camera) for camera in component.entities), | ||||||||||||||
| return_exceptions=True, | ||||||||||||||
| ) | ||||||||||||||
| children = [ | ||||||||||||||
| result for result in results if isinstance(result, BrowseMediaSource) | ||||||||||||||
| ] | ||||||||||||||
| not_shown = len(results) - len(children) | ||||||||||||||
| return BrowseMediaSource( | ||||||||||||||
| domain=DOMAIN, | ||||||||||||||
| identifier=None, | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think everything looks good to go here, but now i'm thinking about one more idea to further streamline this, but not positive it would work out: Could the scope of this function be to just filter out the not relevant cameras and leave the browse media source building all together how it was? It could just get the eligible cameras in one pass then build the sources in a simple loop together with building the root.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could return the camera instead of
BrowseMediaSource, and then iterate over the results to build the sources, but I'm not sure I see why it would be better?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its possibly a style preference, but my thinking is: in general, reducing # of verical lines of inline functions or reducing the scope as much as possible seems positive. Having the
BrowseMediaSourcebuilding closer together together seems logical as well. One exception may be if there is shared logic on both parts but i think the existing stream type filtering looks more complex than it needs to beThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit tricky because we also have to recalculate the content type (or return a tuple).
Arguably refactoring the creation of the
BrowseMediaSourceto a separate function would achieve a similar result (see my latest commit)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that looks good. What do you think about gutting all the existing vertical whitespace? It seems like it makes this harder to follow given it has so many branches and is in an inline function. Collapsing vertically will also help make this simpler to grok quickly.