@@ -292,6 +292,13 @@ which calls the ``myservice.flash_light`` service with the indicated parameters.
292
292
parameter values could be any Python expression, and this call could be inside a loop, an if
293
293
statement or any other Python code.
294
294
295
+ Starting in HASS 2023.7, services can return a response, which is a dictionary of values. You need
296
+ to set the keyword parameter ``return_response=True `` to get the response. For example:
297
+
298
+ .. code :: python
299
+
300
+ service_response = myservice.flash_light(light_name = " front" , light_color = " red" , return_response = True )
301
+
295
302
The function ``service.call(domain, name, **kwargs) `` can also be used to call a service when you
296
303
need to compute the domain or service name dynamically. For example, the above service could also be
297
304
called by:
@@ -302,8 +309,9 @@ called by:
302
309
303
310
When making a service call, either using the ``service.call `` function or the service name as the
304
311
function, you can optionally pass the keyword argument ``blocking=True `` if you would like to wait
305
- for the service to finish execution before continuing execution in your function. You can also
306
- specify a timeout for a blocking service call using the ``limit=<number_of_seconds> `` parameters.
312
+ for the service to finish execution before continuing execution in your function. If the service
313
+ returns a response (new feature starting in HASS 2023.7), it will be returned by the call if you set
314
+ the optional keyword parameter ``return_response=True ``.
307
315
308
316
Firing events
309
317
-------------
@@ -919,15 +927,20 @@ Notice that `read_file` is called like a regular function, and it automatically
919
927
``task.executor ``, which runs the compiled native python function in a new thread, and
920
928
then returns the result.
921
929
922
- @service(service_name, ...)
923
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^
930
+ @service(service_name, ..., supports_response="none" )
931
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
924
932
925
933
The ``@service `` decorator causes the function to be registered as a service so it can be called
926
934
externally. The string ``service_name `` argument is optional and defaults to ``"pyscript.FUNC_NAME" ``,
927
935
where ``FUNC_NAME `` is the name of the function. You can override that default by specifying
928
936
a string with a single period of the form ``"DOMAIN.SERVICE" ``. Multiple arguments and multiple
929
937
``@service `` decorators can be used to register multiple names (eg, aliases) for the same function.
930
938
939
+ The ``supports_response `` keyword argument can be set to one of three string values: ``"none" `` (the
940
+ default), ``"only" ``, or ``"optional" ``, depending on whether the service provides no response, always
941
+ provides a response, or sometimes provides a response. Services responses were added in HASS 2023.7.
942
+ The service response is a dictionary returned by the function.
943
+
931
944
Other trigger decorators like ``@state_active `` and ``@time_active `` don't affect the service.
932
945
Those still allow state, time or other triggers to be specified in addition.
933
946
@@ -1057,11 +1070,12 @@ or ``float()``). Attributes keep their native type.
1057
1070
Service Calls
1058
1071
^^^^^^^^^^^^^
1059
1072
1060
- ``service.call(domain, name, blocking=False, limit=10 , **kwargs) ``
1073
+ ``service.call(domain, name, blocking=False, return_response=False , **kwargs) ``
1061
1074
calls the service ``domain.name `` with the given keyword arguments as parameters. If ``blocking ``
1062
1075
is ``True ``, pyscript will wait for the service to finish executing before continuing the current
1063
- routine, or will wait a maximum of the number of seconds specified in the ``limit `` keyword
1064
- argument.
1076
+ routine. If the service returns a response, set ``return_response `` to ``True `` (which also
1077
+ causes blocking), and the response will be returned by the function call. Note that starting
1078
+ in HASS 2023.7, the blocking timeout parameter ``limit `` is no longer supported.
1065
1079
``service.has_service(domain, name) ``
1066
1080
returns whether the service ``domain.name `` exists.
1067
1081
0 commit comments