Search in sources :

Example 31 with ChannelType

use of org.openhab.core.thing.type.ChannelType in project openhab-addons by openhab.

the class HomematicThingHandler method updateDynamicChannelList.

/**
 * Update the given thing channel list to reflect the device's current datapoint set
 *
 * @return true if the list was modified, false if it was not modified
 */
private boolean updateDynamicChannelList(HmDevice device, List<Channel> thingChannels) {
    boolean changed = false;
    for (HmChannel channel : device.getChannels()) {
        if (!channel.isReconfigurable()) {
            continue;
        }
        final String expectedFunction = channel.getDatapoint(HmParamsetType.MASTER, HomematicConstants.DATAPOINT_NAME_CHANNEL_FUNCTION).getOptionValue();
        final String propertyName = String.format(PROPERTY_DYNAMIC_FUNCTION_FORMAT, channel.getNumber());
        // remove thing channels that were configured for a different function
        Iterator<Channel> channelIter = thingChannels.iterator();
        while (channelIter.hasNext()) {
            Map<String, String> properties = channelIter.next().getProperties();
            String function = properties.get(propertyName);
            if (function != null && !function.equals(expectedFunction)) {
                channelIter.remove();
                changed = true;
            }
        }
        for (HmDatapoint dp : channel.getDatapoints()) {
            if (HomematicTypeGeneratorImpl.isIgnoredDatapoint(dp) || dp.getParamsetType() != HmParamsetType.VALUES) {
                continue;
            }
            ChannelUID channelUID = UidUtils.generateChannelUID(dp, getThing().getUID());
            if (containsChannel(thingChannels, channelUID)) {
                // Channel is already present -> channel configuration likely hasn't changed
                continue;
            }
            Map<String, String> channelProps = new HashMap<>();
            channelProps.put(propertyName, expectedFunction);
            ChannelTypeUID channelTypeUID = UidUtils.generateChannelTypeUID(dp);
            ChannelType channelType = channelTypeProvider.getInternalChannelType(channelTypeUID);
            if (channelType == null) {
                channelType = HomematicTypeGeneratorImpl.createChannelType(dp, channelTypeUID);
                channelTypeProvider.addChannelType(channelType);
            }
            Channel thingChannel = ChannelBuilder.create(channelUID, MetadataUtils.getItemType(dp)).withProperties(channelProps).withLabel(MetadataUtils.getLabel(dp)).withDescription(MetadataUtils.getDatapointDescription(dp)).withType(channelType.getUID()).build();
            thingChannels.add(thingChannel);
            changed = true;
        }
    }
    return changed;
}
Also used : ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) HashMap(java.util.HashMap) ChannelUID(org.openhab.core.thing.ChannelUID) Channel(org.openhab.core.thing.Channel) HmChannel(org.openhab.binding.homematic.internal.model.HmChannel) HmChannel(org.openhab.binding.homematic.internal.model.HmChannel) ChannelType(org.openhab.core.thing.type.ChannelType) HmDatapoint(org.openhab.binding.homematic.internal.model.HmDatapoint)

Example 32 with ChannelType

use of org.openhab.core.thing.type.ChannelType in project openhab-addons by openhab.

the class HarmonyHubHandlerFactory method removeChannelTypesForThing.

public void removeChannelTypesForThing(ThingUID uid) {
    List<ChannelType> removes = new ArrayList<>();
    for (ChannelType c : channelTypes) {
        if (c.getUID().getAsString().startsWith(uid.getAsString())) {
            removes.add(c);
        }
    }
    channelTypes.removeAll(removes);
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ChannelType(org.openhab.core.thing.type.ChannelType)

Example 33 with ChannelType

use of org.openhab.core.thing.type.ChannelType in project openhab-addons by openhab.

the class KM200ChannelTypeProvider method removeChannelTypesForThing.

public void removeChannelTypesForThing(ThingUID uid) {
    List<ChannelType> removes = new ArrayList<>();
    for (ChannelType c : channelTypes) {
        if (c.getUID().getAsString().startsWith(uid.getAsString())) {
            removes.add(c);
        }
    }
    channelTypes.removeAll(removes);
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ChannelType(org.openhab.core.thing.type.ChannelType)

Example 34 with ChannelType

use of org.openhab.core.thing.type.ChannelType in project openhab-addons by openhab.

the class DsChannelTypeProvider method getChannelTypes.

@Override
public Collection<ChannelType> getChannelTypes(Locale locale) {
    List<ChannelType> channelTypeList = new LinkedList<>();
    for (String channelTypeId : SUPPORTED_OUTPUT_CHANNEL_TYPES) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, channelTypeId), locale));
    }
    for (SensorEnum sensorType : SensorEnum.values()) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(sensorType)), locale));
    }
    for (MeteringTypeEnum meteringType : MeteringTypeEnum.values()) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(meteringType, MeteringUnitsEnum.WH)), locale));
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(TOTAL_PRE, meteringType, MeteringUnitsEnum.WH)), locale));
    }
    for (DeviceBinarayInputEnum binaryInput : DeviceBinarayInputEnum.values()) {
        channelTypeList.add(getChannelType(new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, buildIdentifier(BINARY_INPUT_PRE, binaryInput)), locale));
    }
    return channelTypeList;
}
Also used : MeteringTypeEnum(org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) SensorEnum(org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.SensorEnum) DeviceBinarayInputEnum(org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.DeviceBinarayInputEnum) ChannelType(org.openhab.core.thing.type.ChannelType) LinkedList(java.util.LinkedList)

