Repository navigation
Service via decorator manager - #899
craigbarratt merged 5 commits into
Conversation
|
FYI: the PR removing the legacy decorator subsystem is ready. It depends on this one, so I'll open it once this is merged. Happy to make any changes needed here. @craigbarratt a lot has piled up since 2.1.1. How about releasing 2.2.0 after this is merged, and removing the legacy subsystem in the next release? That way users still have |
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks @dmamelin, the pipeline change is a nice cleanup. First, an apology: in #870 I said it was fine for @state_active, @time_active and @task_unique to apply to @service. I've been mulling it over since, and I can't see a good reason to gate an explicit service call with the two @*_active decorators. A caller asked for the call, and the docs' vacation manual-override example relies on services not being gated. Sorry for changing direction after you did the work.
Requested changes:
- Keep
@task_uniqueon services, but skip@state_active/@time_active. They're the onlyTriggerHandlerDecorators, so a class flag onTriggerDecorator(e.g.gated_by_trigger_handlers = True, set toFalseinServiceDecorator) checked inFunctionDecoratorManager.dispatch()would do it. Two related details:TriggerHandlerDecorator.validate()should only count gated triggers, so@service @state_activewith no other trigger is still an error.- Service calls then won't touch
@time_active's hold-off state, so thehold_off_send_lastnote and its docs can go. The docs can keep saying@*_activedon't affect services.
- A blocking caller can hang. If the call task is cancelled before it starts, or a call handler raises, the result future is never set. A done-callback on
data.call_taskthat resolves the future if it isn't done would cover both cases. - Breaking change 3: silently ignoring undeclared service data removes the feedback for a typo in a field name. I'd prefer to keep the error for services.
- Breaking change 4: agreed, failing the call is the right behavior. Please keep it.
- Please document
kwargs=on@service, or reject it.
|
Thanks @craigbarratt for the review! Before I get to the other points, I'd like to settle what
Skipping I don't see a reason to restrict
|
|
One more point in favour of keeping |
|
Thanks @dmamelin, good points, and I agree that a uniform pipeline is appealing, which is why I was initially supportive. But I've been thinking about it more, and I still think
|
… the caller hanging Add RequestResponseDecorator, a base for a decorator that answers a request with the result of the call it starts. It resolves the request's future when the call returns, raises, is skipped or cancelled, or fails before the function runs, and cancels the call when the caller goes away. @webhook_handler is built on it: a call that fails or is cancelled before the function runs is answered with 500/503 right away instead of after the timeout. Like Home Assistant's REST API, it doesn't cancel the function when the client disconnects. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A service call starts the call directly (FunctionDecoratorManager.start_call), so trigger handlers (@state_active, @time_active) don't apply to it, while call handlers (@task_unique) do. ServiceDecorator is a RequestResponseDecorator: - an exception raised by the function fails the service call with a HomeAssistantError (the exception is still logged); - a call skipped or cancelled by @task_unique, or a function returning None, gives an empty response when one is requested, like Home Assistant scripts; - cancelling the caller cancels the function; - each call fires pyscript_running, like triggers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…A services Home Assistant now checks the service data before the call: a field the function doesn't declare, or a missing parameter without a default value, fails the call (a function with **kwargs accepts any other field). trigger_type and context aren't service data. The generated description follows the same parameters: no positional-only, trigger_type or context fields, keyword-only ones included, parameters without a default value marked required, simple default values shown, and a selector from a simple type annotation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8255605 to
81ab7a4
Compare
|
@craigbarratt thanks for the review. I've addressed all the points, rebuilt the history into 5 commits on the current master and updated the description.
Also, without a yaml docstring the service fields are now generated from the signature: required fields, constant default values (never computed ones), and a UI input from a simple type annotation. See the description for details. |
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks @dmamelin, this addresses everything, and making @service an explicit call through start_call() is cleaner than the flag I suggested. The shared RequestResponseDecorator is a nice bonus for @webhook_handler, and generating the fields and selectors from the signature is a real improvement for the UI.
I also checked a few cases outside the tests, and they all behave well:
- a call with
target:fillsentity_id; - a non-blocking call whose function raises is just logged;
- calling a pyscript service from pyscript with an undeclared field raises in the caller;
- a required positional-only parameter fails the call with a clear
HomeAssistantError.
I'll fix a small doc nit in a follow-up commit: "description that appears is in the Services tab of the Developer Tools page" has a stray "is", and the tab is now called Actions.
Route
@servicethrough the decorator managerFollow-up to #870.
Until now,
@serviceran the function itself, bypassing the decorator manager. Now a service call goes through the manager as an explicit call, so it shares the call pipeline with triggers.Changes
@servicegoes through the decorator manager. A service call starts the call directly (FunctionDecoratorManager.start_call()), without the trigger handlers:@state_activeand@time_activedon't apply, as before, while call handlers (@task_unique) now do.RequestResponseDecorator(decorators/base.py) answers a request with the outcome of the call it starts.@serviceand@webhook_handlerare built on it.**kwargsaccepts any other field.@webhook_handlerno longer waits for the timeout when the call ends without a result: a call cancelled while running (e.g. by a newer@task_uniquecall) or before the function starts gets 503, and a call handler that raises gives 500, right away instead of 504 after the timeout.Service description from the signature
When the docstring isn't a yaml description, the service fields shown in Developer Tools → Actions and in the automation editor are now generated from the function's signature:
bool, number or string constant default value is shown as the field's default; a computed one isn't, so it's never published;bool,int,float,str,listordictannotation (alsoX | None,Optional[X],list[int]) gives the field a matching input (toggle, integer box, number box, text, YAML object).gives a required text field
entity_id, an integer boxlevelwith default 50, a number boxtransitionand a togglenotify. The previous generation dates from before Home Assistant had selectors and a UI for service calls (2021): it only listed the parameters, each with anargument <name>description and no selector, so the UI editor doesn't show them at all and the service data has to be written in YAML.Breaking changes (
@service)HomeAssistantError, which stops a calling automation or script unlesscontinue_on_erroris set. Previously it was only logged and the call succeeded.TypeErrorwas logged and the call succeeded, and undeclared names from the trigger kwargs (value,var_name,payload,topic, …) were dropped silently and the function ran.TypeErrorwas logged and the call succeeded.@task_uniqueapplies to service calls. A service call can cancel a running trigger call and vice versa, and withkill_me=Truethe service call is skipped.Other changes
Nonegives{}when a response is requested. Previously Home Assistant raised an error.