Skip to content

Allow multiple media directories for collection_image - #180337

Open
karwosts wants to merge 9 commits into
home-assistant:devfrom
karwosts:multi-dir-colleciton
Open

Allow multiple media directories for collection_image#180337
karwosts wants to merge 9 commits into
home-assistant:devfrom
karwosts:multi-dir-colleciton

Conversation

@karwosts

@karwosts karwosts commented Aug 26, 2026

Copy link
Copy Markdown
Member

Proposed change

Allow multiple media directories to be specified, instead of just 1. We will then randomize between all images in all selected directories.

Once we support multiple items, we can in the future extend it to picking directories and/or single images as well.

Requires home-assistant/frontend#53815

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Extends Collection Image to select random images across multiple media directories while retaining legacy entry compatibility.

Changes:

  • Enables multi-directory media selection.
  • Aggregates images across configured directories.
  • Adds runtime multi-directory coverage and updates existing tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
config_flow.py Accepts and validates multiple directories.
image.py Aggregates images and selects one randomly.
conftest.py Adds a second mocked media directory.
test_config_flow.py Updates inputs to the new list format.
test_image.py Tests multi-directory image selection.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread homeassistant/components/collection_image/config_flow.py
Comment on lines +110 to 114
if not images:
if not self._unavailable_logged:
_LOGGER.info(
"%s: Has become available again",
"%s: No valid images in %s",
self.entity_id,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

Comment thread homeassistant/components/collection_image/config_flow.py
@karwosts
karwosts marked this pull request as draft August 27, 2026 02:32
Copilot AI review requested due to automatic review settings August 27, 2026 04:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Suppressed comments (3)

Previously missed (2) — in code that hasn't changed since the last review.

tests/components/collection_image/test_config_flow.py:52

  • Add a config-flow test that submits at least two directories. Every updated flow test still supplies a one-item list, so the new loop can stop processing additional selections without any test failing; verify that both directories are browsed and the complete list is stored.
                "media": [
                    {
                        "media_content_id": "media-source://mymedia",
                        "media_content_type": "",
                    }
                ],

homeassistant/components/collection_image/image.py:101

  • Add a multi-source test where one browse raises BrowseError and another source provides images. The existing tests cover either all-success or a single failed source, leaving the newly introduced skip-and-continue behavior unverified.
        for media_content_id in self.media_content_ids:
            try:
                media = await async_browse_media(self.hass, media_content_id)
            except BrowseError as err:
                if not self._unavailable_logged:
                    _LOGGER.info(
                        "%s: Unable to browse %s: %s",
                        self.entity_id,
                        media_content_id,
                        err,
                    )
                continue

homeassistant/components/collection_image/config_flow.py:18

  • Update the config-flow strings to describe selecting multiple directories. strings.json:13 and strings.json:15 still refer to one directory/folder, so the UI will misdescribe this new multi-select field; update the source strings and regenerate the English translation.
        vol.Required(CONF_MEDIA): MediaSelector(
            {"accept": ["directory"], "multiple": True}
        ),

Comment thread homeassistant/components/image/media_source.py Outdated
@karwosts
karwosts marked this pull request as ready for review August 27, 2026 04:16
@karwosts
karwosts requested a review from a team as a code owner August 27, 2026 04:16
Copilot AI review requested due to automatic review settings August 27, 2026 13:51

This comment was marked as duplicate.

Copilot AI review requested due to automatic review settings August 27, 2026 14:07

This comment was marked as duplicate.

Copilot AI review requested due to automatic review settings August 27, 2026 14:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

tests/components/collection_image/conftest.py:112

  • Remove this unused fixture. It is never requested by the test suite and, despite its name and docstring, returns a successful browse result rather than raising BrowseError, which makes the shared test setup misleading.
def browse_media_browse_error() -> BrowseMediaSource:
    """Throws a BrowseError."""

Comment thread homeassistant/components/image/media_source.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@karwosts
karwosts requested review from Copilot and removed request for a team August 27, 2026 14:40

This comment was marked as duplicate.

Copilot AI review requested due to automatic review settings August 27, 2026 18:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

tests/components/collection_image/conftest.py:112

  • Remove this unused and misleading fixture. No test requests it—the actual error behavior is provided directly by browse_side_effect—and this fixture returns normal media despite claiming to raise BrowseError.
def browse_media_browse_error() -> BrowseMediaSource:
    """Throws a BrowseError."""

homeassistant/components/collection_image/image.py:101

  • Add a runtime test where one configured directory raises BrowseError and another still supplies an image. The new skip-and-continue behavior is currently only exercised when every browse fails; the config-flow mixed-source case does not verify that the entity remains available and resolves an image from the working directory.
                continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants