Skip to content
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

Add simplified actions for searching and playing to Squeezebox #129858

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from

Conversation

peteS-UK
Copy link
Contributor

@peteS-UK peteS-UK commented Nov 4, 2024

Breaking change

Proposed change

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

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

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Nov 4, 2024

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

Code owner commands

Code owners of squeezebox 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 squeezebox 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) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@emontnemery
Copy link
Contributor

@peteS-UK Please fill in the "Proposed change" section of the PR description, in particular to explain if and how this PR is related to other open squeeze box PRs. Please click the "Ready for review" button once you've done that 👍

@emontnemery emontnemery marked this pull request as draft November 8, 2024 14:48
),
},
"async_call_query",
async_call_query_helper,
supports_response=SupportsResponse.OPTIONAL,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we modify the existing service?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, so slightly long answer... Currently, we have 2 general services, call_query and call_method which can be used to call any function in the squeezebox api either with a response or without. The existing process for returning data when using call_query is to set a state attribute rather than using the response_variable approach, I presume because this was done long before the response_variable approach existed. We've had request to add the response_variable approach for call_query and had an earlier PR on this. However, joostelek wouldn't approve that PR until we also made it easier for people to perform common actions (e.g. like search and play) without detailed knowledge of the API. Whilst implementing this new simpler services, it doesn't make sense to have a different return approach to call_query as that would be confusing. However, it also doesn't seem to make sense to introduce these new services, where people will either adapt or build new scripts/automations etc. using the attribute approach, which we'd like to migrate away from over time. However, we also can't remove that approach short term as it's quite widely used and would be a nasty breaking change. So, the approach here is to modify call_query to allow it to support both the old attribute approach or, if response_variable is specified, then to use the modern return approach. Also, this means that people using these new services are more likely to use the modern approach, but they can also use the attribute approach if that's easier in the short term.

vol.Optional(ATTR_TAGS): cv.string,
},
async_search_service_helper,
supports_response=SupportsResponse.OPTIONAL,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the response optional and not required? Why would someone search and not care about the response?

Copy link
Contributor Author

@peteS-UK peteS-UK Nov 8, 2024

Choose a reason for hiding this comment

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

because currently we return attributes to send the response, so by default, current integrations don't return a response. Making this required would make this a significant breaking change.

@@ -523,8 +581,11 @@ async def async_call_method(
await self._player.async_query(*all_params)

async def async_call_query(
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit weird to repurpose the service handle like this IMHO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did that to enable easy access to serviceCall.return_response whilst keeping the changes to existing code to a minimum.

if response:
_res: ServiceResponse = query_result
return _res
self._query_result = query_result
self.async_write_ha_state()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really relevant for the search and play services?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

call_query is a general purpose api. I just use it for search. It's not used by play.

Comment on lines 617 to 623
case "albums":
_param = [
"0",
str(return_items),
("tags:" + tags) if tags else "tags:laay",
"search:" + search_string if search_string is not None else "",
]
Copy link
Contributor

Choose a reason for hiding this comment

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

There's too much repetition going on here, maybe match on the command is not a good fit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm. I can compress this, but don't you think this approach makes it most readable - i.e. for an album, these are the items, for a favorite, it's these... Otherwise, it'll end up as multiple different tests (if albums or genres or ... then this structure, but then another test for the different tags for each items where required and different structures for favorites and players. This seems to be the most readable, but happy to hear your thoughts of course.

homeassistant/components/squeezebox/media_player.py Outdated Show resolved Hide resolved
Comment on lines +81 to +87
options:
- album
- artist
- track
- playlist
- genre
- favorite
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use yaml anchors to avoid the repetitions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure what you mean here. There aren't repetitions. Search is albums, artists (plural), whereas play is album, artist (singular). You search albums, but you play an album (at least that's the way the API works.)

@peteS-UK
Copy link
Contributor Author

peteS-UK commented Nov 8, 2024

@peteS-UK Please fill in the "Proposed change" section of the PR description, in particular to explain if and how this PR is related to other open squeeze box PRs. Please click the "Ready for review" button once you've done that 👍

Hi @emontnemery Erik. Happy to provide more info, but not sure what you mean by this. THe proposed changes sections curently is ...

Add simplified actions to allow searching and playing without knowledge of the Squeezebox API. This replaces PR 127824 . This PR was driven by conversations on #123404. Values returned from these new actions are returned as variables if the response_variable is specified.

NB: A documentation PR is required.

I've added more details ...

PR 128187 was a first iteration of this which just provided search rather than search and play. Also, subsequent other refactoring to the squeezebox api made updating and merging that PR messy, hence easier to open a new one.

PR 123404 was the original PR suggested to add the response_variable approach to the current attribute response approach. If we take this PR forwards, we will close that PR (which would need refactoring anyway now). We could seperate the two PRs of course as we originally had (one to add response_variable and anotehr to add these services), but joostelek didn't want to approve 123404 until we'd added simple services, but adding those services without also doing 123404 doesn't make sense as we want people to use the new return approach with the new services and it doesn't make sense to have 2 different return approaches for call_query vs. search and play. Hence I've combined the 2 into one PR.

@peteS-UK peteS-UK marked this pull request as ready for review November 8, 2024 15:55
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