Search in sources :

Example 46 with ThingBuilder

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

the class Tr064RootHandler method internalInitialize.

/**
 * internal thing initializer (sets SCPDUtil and connects to remote device)
 */
private void internalInitialize() {
    try {
        scpdUtil = new SCPDUtil(httpClient, endpointBaseURL);
    } catch (SCPDException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "could not get device definitions from " + config.host);
        return;
    }
    if (establishSecureConnectionAndUpdateProperties()) {
        removeConnectScheduler();
        // connection successful, check channels
        ThingBuilder thingBuilder = editThing();
        thingBuilder.withoutChannels(thing.getChannels());
        final SCPDUtil scpdUtil = this.scpdUtil;
        if (scpdUtil != null) {
            Util.checkAvailableChannels(thing, thingBuilder, scpdUtil, "", deviceType, channels);
            updateThing(thingBuilder.build());
        }
        communicationEstablished = true;
        installPolling();
        updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) SCPDUtil(org.openhab.binding.tr064.internal.util.SCPDUtil)

Example 47 with ThingBuilder

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

the class WolfSmartsetUnitThingHandler method updateConfiguration.

/**
 * Update the configuration of this unit and create / update the related channels
 *
 * @param submenu the {@link SubMenuEntryDTO} for this unit
 * @param tabmenu the {@link MenuItemTabViewDTO} for this unit
 */
public void updateConfiguration(SubMenuEntryDTO submenu, MenuItemTabViewDTO tabmenu) {
    this.submenu = submenu;
    this.tabmenu = tabmenu;
    var bridgeHandler = getBridge();
    if (bridgeHandler != null) {
        bridgeStatusChanged(bridgeHandler.getStatusInfo());
    }
    lastRefreshTime = null;
    ThingBuilder thingBuilder = editThing();
    var thingId = thing.getUID();
    paramDescriptionMap.clear();
    for (var param : tabmenu.parameterDescriptors) {
        paramDescriptionMap.put(param.valueId, param);
        // "bindingId:type:thingId:1")
        var channelId = new ChannelUID(thingId, param.parameterId.toString());
        if (thing.getChannel(channelId) == null) {
            logger.debug("UnitThing: Create channel '{}'", channelId);
            Channel channel = ChannelBuilder.create(channelId, getItemType(param.controlType)).withLabel(param.name).withType(getChannelType(param)).build();
            thingBuilder.withChannel(channel);
        }
    }
    updateThing(thingBuilder.build());
    for (var param : tabmenu.parameterDescriptors) {
        var channelId = new ChannelUID(thingId, param.parameterId.toString());
        setState(channelId, WolfSmartsetUtils.undefOrString(param.value));
    }
}
Also used : ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel)

Example 48 with ThingBuilder

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

the class LightThingHandler method processStateResponse.

@Override
protected void processStateResponse(DeconzBaseMessage stateResponse) {
    if (!(stateResponse instanceof LightMessage)) {
        return;
    }
    LightMessage lightMessage = (LightMessage) stateResponse;
    if (needsPropertyUpdate) {
        // if we did not receive an ctmin/ctmax, then we probably don't need it
        needsPropertyUpdate = false;
        Integer ctmax = lightMessage.ctmax;
        Integer ctmin = lightMessage.ctmin;
        if (ctmin != null && ctmax != null) {
            Map<String, String> properties = new HashMap<>(thing.getProperties());
            properties.put(PROPERTY_CT_MAX, Integer.toString(Util.constrainToRange(ctmax, ZCL_CT_MIN, ZCL_CT_MAX)));
            properties.put(PROPERTY_CT_MIN, Integer.toString(Util.constrainToRange(ctmin, ZCL_CT_MIN, ZCL_CT_MAX)));
            updateProperties(properties);
        }
    }
    ThingBuilder thingBuilder = editThing();
    boolean thingEdited = false;
    LightState lightState = lightMessage.state;
    if (lightState != null && lightState.effect != null && checkAndUpdateEffectChannels(thingBuilder, lightMessage)) {
        thingEdited = true;
    }
    if (checkLastSeen(thingBuilder, stateResponse.lastseen)) {
        thingEdited = true;
    }
    if (thingEdited) {
        updateThing(thingBuilder.build());
    }
    messageReceived(lightMessage);
}
Also used : LightState(org.smarthomej.binding.deconz.internal.dto.LightState) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) HashMap(java.util.HashMap) LightMessage(org.smarthomej.binding.deconz.internal.dto.LightMessage)

