custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
874 stars 46 forks source link

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

Closed matzman666 closed 1 year ago

matzman666 commented 1 year ago

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

Calling Services:

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:

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 commented 1 year ago

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.