Skip to content

Add a test for services defined in closures - #934

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
jfriend00:test-service-closures
Dec 1, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
jfriend00:test-service-closures

Conversation

@jfriend00

Copy link
Copy Markdown
Contributor

Following the discussion in #896, I moved my per-instance triggers into trigger factories: a method called from __init__ defines the triggers and keeps them in an attribute, as in the class example now in the "Trigger Closures" section of the docs. I wanted per-instance services the same way, each named from the object it serves, for example one per thermostat. That works on 2.1.1 and on master, but no current test defines a @service inside a function or class, so this adds one, modeled on test_trigger_closures.

test_service_closures covers:

  • services made by a factory function, and by a class in a method called from __init__, each named with an f-string from its own variables
  • each service works once Home Assistant has started
  • dropping the last reference to one removes that service and leaves the others

It passes on master at ad31306. Removing the Function.service_register() call in ServiceDecorator.start(), or the Function.service_remove() call in stop(), makes it fail. It was run on Windows, with local stand-ins for Home Assistant's Unix-only imports, so CI will be its first run on Linux.

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jfriend00, a useful test. It passes merged with current master, and it fails if ServiceDecorator.stop() doesn't remove the service. Approving.

@craigbarratt
craigbarratt merged commit 9a55f97 into custom-components:master Dec 1, 2026
6 checks passed
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