-
-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Allow multiple media directories for collection_image #180337
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
Open
karwosts
wants to merge
9
commits into
home-assistant:dev
Choose a base branch
from
karwosts:multi-dir-colleciton
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ebb2a2
Allow multiple media directories for collection_image
karwosts f05edac
Fix a crash browsing /image integration
karwosts f3b21bb
test cleanup
karwosts 3d5d416
more tests
karwosts da5ee57
adjust coverage
karwosts 6b7e6f8
test cleanup
karwosts 38f0007
Potential fix for pull request finding
karwosts ce12583
Disallow selecting the image directory
karwosts 05d2f53
Merge branch 'multi-dir-colleciton' of https://github.com/karwosts/co…
karwosts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| from homeassistant.components.image import ImageEntity | ||
| from homeassistant.components.media_player import ( | ||
| BrowseError, | ||
| BrowseMedia, | ||
| MediaClass, | ||
| async_process_play_media_url, | ||
| ) | ||
|
|
@@ -36,11 +37,15 @@ async def async_setup_entry( | |
| ) -> None: | ||
| """Set up the Collection Image image entities.""" | ||
| media = entry.data[CONF_MEDIA] | ||
| if isinstance(media, dict): | ||
| content_ids = [media["media_content_id"]] | ||
| else: | ||
| content_ids = [item["media_content_id"] for item in media] | ||
| async_add_entities( | ||
| [ | ||
| CollectionImageImageEntity( | ||
| name=entry.title, | ||
| media_content_id=media["media_content_id"], | ||
| media_content_ids=content_ids, | ||
| unique_id=entry.entry_id, | ||
| hass=hass, | ||
| ) | ||
|
|
@@ -58,7 +63,7 @@ class CollectionImageImageEntity(ImageEntity): | |
| def __init__( | ||
| self, | ||
| name: str, | ||
| media_content_id: str, | ||
| media_content_ids: list[str], | ||
| unique_id: str, | ||
| hass: HomeAssistant, | ||
| ) -> None: | ||
|
|
@@ -67,11 +72,10 @@ def __init__( | |
| self.path = None | ||
| self._attr_unique_id = unique_id | ||
| self._attr_name = name | ||
| self.media_content_id = media_content_id | ||
| self.media_content_ids = media_content_ids | ||
|
|
||
| async def get_next_image(self) -> None: | ||
| """Update the image entity with the next image from the source media.""" | ||
|
|
||
| """Update the image entity with a random image from configured media sources.""" | ||
| self._cached_image = None | ||
|
|
||
| def set_unavailable() -> None: | ||
|
|
@@ -81,59 +85,76 @@ def set_unavailable() -> None: | |
| self._attr_image_url = UNDEFINED | ||
| self.async_write_ha_state() | ||
|
|
||
| try: | ||
| media = await async_browse_media(self.hass, self.media_content_id) | ||
| except BrowseError as err: | ||
| if not self._unavailable_logged: | ||
| _LOGGER.info("%s: %s", self.entity_id, str(err)) | ||
| set_unavailable() | ||
| return | ||
| images: list[BrowseMedia] = [] | ||
|
|
||
| if media.children and ( | ||
| filtered := [ | ||
| item for item in media.children if item.media_class == MediaClass.IMAGE | ||
| ] | ||
| ): | ||
| child = random.choice(filtered) | ||
| for media_content_id in self.media_content_ids: | ||
| try: | ||
| resolved = await async_resolve_media( | ||
| self.hass, child.media_content_id, self.entity_id | ||
| ) | ||
| except Unresolvable as err: | ||
| media = await async_browse_media(self.hass, media_content_id) | ||
| except BrowseError as err: | ||
| if not self._unavailable_logged: | ||
| _LOGGER.info("%s: %s", self.entity_id, str(err)) | ||
| set_unavailable() | ||
| return | ||
|
|
||
| if resolved.url: | ||
| self.path = None | ||
| self._attr_image_url = async_process_play_media_url( | ||
| self.hass, resolved.url | ||
| _LOGGER.info( | ||
| "%s: Unable to browse %s: %s", | ||
| self.entity_id, | ||
| media_content_id, | ||
| err, | ||
| ) | ||
| continue | ||
|
karwosts marked this conversation as resolved.
|
||
|
|
||
| if media.children: | ||
| images.extend( | ||
| item | ||
| for item in media.children | ||
| if item.media_class == MediaClass.IMAGE | ||
| ) | ||
| else: | ||
| self.path = resolved.path | ||
| self._attr_image_url = UNDEFINED | ||
|
|
||
| self._attr_content_type = resolved.mime_type | ||
| self._attr_available = True | ||
| self._attr_image_last_updated = dt_util.utcnow() | ||
| if self._unavailable_logged: | ||
|
|
||
| if not images: | ||
| if not self._unavailable_logged: | ||
| _LOGGER.info( | ||
| "%s: Has become available again", | ||
| "%s: No valid images in %s", | ||
| self.entity_id, | ||
|
Comment on lines
+110
to
114
Member
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 don't think it's worthwhile to add extra complexity to avoid just a single extra log line in a pathological case. Right now we get one log per unbrowseable directory, and an overall error if we can't find anything at all. That seems reasonable to me. |
||
| self.media_content_ids, | ||
| ) | ||
| self._unavailable_logged = False | ||
| self.async_write_ha_state() | ||
| set_unavailable() | ||
| return | ||
|
|
||
| if not self._unavailable_logged: | ||
| _LOGGER.info( | ||
| "%s: No valid images in %s", | ||
| child = random.choice(images) | ||
|
|
||
| try: | ||
| resolved = await async_resolve_media( | ||
| self.hass, | ||
| child.media_content_id, | ||
| self.entity_id, | ||
| self.media_content_id, | ||
| ) | ||
| set_unavailable() | ||
| return | ||
| except Unresolvable as err: | ||
| if not self._unavailable_logged: | ||
| _LOGGER.info( | ||
| "%s: Unable to resolve %s: %s", | ||
| self.entity_id, | ||
| child.media_content_id, | ||
| err, | ||
| ) | ||
| set_unavailable() | ||
| return | ||
|
|
||
| if resolved.url: | ||
| self.path = None | ||
| self._attr_image_url = async_process_play_media_url( | ||
| self.hass, | ||
| resolved.url, | ||
| ) | ||
| else: | ||
| self.path = resolved.path | ||
| self._attr_image_url = UNDEFINED | ||
|
|
||
| self._attr_content_type = resolved.mime_type | ||
| self._attr_available = True | ||
| self._attr_image_last_updated = dt_util.utcnow() | ||
|
|
||
| if self._unavailable_logged: | ||
| _LOGGER.info("%s: Has become available again", self.entity_id) | ||
|
|
||
| self._unavailable_logged = False | ||
| self.async_write_ha_state() | ||
|
|
||
| @override | ||
| async def async_added_to_hass(self) -> None: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.