Example 49 with ThingBuilder

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

the class SensorBaseThingHandler method processStateResponse.

@Override
protected void processStateResponse(DeconzBaseMessage stateResponse) {
    if (!(stateResponse instanceof SensorMessage)) {
        return;
    }
    SensorMessage sensorMessage = (SensorMessage) stateResponse;
    sensorConfig = Objects.requireNonNullElse(sensorMessage.config, new SensorConfig());
    sensorState = Objects.requireNonNullElse(sensorMessage.state, new SensorState());
    // Add some information about the sensor
    if (!sensorConfig.reachable) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Not reachable");
        return;
    }
    Map<String, String> editProperties = editProperties();
    editProperties.put(UNIQUE_ID, sensorMessage.uniqueid);
    editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, sensorMessage.swversion);
    editProperties.put(Thing.PROPERTY_VENDOR, sensorMessage.manufacturername);
    editProperties.put(Thing.PROPERTY_MODEL_ID, sensorMessage.modelid);
    ignoreConfigurationUpdate = true;
    updateProperties(editProperties);
    // Some sensors support optional channels
    // (see https://github.com/dresden-elektronik/deconz-rest-plugin/wiki/Supported-Devices#sensors)
    // any battery-powered sensor
    ThingBuilder thingBuilder = editThing();
    boolean thingEdited = false;
    if (sensorConfig.battery != null) {
        if (createChannel(thingBuilder, CHANNEL_BATTERY_LEVEL, ChannelKind.STATE)) {
            thingEdited = true;
        }
        if (createChannel(thingBuilder, CHANNEL_BATTERY_LOW, ChannelKind.STATE)) {
            thingEdited = true;
        }
    }
    if (sensorState.lowbattery != null && sensorConfig.battery == null) {
        // if sensorConfig.battery != null the channel is already added
        if (createChannel(thingBuilder, CHANNEL_BATTERY_LOW, ChannelKind.STATE)) {
            thingEdited = true;
        }
    }
    if (createTypeSpecificChannels(thingBuilder, sensorConfig, sensorState)) {
        thingEdited = true;
    }
    if (checkLastSeen(thingBuilder, sensorMessage.lastseen)) {
        thingEdited = true;
    }
    // if the thing was edited, we update it now
    if (thingEdited) {
        logger.debug("Thing configuration changed, updating thing.");
        updateThing(thingBuilder.build());
    }
    ignoreConfigurationUpdate = false;
    // Initial data
    updateChannels(sensorConfig);
    updateChannels(sensorState, true);
    updateStatus(ThingStatus.ONLINE);
}
Also used : SensorConfig(org.smarthomej.binding.deconz.internal.dto.SensorConfig) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) SensorMessage(org.smarthomej.binding.deconz.internal.dto.SensorMessage) SensorState(org.smarthomej.binding.deconz.internal.dto.SensorState)

Example 50 with ThingBuilder

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

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.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);
}
Also used : OwException(org.smarthomej.binding.onewire.internal.OwException) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.openhab.core.library.types.OnOffType) OwDynamicStateDescriptionProvider(org.smarthomej.binding.onewire.internal.OwDynamicStateDescriptionProvider) OwBindingConstants(org.smarthomej.binding.onewire.internal.OwBindingConstants) BAE091xHandlerConfiguration(org.smarthomej.binding.onewire.internal.config.BAE091xHandlerConfiguration) HashSet(java.util.HashSet) Thing(org.openhab.core.thing.Thing) Map(java.util.Map) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) ChannelUID(org.openhab.core.thing.ChannelUID) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) OwChannelConfig(org.smarthomej.binding.onewire.internal.device.OwChannelConfig) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) ThingUID(org.openhab.core.thing.ThingUID) Set(java.util.Set) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) Collectors(java.util.stream.Collectors) OwSensorType(org.smarthomej.binding.onewire.internal.device.OwSensorType) BAE0910(org.smarthomej.binding.onewire.internal.device.BAE0910) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) OwChannelConfig(org.smarthomej.binding.onewire.internal.device.OwChannelConfig) ThingUID(org.openhab.core.thing.ThingUID) BAE091xHandlerConfiguration(org.smarthomej.binding.onewire.internal.config.BAE091xHandlerConfiguration) HashSet(java.util.HashSet) OwException(org.smarthomej.binding.onewire.internal.OwException)

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