Example 35 with ChannelType

use of org.openhab.core.thing.type.ChannelType in project openhab-addons by openhab.

the class HomematicTypeGeneratorImpl method createChannelType.

/**
 * Creates the ChannelType for the given datapoint.
 */
public static ChannelType createChannelType(HmDatapoint dp, ChannelTypeUID channelTypeUID) {
    ChannelType channelType;
    if (dp.getName().equals(DATAPOINT_NAME_LOWBAT) || dp.getName().equals(DATAPOINT_NAME_LOWBAT_IP)) {
        channelType = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_LOW_BATTERY;
    } else if (dp.getName().equals(VIRTUAL_DATAPOINT_NAME_SIGNAL_STRENGTH)) {
        channelType = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_SIGNAL_STRENGTH;
    } else if (dp.getName().equals(VIRTUAL_DATAPOINT_NAME_BUTTON)) {
        channelType = DefaultSystemChannelTypeProvider.SYSTEM_BUTTON;
    } else {
        String itemType = MetadataUtils.getItemType(dp);
        String category = MetadataUtils.getCategory(dp, itemType);
        String label = MetadataUtils.getLabel(dp);
        String description = MetadataUtils.getDatapointDescription(dp);
        List<StateOption> options = null;
        if (dp.isEnumType()) {
            options = MetadataUtils.generateOptions(dp, new OptionsBuilder<StateOption>() {

                @Override
                public StateOption createOption(String value, String description) {
                    return new StateOption(value, description);
                }
            });
        }
        StateDescriptionFragmentBuilder stateFragment = StateDescriptionFragmentBuilder.create();
        if (dp.isNumberType()) {
            BigDecimal min = MetadataUtils.createBigDecimal(dp.getMinValue());
            BigDecimal max = MetadataUtils.createBigDecimal(dp.getMaxValue());
            if (ITEM_TYPE_DIMMER.equals(itemType) && (max.compareTo(new BigDecimal("1.0")) == 0 || max.compareTo(new BigDecimal("1.01")) == 0)) {
                // For dimmers with a max value of 1.01 or 1.0 the values must be corrected
                min = MetadataUtils.createBigDecimal(0);
                max = MetadataUtils.createBigDecimal(100);
            }
            stateFragment.withMinimum(min).withMaximum(max).withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
        } else {
            stateFragment.withPattern(MetadataUtils.getStatePattern(dp)).withReadOnly(dp.isReadOnly());
        }
        if (options != null) {
            stateFragment.withOptions(options);
        }
        ChannelTypeBuilder channelTypeBuilder;
        EventDescription eventDescription = null;
        if (dp.isTrigger()) {
            eventDescription = new EventDescription(MetadataUtils.generateOptions(dp, new OptionsBuilder<EventOption>() {

                @Override
                public EventOption createOption(String value, String description) {
                    return new EventOption(value, description);
                }
            }));
            channelTypeBuilder = ChannelTypeBuilder.trigger(channelTypeUID, label).withEventDescription(eventDescription);
        } else {
            channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, label, itemType).withStateDescriptionFragment(stateFragment.build());
        }
        channelType = channelTypeBuilder.isAdvanced(!MetadataUtils.isStandard(dp)).withDescription(description).withCategory(category).withConfigDescriptionURI(configDescriptionUriChannel).build();
    }
    return channelType;
}
Also used : StateDescriptionFragmentBuilder(org.openhab.core.types.StateDescriptionFragmentBuilder) ChannelType(org.openhab.core.thing.type.ChannelType) EventDescription(org.openhab.core.types.EventDescription) StateOption(org.openhab.core.types.StateOption) OptionsBuilder(org.openhab.binding.homematic.internal.type.MetadataUtils.OptionsBuilder) BigDecimal(java.math.BigDecimal) EventOption(org.openhab.core.types.EventOption) ChannelTypeBuilder(org.openhab.core.thing.type.ChannelTypeBuilder)

Aggregations

ChannelType (org.openhab.core.thing.type.ChannelType)57 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)27 Channel (org.openhab.core.thing.Channel)21 ArrayList (java.util.ArrayList)20 Nullable (org.eclipse.jdt.annotation.Nullable)17 ChannelUID (org.openhab.core.thing.ChannelUID)16 ChannelDefinition (org.openhab.core.thing.type.ChannelDefinition)14 Test (org.junit.jupiter.api.Test)11 URI (java.net.URI)10 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)10 Thing (org.openhab.core.thing.Thing)9 ThingType (org.openhab.core.thing.type.ThingType)9 StateOption (org.openhab.core.types.StateOption)9 BigDecimal (java.math.BigDecimal)8 Collection (java.util.Collection)8 ChannelDefinitionBuilder (org.openhab.core.thing.type.ChannelDefinitionBuilder)8 ChannelTypeProvider (org.openhab.core.thing.type.ChannelTypeProvider)8 Locale (java.util.Locale)7 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)7 Configuration (org.openhab.core.config.core.Configuration)7