use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class TACmiSchemaHandler method refreshData.
private void refreshData() {
URI schemaApiPage = this.schemaApiPage;
if (schemaApiPage == null) {
return;
}
try {
final ApiPageParser pp = parsePage(schemaApiPage, new ApiPageParser(this, entries, this.channelTypeProvider));
final List<Channel> channels = pp.getChannels();
if (pp.isConfigChanged() || channels.size() != this.getThing().getChannels().size()) {
// we have to update our channels...
final ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
}
if (!this.online) {
updateStatus(ThingStatus.ONLINE);
this.online = true;
}
} catch (final InterruptedException e) {
// binding shutdown is in progress
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE);
this.online = false;
} catch (final ParseException | RuntimeException e) {
logger.debug("Error parsing API Scheme: {} ", e.getMessage(), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "Error: " + e.getMessage());
this.online = false;
} catch (final TimeoutException | ExecutionException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error: " + e.getMessage());
this.online = false;
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class BAE091xSensorThingHandler method configureThingChannels.
@Override
protected void configureThingChannels() {
ThingUID thingUID = getThing().getUID();
logger.debug("configuring sensors for {}", thingUID);
BAE091xHandlerConfiguration configuration = getConfig().as(BAE091xHandlerConfiguration.class);
Set<OwChannelConfig> wantedChannel = new HashSet<>();
wantedChannel.addAll(SENSOR_TYPE_CHANNEL_MAP.getOrDefault(sensorType, Set.of()));
// Pin1:
switch(configuration.pin1) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_COUNTER:
wantedChannel.add(new OwChannelConfig(CHANNEL_COUNTER, CHANNEL_TYPE_UID_BAE_COUNTER));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 1");
return;
}
// Pin2:
switch(configuration.pin2) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_OUT:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL2, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 2"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY3, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 3"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 1/3"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 2");
return;
}
// Pin6:
switch(configuration.pin6) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_PIO:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL6, CHANNEL_TYPE_UID_BAE_PIO, "PIO Pin 6"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY4, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 4"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 2/4"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 6");
return;
}
// Pin7:
switch(configuration.pin7) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_ANALOG:
wantedChannel.add(new OwChannelConfig(CHANNEL_VOLTAGE, CHANNEL_TYPE_UID_BAE_ANALOG, "Analog Input"));
break;
case CONFIG_BAE_PIN_OUT:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL7, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 7"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY2, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 2"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 2/4"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 7");
return;
}
// Pin8:
switch(configuration.pin8) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_IN:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DIN, "Digital In Pin 8"));
break;
case CONFIG_BAE_PIN_OUT:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DOUT, "Digital Out Pin 8"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY1, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 1"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 1/3"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 8");
return;
}
ThingBuilder thingBuilder = editThing();
// remove unwanted channels
Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId()).collect(Collectors.toSet());
Set<String> wantedChannelIds = wantedChannel.stream().map(channelConfig -> channelConfig.channelId).collect(Collectors.toSet());
existingChannelIds.stream().filter(channelId -> !wantedChannelIds.contains(channelId)).forEach(channelId -> removeChannelIfExisting(thingBuilder, channelId));
// add or update wanted channels
wantedChannel.stream().forEach(channelConfig -> {
addChannelIfMissingAndEnable(thingBuilder, channelConfig);
});
updateThing(thingBuilder.build());
try {
sensors.get(0).configureChannels();
} catch (OwException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
return;
}
validConfig = true;
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class PlugwiseHAApplianceHandler method addBatteryChannels.
protected synchronized void addBatteryChannels() {
logger.debug("Battery operated appliance: {} detected: adding 'Battery level' and 'Battery low level' channels", thing.getLabel());
ChannelUID channelUIDBatteryLevel = new ChannelUID(getThing().getUID(), APPLIANCE_BATTERYLEVEL_CHANNEL);
ChannelUID channelUIDBatteryLevelLow = new ChannelUID(getThing().getUID(), APPLIANCE_BATTERYLEVELLOW_CHANNEL);
boolean channelBatteryLevelExists = false;
boolean channelBatteryLowExists = false;
List<Channel> channels = getThing().getChannels();
for (Channel channel : channels) {
if (channel.getUID().equals(channelUIDBatteryLevel)) {
channelBatteryLevelExists = true;
} else if (channel.getUID().equals(channelUIDBatteryLevelLow)) {
channelBatteryLowExists = true;
}
if (channelBatteryLevelExists && channelBatteryLowExists) {
break;
}
}
if (!channelBatteryLevelExists) {
ThingBuilder thingBuilder = editThing();
Channel channelBatteryLevel = ChannelBuilder.create(channelUIDBatteryLevel, "Number").withType(CHANNEL_TYPE_BATTERYLEVEL).withKind(ChannelKind.STATE).withLabel("Battery Level").withDescription("Represents the battery level as a percentage (0-100%)").build();
thingBuilder.withChannel(channelBatteryLevel);
updateThing(thingBuilder.build());
}
if (!channelBatteryLowExists) {
ThingBuilder thingBuilder = editThing();
Channel channelBatteryLow = ChannelBuilder.create(channelUIDBatteryLevelLow, "Switch").withType(CHANNEL_TYPE_BATTERYLEVELLOW).withKind(ChannelKind.STATE).withLabel("Battery Low Level").withDescription("Switches ON when battery level gets below threshold level").build();
thingBuilder.withChannel(channelBatteryLow);
updateThing(thingBuilder.build());
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class TouchWandBSensorHandler method removeUnsupportedChannels.
void removeUnsupportedChannels(TouchWandBSensorUnitData unitData) {
ArrayList<Channel> toBeRemovedChannels = new ArrayList<>(thing.getChannels());
String sensorSubType = unitData.getIdData().getSubType();
switch(sensorSubType) {
case BSENSOR_SUBTYPE_DOORWINDOW:
toBeRemovedChannels.remove(thing.getChannel(CHANNEL_DOORWINDOW));
break;
case BSENSOR_SUBTYPE_MOTION:
toBeRemovedChannels.remove(thing.getChannel(CHANNEL_MOTION));
break;
case BSENSOR_SUBTYPE_SMOKE:
Channel channel = thing.getChannel(CHANNEL_SMOKE);
toBeRemovedChannels.remove(channel);
break;
}
ThingBuilder thingBuilder = editThing();
thingBuilder.withoutChannels(toBeRemovedChannels);
updateThing(thingBuilder.build());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class PLCDateTimeHandler method doInitialization.
@Override
protected void doInitialization() {
Thing thing = getThing();
logger.debug("Initialize LOGO! date/time handler.");
config.set(getConfigAs(PLCDateTimeConfiguration.class));
super.doInitialization();
if (ThingStatus.OFFLINE != thing.getStatus()) {
String block = config.get().getBlockType();
String text = "Time".equalsIgnoreCase(block) ? "Time" : "Date";
ThingBuilder tBuilder = editThing();
String label = thing.getLabel();
if (label == null) {
Bridge bridge = getBridge();
label = (bridge == null) || (bridge.getLabel() == null) ? "Siemens Logo!" : bridge.getLabel();
label += (": " + text.toLowerCase() + " in/output");
}
tBuilder.withLabel(label);
String name = config.get().getBlockName();
String type = config.get().getChannelType();
ChannelUID uid = new ChannelUID(thing.getUID(), "Time".equalsIgnoreCase(block) ? "time" : "date");
ChannelBuilder cBuilder = ChannelBuilder.create(uid, type);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " block parameter " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
cBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), VALUE_CHANNEL), ANALOG_ITEM);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, ANALOG_ITEM.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " block parameter " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);
updateThing(tBuilder.build());
updateStatus(ThingStatus.ONLINE);
}
}
Aggregations