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());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class AdvancedMultisensorThingHandler method configureThingChannels.
@Override
protected void configureThingChannels() {
Configuration configuration = getConfig();
ThingBuilder thingBuilder = editThing();
// delete unwanted channels
Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId()).collect(Collectors.toSet());
Set<String> wantedChannelIds = SENSOR_TYPE_CHANNEL_MAP.get(sensorType).stream().map(channelConfig -> channelConfig.channelId).collect(Collectors.toSet());
wantedChannelIds.add(CHANNEL_TEMPERATURE);
wantedChannelIds.add(CHANNEL_HUMIDITY);
existingChannelIds.stream().filter(channelId -> !wantedChannelIds.contains(channelId)).forEach(channelId -> removeChannelIfExisting(thingBuilder, channelId));
// add or update wanted channels
SENSOR_TYPE_CHANNEL_MAP.get(sensorType).stream().forEach(channelConfig -> {
addChannelIfMissingAndEnable(thingBuilder, channelConfig);
});
// temperature channel
if (configuration.containsKey(CONFIG_TEMPERATURESENSOR) && configuration.get(CONFIG_TEMPERATURESENSOR).equals("DS18B20")) {
addChannelIfMissingAndEnable(thingBuilder, new OwChannelConfig(CHANNEL_TEMPERATURE, CHANNEL_TYPE_UID_TEMPERATURE_POR_RES), 1);
} else {
addChannelIfMissingAndEnable(thingBuilder, new OwChannelConfig(CHANNEL_TEMPERATURE, CHANNEL_TYPE_UID_TEMPERATURE));
}
// humidity channel
addChannelIfMissingAndEnable(thingBuilder, new OwChannelConfig(CHANNEL_HUMIDITY, CHANNEL_TYPE_UID_HUMIDITY), new Configuration(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put(CONFIG_HUMIDITY, "/HIH4000/humidity");
}
}));
// configure light channel
if (sensorType == OwSensorType.AMS_S || sensorType == OwSensorType.BMS_S) {
if (hwRevision <= 13) {
((DS2438) sensors.get(0)).setLightSensorType(LightSensorType.ELABNET_V1);
} else {
((DS2438) sensors.get(0)).setLightSensorType(LightSensorType.ELABNET_V2);
}
}
updateThing(thingBuilder.build());
try {
for (AbstractOwDevice sensor : sensors) {
sensor.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 OwBaseThingHandler method configureThingChannels.
protected void configureThingChannels() {
ThingBuilder thingBuilder = editThing();
logger.debug("configuring sensors for {}", thing.getUID());
// remove unwanted channels
Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId()).collect(Collectors.toSet());
Set<String> wantedChannelIds = SENSOR_TYPE_CHANNEL_MAP.get(sensorType).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
SENSOR_TYPE_CHANNEL_MAP.get(sensorType).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;
}
if (thing.getChannel(CHANNEL_PRESENT) != null) {
showPresence = true;
}
validConfig = true;
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class PublicTransportSwitzerlandStationboardHandler method createDynamicChannels.
private void createDynamicChannels(int numberOfChannels) {
List<Channel> existingChannels = getThing().getChannelsOfGroup(dynamicChannelGroupUID.getId());
ThingBuilder thingBuilder = editThing();
for (int i = existingChannels.size(); i < numberOfChannels; i++) {
Channel channel = ChannelBuilder.create(getChannelUIDForPosition(i), "String").withLabel("Departure " + (i + 1)).withType(new ChannelTypeUID("publictransportswitzerland", "departure")).build();
thingBuilder.withChannel(channel);
}
updateThing(thingBuilder.build());
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class SmartMeterHandler method updateOBISValue.
/**
* Get new data the device
*/
private void updateOBISValue() {
cancelRead();
valueChangeListener = new MeterValueListener() {
@Override
public <Q extends @NonNull Quantity<Q>> void valueChanged(MeterValue<Q> value) {
ThingBuilder thingBuilder = editThing();
String obis = value.getObisCode();
String obisChannelString = SmartMeterBindingConstants.getObisChannelId(obis);
Channel channel = thing.getChannel(obisChannelString);
ChannelTypeUID channelTypeId = channelTypeProvider.getChannelTypeIdForObis(obis);
ChannelType channelType = channelTypeProvider.getChannelType(channelTypeId, null);
if (channelType != null) {
String itemType = channelType.getItemType();
State state = getStateForObisValue(value, channel);
if (channel == null) {
logger.debug("Adding channel: {} with item type: {}", obisChannelString, itemType);
// channel has not been created yet
ChannelBuilder channelBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), obisChannelString), itemType).withType(channelTypeId);
Configuration configuration = new Configuration();
configuration.put(SmartMeterBindingConstants.CONFIGURATION_CONVERSION, 1);
channelBuilder.withConfiguration(configuration);
channelBuilder.withLabel(obis);
Map<String, String> channelProps = new HashMap<>();
channelProps.put(SmartMeterBindingConstants.CHANNEL_PROPERTY_OBIS, obis);
channelBuilder.withProperties(channelProps);
channelBuilder.withDescription(MessageFormat.format("Value for OBIS code: {0} with Unit: {1}", obis, value.getUnit()));
channel = channelBuilder.build();
ChannelUID channelId = channel.getUID();
// add all valid channels to the thing builder
List<Channel> channels = new ArrayList<>(getThing().getChannels());
if (channels.stream().filter((element) -> element.getUID().equals(channelId)).count() == 0) {
channels.add(channel);
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
}
}
if (!channel.getProperties().containsKey(SmartMeterBindingConstants.CHANNEL_PROPERTY_OBIS)) {
addObisPropertyToChannel(obis, channel);
}
if (state != null) {
updateState(channel.getUID(), state);
}
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
} else {
logger.warn("No ChannelType found for OBIS {}", obis);
}
}
private void addObisPropertyToChannel(String obis, Channel channel) {
String description = channel.getDescription();
String label = channel.getLabel();
ChannelBuilder newChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType()).withDefaultTags(channel.getDefaultTags()).withConfiguration(channel.getConfiguration()).withDescription(description == null ? "" : description).withKind(channel.getKind()).withLabel(label == null ? "" : label).withType(channel.getChannelTypeUID());
Map<String, String> properties = new HashMap<>(channel.getProperties());
properties.put(SmartMeterBindingConstants.CHANNEL_PROPERTY_OBIS, obis);
newChannel.withProperties(properties);
updateThing(editThing().withoutChannel(channel.getUID()).withChannel(newChannel.build()).build());
}
@Override
public <Q extends @NonNull Quantity<Q>> void valueRemoved(MeterValue<Q> value) {
// channels that are not available are removed
String obisChannelId = SmartMeterBindingConstants.getObisChannelId(value.getObisCode());
logger.debug("Removing channel: {}", obisChannelId);
ThingBuilder thingBuilder = editThing();
thingBuilder.withoutChannel(new ChannelUID(thing.getUID(), obisChannelId));
updateThing(thingBuilder.build());
}
@Override
public void errorOccurred(Throwable e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage());
}
};
this.smlDevice.addValueChangeListener(valueChangeListener);
SmartMeterConfiguration config = getConfigAs(SmartMeterConfiguration.class);
int delay = config.refresh != null ? config.refresh : DEFAULT_REFRESH_PERIOD;
valueReader = this.smlDevice.readValues(DEFAULT_TIMEOUT, this.scheduler, Duration.ofSeconds(delay));
}
Aggregations