use of org.openhab.core.thing.binding.builder.ThingBuilder in project addons by smarthomej.
the class Tr064SubHandler method internalInitialize.
private void internalInitialize() {
final Bridge bridge = getBridge();
if (bridge == null) {
return;
}
final Tr064RootHandler bridgeHandler = (Tr064RootHandler) bridge.getHandler();
if (bridgeHandler == null) {
logger.warn("Bridge-handler is null in thing {}", thing.getUID());
return;
}
final SCPDUtil scpdUtil = bridgeHandler.getSCPDUtil();
if (scpdUtil == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Could not get device definitions");
return;
}
final ThingHandlerCallback callback = getCallback();
if (callback == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Could not get callback");
return;
}
if (checkProperties(scpdUtil)) {
// properties set, check channels
ThingBuilder thingBuilder = editThing();
thingBuilder.withoutChannels(thing.getChannels());
Util.checkAvailableChannels(thing, callback, thingBuilder, scpdUtil, config.uuid, deviceType, channels);
updateThing(thingBuilder.build());
// remove connect scheduler
removeConnectScheduler();
soapConnector = bridgeHandler.getSOAPConnector();
isInitialized = true;
installPolling();
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project addons by smarthomej.
the class AbstractSnmpTargetHandlerTest method setup.
protected void setup(ChannelTypeUID channelTypeUID, SnmpChannelMode channelMode, @Nullable SnmpDatatype datatype, @Nullable String onValue, @Nullable String offValue, @Nullable String exceptionValue, @Nullable String unit) {
Map<String, Object> channelConfig = new HashMap<>();
Map<String, Object> thingConfig = new HashMap<>();
mocks = MockitoAnnotations.openMocks(this);
thingConfig.put("hostname", "localhost");
ThingBuilder thingBuilder = ThingBuilder.create(THING_TYPE_TARGET, THING_UID).withLabel("Test thing").withConfiguration(new Configuration(thingConfig));
String itemType = SnmpBindingConstants.CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID) ? "Number" : "String";
channelConfig.put("oid", TEST_OID);
channelConfig.put("mode", channelMode.name());
if (datatype != null) {
channelConfig.put("datatype", datatype.name());
}
if (onValue != null) {
channelConfig.put("onvalue", onValue);
}
if (offValue != null) {
channelConfig.put("offvalue", offValue);
}
if (exceptionValue != null) {
channelConfig.put("exceptionValue", exceptionValue);
}
if (unit != null) {
channelConfig.put("unit", unit);
}
Channel channel = ChannelBuilder.create(CHANNEL_UID, itemType).withType(channelTypeUID).withConfiguration(new Configuration(channelConfig)).build();
thingBuilder.withChannel(channel);
thing = thingBuilder.build();
thingHandler = new SnmpTargetHandler(thing, snmpService);
thingHandler.getThing().setHandler(thingHandler);
thingHandler.setCallback(thingHandlerCallback);
doAnswer(answer -> {
((Thing) answer.getArgument(0)).setStatusInfo(answer.getArgument(1));
return null;
}).when(thingHandlerCallback).statusUpdated(any(), any());
thingHandler.initialize();
verifyStatus(ThingStatus.UNKNOWN);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-core by openhab.
the class ThingHelper method merge.
/**
* Merges the content of a ThingDTO with an existing Thing.
* Where ever the DTO has null values, the content of the original Thing is kept.
* Where ever the DTO has non-null values, these are used.
* In consequence, care must be taken when the content of a list (like configuration, properties or channels) is to
* be updated - the DTO must contain the full list, otherwise entries will be deleted.
*
* @param thing the Thing instance to merge the new content into
* @param updatedContents a DTO which carries the updated content
* @return A Thing instance, which is the result of the merge
*/
public static Thing merge(Thing thing, ThingDTO updatedContents) {
ThingBuilder builder;
if (thing instanceof Bridge) {
builder = BridgeBuilder.create(thing.getThingTypeUID(), thing.getUID());
} else {
builder = ThingBuilder.create(thing.getThingTypeUID(), thing.getUID());
}
// Update the label
if (updatedContents.label != null) {
builder.withLabel(updatedContents.label);
} else {
builder.withLabel(thing.getLabel());
}
// Update the location
if (updatedContents.location != null) {
builder.withLocation(updatedContents.location);
} else {
builder.withLocation(thing.getLocation());
}
// update bridge UID
if (updatedContents.bridgeUID != null) {
builder.withBridge(new ThingUID(updatedContents.bridgeUID));
} else {
builder.withBridge(thing.getBridgeUID());
}
// update thing configuration
if (updatedContents.configuration != null && !updatedContents.configuration.keySet().isEmpty()) {
builder.withConfiguration(new Configuration(updatedContents.configuration));
} else {
builder.withConfiguration(thing.getConfiguration());
}
// update thing properties
if (updatedContents.properties != null) {
builder.withProperties(updatedContents.properties);
} else {
builder.withProperties(thing.getProperties());
}
// Update the channels
if (updatedContents.channels != null) {
for (ChannelDTO channelDTO : updatedContents.channels) {
builder.withChannel(ChannelDTOMapper.map(channelDTO));
}
} else {
builder.withChannels(thing.getChannels());
}
if (updatedContents.location != null) {
builder.withLocation(updatedContents.location);
} else {
builder.withLocation(thing.getLocation());
}
Thing mergedThing = builder.build();
// keep all child things in place on a merged bridge
if (mergedThing instanceof BridgeImpl && thing instanceof Bridge) {
Bridge bridge = (Bridge) thing;
BridgeImpl mergedBridge = (BridgeImpl) mergedThing;
for (Thing child : bridge.getThings()) {
mergedBridge.addThing(child);
}
}
return mergedThing;
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class BaseKeypadHandler method configureChannels.
protected void configureChannels() {
Channel channel;
ChannelTypeUID channelTypeUID;
ChannelUID channelUID;
logger.debug("Configuring channels for keypad {}", integrationId);
List<Channel> channelList = new ArrayList<>();
List<Channel> existingChannels = getThing().getChannels();
if (!existingChannels.isEmpty()) {
// Clear existing channels
logger.debug("Clearing existing channels for keypad {}", integrationId);
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(channelList);
updateThing(thingBuilder.build());
}
ThingBuilder thingBuilder = editThing();
// add channels for buttons
for (KeypadComponent component : buttonList) {
channelTypeUID = new ChannelTypeUID(BINDING_ID, advancedChannels ? "buttonAdvanced" : "button");
channelUID = new ChannelUID(getThing().getUID(), component.channel());
channel = ChannelBuilder.create(channelUID, "Switch").withType(channelTypeUID).withLabel(component.description()).build();
channelList.add(channel);
}
// add channels for LEDs
for (KeypadComponent component : ledList) {
channelTypeUID = new ChannelTypeUID(BINDING_ID, advancedChannels ? "ledIndicatorAdvanced" : "ledIndicator");
channelUID = new ChannelUID(getThing().getUID(), component.channel());
channel = ChannelBuilder.create(channelUID, "Switch").withType(channelTypeUID).withLabel(component.description()).build();
channelList.add(channel);
}
// add channels for CCIs (for VCRX or eventually HomeWorks CCI)
for (KeypadComponent component : cciList) {
channelTypeUID = new ChannelTypeUID(BINDING_ID, "cciState");
channelUID = new ChannelUID(getThing().getUID(), component.channel());
channel = ChannelBuilder.create(channelUID, "Contact").withType(channelTypeUID).withLabel(component.description()).build();
channelList.add(channel);
}
thingBuilder.withChannels(channelList);
updateThing(thingBuilder.build());
logger.debug("Done configuring channels for keypad {}", integrationId);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class LuxtronikHeatpumpHandler method updateChannels.
private void updateChannels(HeatpumpConnector connector) {
Integer[] visibilityValues = connector.getVisibilities();
Integer[] heatpumpValues = connector.getValues();
Integer[] heatpumpParams = connector.getParams();
logger.debug("Updating available channels for thing {}", thing.getUID());
final ThingHandlerCallback callback = getCallback();
if (callback == null) {
logger.debug("ThingHandlerCallback is null. Skipping migration of last_update channel.");
return;
}
ThingBuilder thingBuilder = editThing();
List<Channel> channelList = new ArrayList<>();
// clear channel list
thingBuilder.withoutChannels(thing.getChannels());
// create list with available channels
for (HeatpumpChannel channel : HeatpumpChannel.values()) {
Integer channelId = channel.getChannelId();
int length = channel.isWritable() ? heatpumpParams.length : heatpumpValues.length;
ChannelUID channelUID = new ChannelUID(thing.getUID(), channel.getCommand());
ChannelTypeUID channelTypeUID;
if (channel.getCommand().matches("^channel[0-9]+$")) {
channelTypeUID = new ChannelTypeUID(LuxtronikHeatpumpBindingConstants.BINDING_ID, "unknown");
} else {
channelTypeUID = new ChannelTypeUID(LuxtronikHeatpumpBindingConstants.BINDING_ID, channel.getCommand());
}
if ((channelId != null && length <= channelId) || (config.showAllChannels == Boolean.FALSE && !channel.isVisible(visibilityValues))) {
logger.debug("Hiding channel {}", channel.getCommand());
} else {
channelList.add(callback.createChannelBuilder(channelUID, channelTypeUID).build());
}
}
thingBuilder.withChannels(channelList);
updateThing(thingBuilder.build());
}
Aggregations