Send command from a binding?

Check out https://github.com/openhab/openhab-addons/blob/dependabot/maven/bundles/org.openhab.binding.chromecast/com.fasterxml.jackson.core-jackson-databind-2.9.10.1/bundles/org.openhab.io.neeo/src/main/java/org/openhab/io/neeo/NeeoService.java

for how to get it injected.

EDIT: you have to have it on some class marked as a @Component then the @Reference will inject the proper implementation right after initiation. If this is a true binding, usually your handler factory is a great place to have the event publisher injected and then you pass it to your handler.

Here you can find three different examples of how to injection a service into a bundle. The org.openhab.io.neeo - linked above - uses the “Method Injection”. Nowadays the “Constructor Injection” should be preferred.

Learn something new everyday - didn’t even know about constructor/field injection!

Hi @cweitkamp and @tmrobert8 many thanks for your advice.

@cweitkamp I hope you don’t mind, but notwithstanding your preference, I decided to go with the Method Injection, since I had followed @tmrobert8 suggestion to build this functionality onto the Thing Handler Factory component. And since the handler factory inherits from BaseHandlerFactory, and since BaseHandlerFactory already has its own constructor and @Activate and @Deactivate annotated methods, I did not want to get myself confused by overriding the constructor or multi- stacking @Override annotated methods on top of @Activate methods etc. ( or something like that :slight_smile: )

@AndrewFG
Since my sony addon is starting review - I changed over to the constructor injection - really simplified the class and it works fine. Here’s an example: https://github.com/tmrobert8/openhab-addons/blob/1234-Sony/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyHandlerFactory.java

Nope, I do not mind about that. I am just trying to help by giving hints for possible solutions. You have to figure out the best option for your use case.

I am afraid that is not true. The BaseHandlerFacrory does neither has a constructor nor annotated activate() / deactivate() methods. Even the JavaDoc states clearly that a sub class can overide them but must call super.... to initialize the BaseHandlerFactory properly.

I also think a constructor based injection is the cleaner way, just because we then don’t need a @Nullable which then needs to get checked on every access.