Search in sources :

Example 1 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class AirQualityStationHandler method discoverAttributes.

private void discoverAttributes() {
    getAirQualityData().ifPresent(data -> {
        // Update thing properties
        Map<String, String> properties = new HashMap<>();
        properties.put(ATTRIBUTIONS, data.getAttributions());
        PointType serverLocation = locationProvider.getLocation();
        if (serverLocation != null) {
            PointType stationLocation = new PointType(data.getCity().getGeo());
            double distance = serverLocation.distanceFrom(stationLocation).doubleValue();
            properties.put(DISTANCE, new QuantityType<>(distance / 1000, KILO(SIUnits.METRE)).toString());
        }
        // Search and remove missing pollutant channels
        List<Channel> channels = new ArrayList<>(getThing().getChannels());
        Stream.of(Pollutant.values()).forEach(pollutant -> {
            String groupName = pollutant.name().toLowerCase();
            double value = data.getIaqiValue(pollutant);
            channels.removeIf(channel -> value == -1 && groupName.equals(channel.getUID().getGroupId()));
        });
        // Update thing configuration
        Configuration config = editConfiguration();
        config.put(AirQualityConfiguration.STATION_ID, data.getStationId());
        ThingBuilder thingBuilder = editThing();
        thingBuilder.withChannels(channels).withConfiguration(config).withProperties(properties).withLocation(data.getCity().getName());
        updateThing(thingBuilder.build());
    });
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Configuration(org.openhab.core.config.core.Configuration) SensitiveGroupConfiguration(org.openhab.binding.airquality.internal.config.SensitiveGroupConfiguration) AirQualityConfiguration(org.openhab.binding.airquality.internal.config.AirQualityConfiguration) HashMap(java.util.HashMap) QuantityType(org.openhab.core.library.types.QuantityType) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) PointType(org.openhab.core.library.types.PointType)

Example 2 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class IntesisBoxHandler method addChannel.

public void addChannel(String channelId, String itemType) {
    if (thing.getChannel(channelId) == null) {
        logger.trace("Channel '{}' for UID to be added", channelId);
        ThingBuilder thingBuilder = editThing();
        final ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channelId);
        Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), channelId), itemType).withType(channelTypeUID).withKind(ChannelKind.STATE).build();
        thingBuilder.withChannel(channel);
        updateThing(thingBuilder.build());
    }
    if (limits.containsKey(channelId)) {
        List<StateOption> options = new ArrayList<>();
        for (String mode : limits.get(channelId)) {
            options.add(new StateOption(mode, mode.substring(0, 1).toUpperCase() + mode.substring(1).toLowerCase()));
        }
        intesisStateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), channelId), options);
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) ArrayList(java.util.ArrayList) StateOption(org.openhab.core.types.StateOption)

Example 3 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class JablotronJa100FHandler method createPGChannel.

private void createPGChannel(String name, String label) {
    ChannelTypeUID pgmStatus = new ChannelTypeUID(BINDING_ID, "pgm_state");
    ThingBuilder thingBuilder = editThing();
    Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), name), "Switch").withLabel(label).withType(pgmStatus).build();
    thingBuilder.withChannel(channel);
    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) Channel(org.openhab.core.thing.Channel)

Example 4 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class JablotronJa100FHandler method createStateChannel.

private void createStateChannel(String name, String label) {
    ChannelTypeUID alarmStatus = new ChannelTypeUID(BINDING_ID, "ja100f_alarm_state");
    ThingBuilder thingBuilder = editThing();
    Channel channel = ChannelBuilder.create(new ChannelUID(thing.getUID(), name), "String").withLabel(label).withType(alarmStatus).build();
    thingBuilder.withChannel(channel);
    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) Channel(org.openhab.core.thing.Channel)

Example 5 with ThingBuilder

use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.

the class EcobeeSensorThingHandler method updateCapabilityChannels.

private void updateCapabilityChannels(RemoteSensorCapabilityDTO capability) {
    ChannelUID uid = new ChannelUID(thing.getUID(), capability.type);
    Channel channel = thing.getChannel(uid);
    if (channel == null) {
        logger.debug("SensorThing: Create channel '{}'", uid);
        ThingBuilder thingBuilder;
        thingBuilder = editThing();
        channel = ChannelBuilder.create(uid, getAcceptedItemType(capability.type)).withLabel("Sensor " + WordUtils.capitalize(capability.type)).withType(getChannelTypeUID(capability.type)).build();
        thingBuilder.withChannel(channel);
        updateThing(thingBuilder.build());
    }
    logger.trace("Capability '{}' has type '{}' with value '{}'", capability.id, capability.type, capability.value);
    updateCapabilityState(capability.type, capability.value);
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel)

Aggregations

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