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

Added support for service responses when calling or creating services. #495

Conversation

matzman666
Copy link
Contributor

Added support for returning service responses for both service execution and service creating.

Calling Services:

  • Added argument "return_response" to be passed to the call to the underlying home assistant service API.
  • Added "return" to several places so that executed service calls can actually return values.
  • Added some convenience functionality so that the user does not need to explicitly specify arguments "return_response" or "blocking" when calling a service which returns a response.

The user can now call services and get responses back. This works for both ways of calling services, via service.call() and also when using the service name as a function. Users can now use the argument "return_response = True" when they are interested in getting a response like this:

retval = service.call("mydomain", "myservice", return_response = True, ...)

or

retval = mydomain.myservice(return_response = True, ...)

When the called service has been created with "SupportsResponse.ONLY", then "return_response = True" is automatically assumed when not explicitly given by the user.

Creating service:

  • Decorator @service now accepts a new keyword argument "supports_response" which has the same semantics as the argument of the same name of the underlying home assistant API. When not explicitly given it has a default value of "none".
  • The wrapper function of the custom service implementations now awaits the task and returns the return value.

The user can now use the keyword argument "supports_response" to specify whether the service returns a response when creating a service using the @service decorator:

@service(supports_response = "only")
def myservice():
    return {"response_data1": "somedata1", "response_data2": "somedata2"}

The argument "supports_response" accepts the same values as the underlying home assistant API: "none" when no response is returned, "optional" when optionally a response can be returned or "only" when a response must be returned. Default value of "supports_response" is "none".

@craigbarratt craigbarratt merged commit 9522331 into custom-components:master Jul 29, 2023
4 of 6 checks passed
@craigbarratt
Copy link
Member

craigbarratt commented Jul 29, 2023

Thanks for the PR! I'll make it backward compatible (fix the test failures, then upgrade the tests to 2023.7) and add tests and documentation.

craigbarratt added a commit that referenced this pull request Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants