Skip to content

Add support for searching media to Kodi - #161681

Draft
anishsane wants to merge 2 commits into
home-assistant:devfrom
anishsane:kodi_search_media
Draft

Add support for searching media to Kodi#161681
anishsane wants to merge 2 commits into
home-assistant:devfrom
anishsane:kodi_search_media

Conversation

@anishsane

@anishsane anishsane commented Jan 27, 2026

Copy link
Copy Markdown
Contributor

Kodi provides a mechanism to query the available movies, TV shows etc using jsonrpc.

This change adds a logic to match them against the search query using rapidfuzz.

This change only adds the support for searching movies and TV shows. I do not have music hosted in my kodi instance to test it. But the logic can be extended in the same way.

The search behaviour is as follows:
For movies, return the matching movie(s).
For TV shows, return the first unwatched episode for the matching show(s).

Sample usage:
"Play Avengers on Kodi"
"Play Brooklyn nine nine on Kodi"

Proposed change

Add initial HassMediaSearchAndPlay support for Kodi.

Kodi provides a mechanism to query the available movies, TV shows etc
using jsonrpc.

This change adds a logic to match them against the search query using
rapidfuzz.

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

  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

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. (NA)
  • 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:

@home-assistant

Copy link
Copy Markdown
Contributor

Hey there @OnFreund, mind taking a look at this pull request as it has been labeled with an integration (kodi) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of kodi can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign kodi Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@epenet epenet changed the title Add initial HassMediaSearchAndPlay support for Kodi. Add initial HassMediaSearchAndPlay support for Kodi Jan 27, 2026
@anishsane
anishsane force-pushed the kodi_search_media branch 2 times, most recently from 1d681d7 to 26bbb7f Compare February 1, 2026 06:01
Kodi provides a mechanism to query the available movies, TV shows etc
using jsonrpc.

This change adds a logic to match them against the search query using
rapidfuzz.

This change only adds the support for searching movies and TV shows.
I do not have music hosted in my kodi instance to test it. But the logic
can be extended in the same way.

The search behaviour is as follows:
For movies, return the matching movie(s).
For TV shows, return the first unwatched episode for the matching
show(s).

Sample usage:
"Play Avengers on Kodi"
"Play Brooklyn nine nine on Kodi"
If the requester (say an llm conversation agent) sends an invalid
value for media class (e.g. 'tv' instead of 'tv_show') the current
logic will filter it out entirely. Instead we can default to the
allowed list when this happens.
@joostlek joostlek changed the title Add initial HassMediaSearchAndPlay support for Kodi Add support for searching media to Kodi Mar 16, 2026

@joostlek joostlek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So while I think this is a neat change, I consider the matching of the titles a service specific, so would it make sense to move this to pykodi? The library seems to be maintained by codeowners, so we might be able to move it there and make our code less service specific

@home-assistant
home-assistant Bot marked this pull request as draft March 16, 2026 12:28
@home-assistant

Copy link
Copy Markdown
Contributor

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@anishsane

Copy link
Copy Markdown
Contributor Author

So while I think this is a neat change, I consider the matching of the titles a service specific, so would it make sense to move this to pykodi? The library seems to be maintained by codeowners, so we might be able to move it there and make our code less service specific

I would normally agree with you. However, the pykodi library does not seem to be maintained. The last checkin was almost 3 years ago.

@joostlek

Copy link
Copy Markdown
Member

Sent the maintainer a message

@joostlek

Copy link
Copy Markdown
Member

He just said he's open to such a PR :)

@anishsane

Copy link
Copy Markdown
Contributor Author

Thanks! Will update.

@anishsane

Copy link
Copy Markdown
Contributor Author

I have created a PR in PyKodi. OnFreund/PyKodi#21
Once it gets approved, I will update the PR here.

@github-actions

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions Bot added the stale label May 26, 2026
@anishsane

Copy link
Copy Markdown
Contributor Author

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes. Thank you for your contribution!

I have created a PR in PyKodi. OnFreund/PyKodi#21
Once it gets approved, I will update the PR here.

@github-actions github-actions Bot removed the stale label May 26, 2026
@issue-triage-workflows

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@anishsane

Copy link
Copy Markdown
Contributor Author

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes. Thank you for your contribution!

I have created a PR in PyKodi. OnFreund/PyKodi#21
Once it gets approved, I will update the PR here.

@anishsane

Copy link
Copy Markdown
Contributor Author

@OnFreund , @joostlek, can we conclude on the approach in this PR vs the approach from the PR in the PyKodi library?

If this current PR's approach is the preferred approach, I can rebase my change to the latest code base.
If the approach from the PR in PyKodi is the preferred approach, I can refresh this PR after the PyKodi PR is merged.

@OnFreund

Copy link
Copy Markdown
Contributor

Since the "search" functionality isn't really related to Kodi in any way, and is just a filter on the browse results, I don't think it belongs in the library, and would prefer finding a solution here.

@emontnemery

emontnemery commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

There's no other concrete media player integration which implements the search functionality in the integration, here's a summary of integrations which support search.

┌──────────────────────────┬────────────────────────────────────────────────────────────────────────────────────┐
│       Integration        │                                     Mechanism                                      │
├──────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ sonos                    │ Native UPnP search via SoCo get_music_library_information(search_term=…) (device   │
│                          │ does the fuzzy)                                                                    │
├──────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ squeezebox               │ Native LMS search: query via pysqueezebox (+ exact media_filter_classes narrowing  │
│                          │ client-side)                                                                       │
├──────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ music_assistant          │ Native mass.music.search() (+ substring fallback only for "within album/playlist") │
├──────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ jellyfin                 │ Native search_media_items(term=…) — pure passthrough                               │
├──────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ radio_browser            │ Native radios.search()                                                             │
│ (media_source)           │                                                                                    │
├──────────────────────────┼────────────────────────────────────────────────────────────────────────────────────┤
│ immich (media_source)    │ Native async_smart_search() (semantic)                                             │
└──────────────────────────┴────────────────────────────────────────────────────────────────────────────────────┘

Hence, I agree with @joostlek it makes sense to add support for searching kodi libraries to the supporting library.

@OnFreund

OnFreund commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

@emontnemery thanks, really helpful. Note that all of these use a native search mechanism. This is not the case here - there's no native mechanism - all we're doing is filtering the results, which, btw, could be useful for other media integrations.

@emontnemery

Copy link
Copy Markdown
Contributor

@emontnemery thanks, really helpful. Note that all of these use a native search mechanism. This is not the case here - there's no native mechanism - all we're doing is filtering the results, which, btw, could be useful for other media integrations.

Yeah, I had a quick look at the kodi RPC docs and was quite surprised to see there doesn't seem to be a way to search. Not sure how active kodi development is, but it seems like a reasonable feature to add? How does the native kodi app and webui work without search, is everything focused around scrolling through lists?

all we're doing is filtering the results, which, btw, could be useful for other media integrations

Do you mean something like a fallback implementation in the base class which implements search by fetching everything and then filtering? It seems wildly inefficient to implement it like that so I'm not sure we'd accept such a proposal.

@anishsane

anishsane commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Would it be reasonable to add the current filtering approach for now and switch to real search when kodi adds it?

btw, kodi application does have a search option. However, it shows the search results on the screen. It does not return the search results.
Even the kodi remote app seems to be doing a client-side filtering.

The VideoLibrary.Get* (e.g. VideoLibrary.GetMovies) supports a contains operator, but not a fuzzy search. The contains (substring match) search is hardly useful when combined with a voice assistant, because it would need a precise substring.

@OnFreund

OnFreund commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Not sure how active kodi development is, but it seems like a reasonable feature to add?

That's a good idea, but given the Kodi release cycle, the highly optimistic case is for this to be available in over a year (probably more like early 2028).

Do you mean something like a fallback implementation in the base class which implements search by fetching everything and then filtering? It seems wildly inefficient to implement it like that so I'm not sure we'd accept such a proposal.

I was thinking more opt-in, but you raise a good point: if we're reluctant to add this to other integrations, there's no reason this should be any different. I can see 3 options:

  1. We say no to filtering full stop
  2. We allow filtering for any integration that wants it
  3. We allow filtering based on specific criteria (e.g. limited number of results)

In either case, this isn't integration-specific, and definitely not part of the Kodi library.

@emontnemery

Copy link
Copy Markdown
Contributor

Would it be reasonable to add the current filtering approach for now and switch to real search when kodi adds it?

IMO it has to be in @OnFreund's PyKodi library unless there's a compelling reason for adding search to the media player base class. If we want to go for the latter, there needs to be an architecture proposal explaining why it's a good idea.

@OnFreund

OnFreund commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

IMO it has to be in @OnFreund's PyKodi library

The one thing I'm absolutely sure of is that in-client filtering of results does not belong in the Kodi library (see my comment above).

@emontnemery

Copy link
Copy Markdown
Contributor

IMO it has to be in @OnFreund's PyKodi library

The one thing I'm absolutely sure of is that in-client filtering of results does not belong in the Kodi library (see my comment above).

It belongs in a library outside of the core integration, I don't care if it's in your library or some other library.
The job of the supporting libraries used by Home Assistant integrations is not to be a thin shim over whatever native API the device or service we connect to are using. The job of the supporting libraries is to provide an API which matches the Home Assistant abstractions (within reason).

As I wrote, the alternative would be to add search to the Home Assistant media player base class, but then you need to show why that's a good idea.

@OnFreund

OnFreund commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

As I wrote, the alternative would be to add search to the Home Assistant media player base class, but then you need to show why that's a good idea.

I'm not claiming it's a good idea. My claim is that there's nothing special about this integration. This is an external filtering mechanism, and it's either something we're willing to support and make available to other integrations (i.e. a good idea), or something that we think no integrations should do (a bad idea). I don't think that "it's a bad idea in general but a good idea for Kodi" makes sense.

@OnFreund

OnFreund commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

To rephrase - I have no opinion on whether it's a good or bad idea, but I do have a strong opinion that it's not a good idea for this integration if it's a bad idea for others.

@anishsane

Copy link
Copy Markdown
Contributor Author

Hi,

Do we have any consensus on the preferred approach?

  1. Adding the filtering function in pykodi and calling it from kodi integration (PR in pykodi)
  2. Adding the filter_by argument in pykodi's individual get_* functions (e.g. get_movies, get_episodes etc) (Earlier version of the pykodi PR)
  3. Adding the filtering logic in the kodi integration (This PR)
  4. Adding the filtering function in media_player base integration that kodi integration will call (And possibly other future integrations too)
  5. Something else?

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.

4 participants