Dynamically add channels defined in XML

I would like to add channels at runtime depending on the capability of the device.

Now I understand that I can add channels dynamically using:

ThingBuilder thingBuilder = editThing();
ChannelUID channelUID = new ChannelUID(getThing().getUID(), capability.getChannel());
ChannelTypeUID channelTypeUID = new ChannelTypeUID(capability.getChannel());
Channel channel = ChannelBuilder.create(channelUID, “String”).build();
thingBuilder.withChannel(channel);
updateThing(thingBuilder.build());

But I would like to avoid defining the channels from scratch, instead adding channelstypes already available in the binding XML definitions in ESH-INF

Is there a way to archive that?

Note… the not elegant way would be to always add them to the thing from the start and to remove all not needed channels later onwards, but that seems quite confusing to me for a user, if channels are there and minutes later they are gone (when the thing is fully operational and we know which channels to add/remove).

See ChannelTypeProvider. This is Java interface which allows you to supply more channel kinds or even calculate them at runtime.

1 Like

Yes it’s possible. You can define the channel types in xml and than create channels referring to this channel type. For things add extensible to the thing-type definition with the list of channel types. That allows the user to manually add those channels.
See the networkupstools binding for how to implement such channels. It implements both channels from types as well as creates channels. The NUTDynamicChannelFactory.java is a starting point.

1 Like