Skip to content

Service via decorator manager - #899

Open
dmamelin wants to merge 5 commits into
custom-components:masterfrom
dmamelin:service-via-decorator-manager
Open

dmamelin wants to merge 5 commits into
custom-components:masterfrom
dmamelin:service-via-decorator-manager

Conversation

@dmamelin

Copy link
Copy Markdown
Contributor

Route @service through the decorator manager

Follow-up to #870. @service now goes through the same DecoratorManager pipeline as the trigger decorators, so it no longer depends on TRIGGER_KWARGS.

Changes

  • ServiceDecorator is now a TriggerDecorator + CallResultHandlerDecorator (like @webhook_handler). The function result is returned through a future.
  • Cancelling the caller (e.g. a stopped or restarted script) still cancels the running function, as the previous await task did: the manager records the call task in DispatchData.call_task, and the service handler cancels it.
  • FunctionDecoratorManager._call() calls handle_call_canceled when a running call is cancelled (e.g. by @task_unique). This also fixes @webhook_handler, which waited for its timeout and now returns 503 immediately.
  • The @service tests are moved to tests/decorators/test_service.py.

Breaking changes

  1. @state_active, @time_active and @task_unique now apply to service calls.
  2. A skipped or cancelled call, or one that returned None, gives {} when a response is requested, like HA scripts. Previously HA core raised "expected a dictionary".
  3. Undeclared service data fields are ignored (the same signature filter as triggers after Decouple DecoratorManager from eval.py TRIGGER_KWARGS allowlist #870). Previously they raised TypeError, unless the name was in TRIGGER_KWARGS.
  4. An exception in the function fails the call with HomeAssistantError. Previously it was only logged and the caller saw success.
  5. Each call fires pyscript_running (logbook) and runs in a child Context.

Notes

  • With @time_active(hold_off_send_last=True), a service call suppressed during the hold-off window is answered right away as skipped, but the function still runs with its data when the window ends (documented).
  • For non-blocking calls, an exception in the function is logged twice: by pyscript and by HA ("Error executing service").
  • As a side effect of @service being a trigger, it accepts kwargs= like other triggers; this is not documented.

Question

@craigbarratt, #4 can stop existing automations that call a failing service. I think failing the call is the correct behavior, but it's easy to revert if you'd rather keep the old one (log only, report success). It's a separate commit.

The service handler waits on a future while the function runs in a separate
task, so cancelling the caller (e.g. a stopped or restarted script) no longer
reached the function. The manager now records the call task in
DispatchData.call_task and the service handler cancels it, as the old
await on the task did.
@dmamelin

Copy link
Copy Markdown
Contributor Author

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 legacy_decorators: true as a fallback for one more release.

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.

1 participant