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 new attribute query_result_json to expose query_result as json in squeezebox #115739

Closed
wants to merge 3 commits into from

Conversation

peteS-UK
Copy link

…. python object

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

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

Hi @peteS-UK

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant bot marked this pull request as draft April 17, 2024 07:16
@home-assistant
Copy link

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.

@home-assistant
Copy link

Hey there @rajlaud, 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.

@rajlaud
Copy link
Contributor

rajlaud commented Apr 23, 2024

This makes sense to me. Can you also update the docs to reflect this change?

@peteS-UK
Copy link
Author

peteS-UK commented Apr 23, 2024 via email

Copy link
Contributor

@rajlaud rajlaud left a comment

Choose a reason for hiding this comment

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

This will be a useful feature for scripting/automation. Thanks @peteS-UK

@peteS-UK
Copy link
Author

peteS-UK commented Apr 23, 2024 via email

Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Why is this needed? There's already a to_json template function which can be used to try parsing the existing query_result to JSON.

@MartinHjelmare MartinHjelmare changed the title Add new attribute query_result_json to expose query_result as json Add new attribute query_result_json to expose query_result as json in squeezebox Apr 28, 2024
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

The service can return the result as a service response if that's needed. But commands that return some state about the media player should probably just be made as sensor entities instead.

Closing this PR as it's not the correct approach.

@peteS-UK
Copy link
Author

peteS-UK commented Apr 28, 2024

Hi Martin. The integration already returns the response as a service, but as a service response as an object. However, if you want to use this service with the API to allow an external app to use this response, it's much easier if the response (e.g. all of the albums on the media server) is available as json. So, the PR is to enable use of the integration with the home assistant API. If there's an existing way to do this easily, then that would be great - only a couple of lines in the integration, so would makes things simple for anyone trying to consume this through the api. For context, this service isn't really returning something about the state - it's to allow arbitrary queries to be passed to the server, and it uses the attributes to return the responses, so it's very useful for external API calls.

@MartinHjelmare
Copy link
Member

You can read about service response here:
https://developers.home-assistant.io/docs/dev_101_services/#response-data

@rajlaud
Copy link
Contributor

rajlaud commented Apr 28, 2024

I'll look into changing squeezebox to use this approach. Will be a breaking change but looks like the right way to handle query results.

@peteS-UK
Copy link
Author

I'll look into changing squeezebox to use this approach. Will be a breaking change but looks like the right way to handle query results.

Raj, I tried to avoid this in the PR to not be breaking and to follow the existing approach. How about we add a optional param to the service (returnResponse perhaps) default to false and return response only if its true, so it's not breaking.

@peteS-UK
Copy link
Author

@rajlaud , Yes, that works fine, and is actually nicer for api since we're getting a return vs. having to poll for the attribute change. I'll send a PR tomorrow.

@peteS-UK
Copy link
Author

Hi @rajlaud . OK, I've done a bit more digging, and there's a weird thing going on... I'd forgotten that the service call in the API is a POST, not a GET, and as such it doesn't return anything - or at least it shouldn't and when digging around, it doesn't return the returned data. However, what it does do, for some reason, is to return the entire state object of the entity if the state is updated. If the state isn't changed, it returns nothing, as I'd expect from a POST. So, for API purposes, the current integration (i.e. writing data to the attribute) actually already works perfectly and there's no need for a change. In fact, if you didn't write to the query_result attribute and just returned data, the POST call in the API doesn't return anything. So, from my perspective, I'd like to just close this one down and leave things as they are - this odd behaviour with the state object is actually just fine as a way of returning the query_result.

@peteS-UK peteS-UK deleted the patch-1 branch April 29, 2024 12:57
@github-actions github-actions bot locked and limited conversation to collaborators Apr 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants