Search in sources :

Example 56 with ThingBuilder

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());
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) HeatpumpChannel(org.openhab.binding.luxtronikheatpump.internal.enums.HeatpumpChannel) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) HeatpumpChannel(org.openhab.binding.luxtronikheatpump.internal.enums.HeatpumpChannel)

Example 57 with ThingBuilder

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);
}
Also used : Arrays(java.util.Arrays) AbstractOwDevice(org.openhab.binding.onewire.internal.device.AbstractOwDevice) LoggerFactory(org.slf4j.LoggerFactory) DS18x20(org.openhab.binding.onewire.internal.device.DS18x20) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Thing(org.openhab.core.thing.Thing) DS2406_DS2413(org.openhab.binding.onewire.internal.device.DS2406_DS2413) Configuration(org.openhab.core.config.core.Configuration) DS2438Configuration(org.openhab.binding.onewire.internal.DS2438Configuration) Map(java.util.Map) OwException(org.openhab.binding.onewire.internal.OwException) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) AMSHandlerConfiguration(org.openhab.binding.onewire.internal.config.AMSHandlerConfiguration) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) SensorId(org.openhab.binding.onewire.internal.SensorId) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Logger(org.slf4j.Logger) OwBindingConstants(org.openhab.binding.onewire.internal.OwBindingConstants) Set(java.util.Set) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) DS2438(org.openhab.binding.onewire.internal.device.DS2438) Collectors(java.util.stream.Collectors) LightSensorType(org.openhab.binding.onewire.internal.device.DS2438.LightSensorType) Stream(java.util.stream.Stream) OwDynamicStateDescriptionProvider(org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider) OwSensorType(org.openhab.binding.onewire.internal.device.OwSensorType) Collections(java.util.Collections) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) Configuration(org.openhab.core.config.core.Configuration) DS2438Configuration(org.openhab.binding.onewire.internal.DS2438Configuration) AMSHandlerConfiguration(org.openhab.binding.onewire.internal.config.AMSHandlerConfiguration) DS2438(org.openhab.binding.onewire.internal.device.DS2438) HashMap(java.util.HashMap) AbstractOwDevice(org.openhab.binding.onewire.internal.device.AbstractOwDevice) OwException(org.openhab.binding.onewire.internal.OwException)

Example 58 with ThingBuilder

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);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) BaseHandlerConfiguration(org.openhab.binding.onewire.internal.config.BaseHandlerConfiguration) AbstractOwDevice(org.openhab.binding.onewire.internal.device.AbstractOwDevice) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) ArrayList(java.util.ArrayList) Thing(org.openhab.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) Map(java.util.Map) OwException(org.openhab.binding.onewire.internal.OwException) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) OwChannelConfig(org.openhab.binding.onewire.internal.device.OwChannelConfig) SensorId(org.openhab.binding.onewire.internal.SensorId) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) State(org.openhab.core.types.State) UnDefType(org.openhab.core.types.UnDefType) OwBindingConstants(org.openhab.binding.onewire.internal.OwBindingConstants) RefreshType(org.openhab.core.types.RefreshType) Set(java.util.Set) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Channel(org.openhab.core.thing.Channel) List(java.util.List) Stream(java.util.stream.Stream) OwDynamicStateDescriptionProvider(org.openhab.binding.onewire.internal.OwDynamicStateDescriptionProvider) OwSensorType(org.openhab.binding.onewire.internal.device.OwSensorType) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) OwException(org.openhab.binding.onewire.internal.OwException)

Example 59 with ThingBuilder

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());
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) Channel(org.openhab.core.thing.Channel)

Example 60 with ThingBuilder

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));
}
Also used : Unit(javax.measure.Unit) StringType(org.openhab.core.library.types.StringType) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HexUtils(org.openhab.core.util.HexUtils) BigDecimal(java.math.BigDecimal) Thing(org.openhab.core.thing.Thing) Quantity(javax.measure.Quantity) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) Duration(java.time.Duration) Map(java.util.Map) TypeParser(org.openhab.core.types.TypeParser) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) DefaultLocation(org.eclipse.jdt.annotation.DefaultLocation) Conformity(org.openhab.binding.smartmeter.internal.conformity.Conformity) QuantityType(org.openhab.core.library.types.QuantityType) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) SerialPortManager(org.openhab.core.io.transport.serial.SerialPortManager) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) Baudrate(org.openhab.binding.smartmeter.internal.helper.Baudrate) State(org.openhab.core.types.State) RefreshType(org.openhab.core.types.RefreshType) BaseThingHandler(org.openhab.core.thing.binding.BaseThingHandler) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) SmartMeterConfiguration(org.openhab.binding.smartmeter.SmartMeterConfiguration) SmartMeterBindingConstants(org.openhab.binding.smartmeter.SmartMeterBindingConstants) ChannelType(org.openhab.core.thing.type.ChannelType) Objects(java.util.Objects) Channel(org.openhab.core.thing.Channel) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) NonNull(org.eclipse.jdt.annotation.NonNull) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Configuration(org.openhab.core.config.core.Configuration) SmartMeterConfiguration(org.openhab.binding.smartmeter.SmartMeterConfiguration) Channel(org.openhab.core.thing.Channel) SmartMeterConfiguration(org.openhab.binding.smartmeter.SmartMeterConfiguration) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) State(org.openhab.core.types.State) ChannelUID(org.openhab.core.thing.ChannelUID) NonNull(org.eclipse.jdt.annotation.NonNull) ArrayList(java.util.ArrayList) List(java.util.List) ChannelBuilder(org.openhab.core.thing.binding.builder.ChannelBuilder) ChannelType(org.openhab.core.thing.type.ChannelType) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ThingBuilder (org.openhab.core.thing.binding.builder.ThingBuilder)107 Channel (org.openhab.core.thing.Channel)71 ChannelUID (org.openhab.core.thing.ChannelUID)60 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)38 ArrayList (java.util.ArrayList)37 Thing (org.openhab.core.thing.Thing)29 HashMap (java.util.HashMap)23 ChannelBuilder (org.openhab.core.thing.binding.builder.ChannelBuilder)20 Map (java.util.Map)19 ThingStatus (org.openhab.core.thing.ThingStatus)18 ThingStatusDetail (org.openhab.core.thing.ThingStatusDetail)18 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 Bridge (org.openhab.core.thing.Bridge)17 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)16 List (java.util.List)15 Nullable (org.eclipse.jdt.annotation.Nullable)15 Configuration (org.openhab.core.config.core.Configuration)15 Collectors (java.util.stream.Collectors)14 Command (org.openhab.core.types.Command)12