use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class GenericBluetoothHandler method updateThingChannels.
private void updateThingChannels() {
List<Channel> channels = //
device.getServices().stream().flatMap(//
service -> service.getCharacteristics().stream()).flatMap(characteristic -> {
logger.trace("{} processing characteristic {}", address, characteristic.getUuid());
CharacteristicHandler handler = getCharacteristicHandler(characteristic);
List<Channel> chans = handler.buildChannels();
List<ChannelUID> chanUids = chans.stream().map(Channel::getUID).collect(Collectors.toList());
for (ChannelUID channel : chanUids) {
channelHandlers.put(channel, handler);
}
handlerToChannels.put(handler, chanUids);
return chans.stream();
}).collect(Collectors.toList());
ThingBuilder builder = editThing();
boolean changed = false;
for (Channel channel : channels) {
logger.trace("{} attempting to add channel {}", address, channel.getLabel());
// we only want to add each channel, not replace all of them
if (getThing().getChannel(channel.getUID()) == null) {
changed = true;
builder.withChannel(channel);
}
}
if (changed) {
updateThing(builder.build());
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class BeaconBluetoothHandler method initialize.
@Override
public void initialize() {
try {
address = new BluetoothAddress(getConfig().get(BluetoothBindingConstants.CONFIGURATION_ADDRESS).toString());
} catch (IllegalArgumentException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getLocalizedMessage());
return;
}
Bridge bridge = getBridge();
if (bridge == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Not associated with any bridge");
return;
}
BridgeHandler bridgeHandler = bridge.getHandler();
if (!(bridgeHandler instanceof BluetoothAdapter)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Associated with an unsupported bridge");
return;
}
adapter = (BluetoothAdapter) bridgeHandler;
try {
deviceLock.lock();
device = adapter.getDevice(address);
device.addListener(this);
} finally {
deviceLock.unlock();
}
ThingBuilder builder = editThing();
boolean changed = false;
for (Channel channel : createDynamicChannels()) {
// we only want to add each channel, not replace all of them
if (getThing().getChannel(channel.getUID()) == null) {
builder.withChannel(channel);
changed = true;
}
}
if (changed) {
updateThing(builder.build());
}
updateStatus(ThingStatus.UNKNOWN);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class EnOceanBaseThingHandler method updateChannels.
protected void updateChannels() {
@NonNull List<@NonNull Channel> channelList = new LinkedList<>(this.getThing().getChannels());
Collection<EEPType> eeps = getEEPTypes();
if (eeps == null) {
return;
}
// First remove channels which are no longer supported by current selected eeps of thing
AtomicBoolean channelListChanged = new AtomicBoolean(channelList.removeIf(channel -> !eeps.stream().anyMatch(eep -> eep.isChannelSupported(channel))));
// Next create supported channels of each selected eep
eeps.stream().flatMap(eep -> eep.GetSupportedChannels().keySet().stream().map(id -> new SimpleEntry<>(id, eep))).forEach(entry -> {
String channelId = entry.getKey();
EnOceanChannelDescription cd = entry.getValue().GetSupportedChannels().get(channelId);
if (cd == null) {
return;
}
// if we do not need to auto create channel => skip
if (!cd.autoCreate) {
return;
}
// if we already created a channel with the same type and id => skip
if (channelList.stream().anyMatch(channel -> cd.channelTypeUID.equals(channel.getChannelTypeUID()) && channelId.equals(channel.getUID().getId()))) {
return;
}
// create channel and add it to the channelList
Channel channel = ChannelBuilder.create(new ChannelUID(this.getThing().getUID(), channelId), cd.acceptedItemType).withConfiguration(entry.getValue().getChannelConfig(channelId)).withType(cd.channelTypeUID).withKind(cd.isStateChannel ? ChannelKind.STATE : ChannelKind.TRIGGER).withLabel(cd.label).build();
channelList.add(channel);
channelListChanged.set(true);
if (!cd.isStateChannel) {
lastEvents.putIfAbsent(channelId, "");
}
});
if (channelListChanged.get()) {
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(channelList);
updateThing(thingBuilder.build());
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class HarmonyDeviceHandler method updateButtonPressChannel.
/**
* Updates the buttonPress channel with the available buttons as option states.
*/
private void updateButtonPressChannel(@Nullable HarmonyConfig harmonyConfig) {
ChannelTypeUID channelTypeUID = new ChannelTypeUID(getThing().getUID().getAsString() + ":" + CHANNEL_BUTTON_PRESS);
if (harmonyConfig == null) {
logger.debug("Cannot update {} when HarmonyConfig is null", channelTypeUID);
return;
}
logger.debug("Updating {}", channelTypeUID);
List<StateOption> states = getButtonStateOptions(harmonyConfig);
ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, "Send Button Press", "String").withDescription("Send a button press to device " + getThing().getLabel()).withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withOptions(states).build()).build();
factory.addChannelType(channelType);
Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), CHANNEL_BUTTON_PRESS), "String").withType(channelTypeUID).build();
// replace existing buttonPress with updated one
List<Channel> newChannels = new ArrayList<>();
for (Channel c : getThing().getChannels()) {
if (!c.getUID().equals(channel.getUID())) {
newChannels.add(c);
}
}
newChannels.add(channel);
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(newChannels);
updateThing(thingBuilder.build());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class GuntamaticHandler method parseAndInit.
private void parseAndInit(String html) {
String[] daqdesc = html.split("\\n");
List<Channel> channelList = new ArrayList<>();
// make sure that static channels are present
for (String channelID : staticChannelIDs) {
Channel channel = thing.getChannel(channelID);
if (channel == null) {
logger.warn("Static Channel '{}' is not present: remove and re-add Thing", channelID);
} else {
channelList.add(channel);
}
}
// add dynamic channels, based on data provided by Guntamatic Heating System
for (int i = 0; i < daqdesc.length; i++) {
String[] param = daqdesc[i].split(";");
String label = param[0].replace("C02", "CO2");
if (!"reserved".equals(label)) {
String channel = toLowerCamelCase(replaceUmlaut(label));
label = label.substring(0, 1).toUpperCase() + label.substring(1);
String unitStr = ((param.length == 1) || param[1].isBlank()) ? "" : param[1].trim();
Unit<?> unit = guessUnit(unitStr);
boolean channelInitialized = channels.containsValue(channel);
if (!channelInitialized) {
String itemType;
String pattern;
String type = types.get(i);
if (type == null) {
type = "";
}
if ("boolean".equals(type)) {
itemType = CoreItemFactory.SWITCH;
pattern = "";
} else if ("integer".equals(type)) {
itemType = guessItemType(unit);
pattern = "%d";
if (unit != null) {
pattern += " %unit%";
}
} else if ("float".equals(type)) {
itemType = guessItemType(unit);
pattern = "%.2f";
if (unit != null) {
pattern += " %unit%";
}
} else if ("string".equals(type)) {
itemType = CoreItemFactory.STRING;
pattern = "%s";
} else {
if (unitStr.isBlank()) {
itemType = CoreItemFactory.STRING;
pattern = "%s";
} else {
itemType = guessItemType(unit);
pattern = "%.2f";
if (unit != null) {
pattern += " %unit%";
}
}
}
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channel);
guntamaticChannelTypeProvider.addChannelType(channelTypeUID, channel, itemType, "Guntamatic " + label, false, pattern);
Channel newChannel = ChannelBuilder.create(new ChannelUID(thing.getUID(), channel), itemType).withType(channelTypeUID).withKind(ChannelKind.STATE).withLabel(label).build();
channelList.add(newChannel);
channels.put(i, channel);
if (unit != null) {
units.put(i, unit);
}
logger.debug("Supported Channel: Idx: '{}', Name: '{}'/'{}', Type: '{}'/'{}', Unit: '{}', Pattern '{}' ", String.format("%03d", i), label, channel, type, itemType, unitStr, pattern);
}
}
}
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(channelList);
updateThing(thingBuilder.build());
channelsInitialized = true;
}
Aggregations