use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class EventFilterHandler method updateChannelSet.
/**
* Checks existing channels, adds missing and removes extraneous channels from the Thing.
*
* @param config The validated Configuration of the Thing.
*/
private void updateChannelSet(EventFilterConfiguration config) {
final ThingHandlerCallback handlerCallback = getCallback();
if (handlerCallback == null) {
return;
}
final List<Channel> currentChannels = getThing().getChannels();
final ThingBuilder thingBuilder = editThing();
BigDecimal maxEvents = config.maxEvents;
if (maxEvents == null || maxEvents.compareTo(BigDecimal.ZERO) < 1) {
thingBuilder.withoutChannels(currentChannels);
updateThing(thingBuilder.build());
return;
}
generateExpectedChannelList(maxEvents.intValue());
synchronized (resultChannels) {
currentChannels.stream().filter((Channel current) -> {
String currentGroupId = current.getUID().getGroupId();
if (currentGroupId == null) {
return true;
}
for (ResultChannelSet channelSet : resultChannels) {
if (channelSet.resultGroup.getId().contentEquals(currentGroupId)) {
return false;
}
}
return true;
}).forEach((Channel toDelete) -> {
thingBuilder.withoutChannel(toDelete.getUID());
});
resultChannels.stream().filter((ResultChannelSet current) -> {
return (getThing().getChannelsOfGroup(current.resultGroup.toString()).size() == 0);
}).forEach((ResultChannelSet current) -> {
for (ChannelBuilder builder : handlerCallback.createChannelBuilders(current.resultGroup, GROUP_TYPE_UID)) {
Channel currentChannel = builder.build();
Channel existingChannel = getThing().getChannel(currentChannel.getUID());
if (existingChannel == null) {
thingBuilder.withChannel(currentChannel);
}
}
});
}
updateThing(thingBuilder.build());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class ZoneTemperatureControlHandler method loadChannel.
private synchronized void loadChannel() {
List<Channel> newChannelList = new ArrayList<>(1);
if (currentChannelID != null) {
newChannelList.add(ChannelBuilder.create(new ChannelUID(this.getThing().getUID(), currentChannelID), DsChannelTypeProvider.getItemType(currentChannelID)).withType(new ChannelTypeUID(BINDING_ID, currentChannelID)).build());
}
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(newChannelList);
updateThing(thingBuilder.build());
logger.debug("load channel: {} with item: {}", currentChannelID, DsChannelTypeProvider.getItemType(currentChannelID));
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class TrackerHandler method createBasicDistanceChannel.
/**
* Create distance channel for measuring the distance between the tracker and the szstem.
*/
private void createBasicDistanceChannel() {
@Nullable ThingHandlerCallback callback = getCallback();
if (callback != null) {
// find the system distance channel
ChannelUID systemDistanceChannelUID = new ChannelUID(thing.getUID(), CHANNEL_DISTANCE_SYSTEM_ID);
Channel systemDistance = thing.getChannel(CHANNEL_DISTANCE_SYSTEM_ID);
ChannelBuilder channelBuilder = null;
if (systemDistance != null) {
if (!systemDistance.getConfiguration().get(CONFIG_REGION_CENTER_LOCATION).equals(sysLocation.toFullString())) {
logger.trace("Existing distance channel for system. Changing system location config parameter: {}", sysLocation.toFullString());
channelBuilder = callback.editChannel(thing, systemDistanceChannelUID);
Configuration configToUpdate = systemDistance.getConfiguration();
configToUpdate.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
channelBuilder.withConfiguration(configToUpdate);
} else {
logger.trace("Existing distance channel for system. No change.");
}
} else {
logger.trace("Creating missing distance channel for system.");
Configuration config = new Configuration();
config.put(ConfigHelper.CONFIG_REGION_NAME, CHANNEL_DISTANCE_SYSTEM_NAME);
config.put(CONFIG_REGION_CENTER_LOCATION, sysLocation.toFullString());
config.put(ConfigHelper.CONFIG_REGION_RADIUS, CHANNEL_DISTANCE_SYSTEM_RADIUS);
config.put(ConfigHelper.CONFIG_ACCURACY_THRESHOLD, 0);
channelBuilder = callback.createChannelBuilder(systemDistanceChannelUID, CHANNEL_TYPE_DISTANCE).withLabel("System Distance").withConfiguration(config);
}
// update the thing with system distance channel
if (channelBuilder != null) {
List<Channel> channels = new ArrayList<>(thing.getChannels());
if (systemDistance != null) {
channels.remove(systemDistance);
}
channels.add(channelBuilder.build());
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
logger.debug("Distance channel created for system: {}", systemDistanceChannelUID);
}
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class Ipx800v3Handler method discoverAttributes.
protected void discoverAttributes() {
final Map<String, String> properties = new HashMap<>();
properties.put(Thing.PROPERTY_VENDOR, "GCE Electronics");
properties.put(Thing.PROPERTY_FIRMWARE_VERSION, statusFile.getElement(StatusEntry.VERSION));
properties.put(Thing.PROPERTY_MAC_ADDRESS, statusFile.getElement(StatusEntry.CONFIG_MAC));
updateProperties(properties);
ThingBuilder thingBuilder = editThing();
List<Channel> channels = new ArrayList<>(getThing().getChannels());
PortDefinition.asStream().forEach(portDefinition -> {
int nbElements = statusFile.getMaxNumberofNodeType(portDefinition);
for (int i = 0; i < nbElements; i++) {
createChannels(portDefinition, i, channels);
}
});
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class LinuxInputHandler method delayedSetup.
@Override
boolean delayedSetup() throws IOException {
ThingBuilder customizer = editThing();
List<Channel> newChannels = new ArrayList<>();
newChannels.add(keyChannel);
EvdevDevice newDevice = new EvdevDevice(config.path);
for (EvdevDevice.Key o : newDevice.enumerateKeys()) {
String name = o.getName();
Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), CHANNEL_GROUP_KEYPRESSES_ID, name), CoreItemFactory.CONTACT).withLabel(name).withType(CHANNEL_TYPE_KEY_PRESS).build();
channels.put(o.getCode(), channel);
newChannels.add(channel);
}
if (Objects.equals(defaultLabel, thing.getLabel())) {
customizer.withLabel(newDevice.getName());
}
customizer.withChannels(newChannels);
Map<String, String> props = getProperties(Objects.requireNonNull(newDevice));
customizer.withProperties(props);
updateThing(customizer.build());
for (Channel channel : newChannels) {
updateState(channel.getUID(), OpenClosedType.OPEN);
}
if (config.enable) {
updateStatus(ThingStatus.ONLINE);
}
device = newDevice;
return config.enable;
}
Aggregations