How to add discovery to the skeleton binding

I’m trying to move a nodejs script and some item/rules into a proper binding. I’ve never written anything in Java before and the last time I used eclipse was a few years ago and I was programming using C for micro-controllers, so everything is kind of new. I’m not sure if I just not familiar with java, how eclipse works or just don’t know how to write bindings.

I managed to get the environment working and the skeleton binding working. I wanted to then implement a discovery services and looked at binding that implemented a discovery service in a similar way. So the skeleton seemed to be missing any of the files for discovery. So initially I sort of manually copy over stuff from a binding with discovery but started manually editing the files. But it seems a bit kind of messy and I’m not sure I’m doing the right thing. It just felt like the way things were set up what I really should have done is just change a config setting or file to add discovery and then it would properly add the default classes and files for discovery, or something along those lines. That might be something that is normal in java/eclipse but since I have no proper experience I jut don’t know.

I’m not sure what your asking. But if you want specific advice please do post a link to something you’ve build. For example push your branch with your work to github and post the link here.

Say I want to add the discovery capability to the skeleton binding, do I manually add files according the docs/copy and edit from an existing binding or is there a more automated better way to do that?

Anyway here is the github, I just copied the milight discovery stuff over and started editing that, renaming milight to tuya, etc. It may work but kind of feels like there is a much easier and better way to add discovery to the binding, so I’m asking is this what I should be doing or is there an easier way.

Yes. It basically comes down to creating a class and extending AbstractDiscoveryService and implementing the start and stop methods with your binding specific code. Copying can be a starting point, but the milight does a lot of stuff so it might do more than you need and therefor it would take more work to strip the code than to start with an empty class. (But that depends on how much it would differ from the milight implementation). But it can help to see how other discovery services have been implemented. For example see TPLinkSmartHomeDiscoveryService.

This is what I do. This is probably stating the obvious, but the binding you select to model after should match the method of discovery used by the product(s) that your binding will support (e.g. uPnp, UDP datagram, etc.).

If there’s a better way, I haven’t found it yet. :smile:

Edit: One additional thought… Of the 8 bindings I’ve written so far, each had its own unique way of discovering devices. Perhaps one of the reasons why there’s not a better way to add discovery.

As Discovery is such an important part of oH2 and onwards, IMO it would make sense for there to be Discovery skeleton code as well.

As a new developer to OH, having this key feature as part of the skeleton would aid understanding of what’s important to implement.

Thanks. I’ve been using vscode and have various extensions installed. If say I was going to add a class one of those extensions would probably create a file and make a skeleton template for that class. So I was wondering if there was something like that with eclipse/java, but it seems like there isn’t.

You could create an editor template for inserting the skeleton (and for the other openHAB-related things you do frequently).

I have a couple of these for license headers, etc.

The current skeleton is being worked on. Because it was part of ESH but hasn’t been moved to openHAB-core yet I’ve created a pull request for it. But as there is also work done on moving to an pure maven build system the idea is to also change the skeleton to generate these new maven supporting build files. My idea is to after getting the basics included again to extend the skeleton and add more (example) code. Maybe a discovery example should also be part of this.

There was recently also a different topic about generating code to also help developers, see

I think this is a great idea. But how do you handle the type of discovery service desired (plain vanilla, uPnP, mDNS, etc.), or do you give the user the choice at skeleton creation time?

I completely missed this post. Glad someone is thinking about this.

Hi all,

I’ve implemented the AbstractDiscoveryService in my binding but in the bindings that I checked (TPLinkSmartThingsDiscovery, Network and Astro bindings) the constructor is used only in the tests.
I just wonder if I have to register/unregister it somewhere upon creation/destruction of bridge handlers or it uses some reflection to find the particular implementation of discovery?

Best regards,
Konstantin

Oops… just found this: https://www.openhab.org/docs/developer/bindings/#implementing-a-discovery-service
It should answer all my questions. Sorry for the premature question.

1 Like