Search in sources :

Example 1 with ChannelDefinition

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

the class DsDeviceThingTypeProvider method getThingType.

@Override
public ThingType getThingType(ThingTypeUID thingTypeUID, Locale locale) {
    try {
        SupportedThingTypes supportedThingType = SupportedThingTypes.valueOf(thingTypeUID.getId());
        ThingTypeBuilder thingTypeBuilder = ThingTypeBuilder.instance(thingTypeUID, getLabelText(thingTypeUID.getId(), locale)).withSupportedBridgeTypeUIDs(Arrays.asList(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE.getAsString())).withDescription(getDescText(thingTypeUID.getId(), locale));
        try {
            if (supportedThingType.havePowerSensors) {
                thingTypeBuilder.withConfigDescriptionURI(new URI(DEVICE_WITH_POWER_SENSORS));
            } else {
                thingTypeBuilder.withConfigDescriptionURI(new URI(DEVICE_WITHOUT_POWER_SENSORS));
            }
        } catch (URISyntaxException e) {
            logger.debug("An URISyntaxException occurred: ", e);
        }
        if (SupportedThingTypes.GR.equals(supportedThingType)) {
            thingTypeBuilder.withChannelDefinitions(Arrays.asList(new ChannelDefinitionBuilder(DsChannelTypeProvider.SHADE, new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, DsChannelTypeProvider.SHADE)).build()));
        }
        if (SupportedThingTypes.circuit.equals(supportedThingType)) {
            List<ChannelDefinition> channelDefinitions = new ArrayList<>(3);
            for (MeteringTypeEnum meteringType : MeteringTypeEnum.values()) {
                channelDefinitions.add(new ChannelDefinitionBuilder(DsChannelTypeProvider.getMeteringChannelID(meteringType, MeteringUnitsEnum.WH, false), new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, DsChannelTypeProvider.getMeteringChannelID(meteringType, MeteringUnitsEnum.WH, false))).build());
            }
            thingTypeBuilder.withChannelDefinitions(channelDefinitions);
        }
        return thingTypeBuilder.build();
    } catch (IllegalArgumentException e) {
    // ignore
    }
    return null;
}
Also used : ChannelDefinitionBuilder(org.openhab.core.thing.type.ChannelDefinitionBuilder) ThingTypeBuilder(org.openhab.core.thing.type.ThingTypeBuilder) MeteringTypeEnum(org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with ChannelDefinition

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

the class HomeAssistantThingHandler method updateThingType.

private void updateThingType() {
    // if this is a dynamic type, then we update the type
    ThingTypeUID typeID = thing.getThingTypeUID();
    if (!MqttBindingConstants.HOMEASSISTANT_MQTT_THING.equals(typeID)) {
        List<ChannelGroupDefinition> groupDefs;
        List<ChannelDefinition> channelDefs;
        synchronized (haComponents) {
            // sync whenever discoverComponents is started
            groupDefs = haComponents.values().stream().map(AbstractComponent::getGroupDefinition).collect(Collectors.toList());
            channelDefs = haComponents.values().stream().map(AbstractComponent::getType).map(ChannelGroupType::getChannelDefinitions).flatMap(List::stream).collect(Collectors.toList());
        }
        ThingType thingType = channelTypeProvider.derive(typeID, MqttBindingConstants.HOMEASSISTANT_MQTT_THING).withChannelDefinitions(channelDefs).withChannelGroupDefinitions(groupDefs).build();
        channelTypeProvider.setThingType(typeID, thingType);
    }
}
Also used : AbstractComponent(org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) List(java.util.List) ThingType(org.openhab.core.thing.type.ThingType) ChannelGroupDefinition(org.openhab.core.thing.type.ChannelGroupDefinition)

Example 3 with ChannelDefinition

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

the class ThingTypeResource method convertToChannelGroupDefinitionDTOs.

private List<ChannelGroupDefinitionDTO> convertToChannelGroupDefinitionDTOs(List<ChannelGroupDefinition> channelGroupDefinitions, Locale locale) {
    List<ChannelGroupDefinitionDTO> channelGroupDefinitionDTOs = new ArrayList<>();
    for (ChannelGroupDefinition channelGroupDefinition : channelGroupDefinitions) {
        String id = channelGroupDefinition.getId();
        ChannelGroupType channelGroupType = channelGroupTypeRegistry.getChannelGroupType(channelGroupDefinition.getTypeUID(), locale);
        // Default to the channelGroupDefinition label/description to override the channelGroupType
        String label = channelGroupDefinition.getLabel();
        String description = channelGroupDefinition.getDescription();
        List<ChannelDefinition> channelDefinitions = Collections.emptyList();
        if (channelGroupType == null) {
            logger.warn("Cannot find channel group type: {}", channelGroupDefinition.getTypeUID());
        } else {
            if (label == null) {
                label = channelGroupType.getLabel();
            }
            if (description == null) {
                description = channelGroupType.getDescription();
            }
            channelDefinitions = channelGroupType.getChannelDefinitions();
        }
        List<ChannelDefinitionDTO> channelDefinitionDTOs = convertToChannelDefinitionDTOs(channelDefinitions, locale);
        channelGroupDefinitionDTOs.add(new ChannelGroupDefinitionDTO(id, label, description, channelDefinitionDTOs));
    }
    return channelGroupDefinitionDTOs;
}
Also used : ChannelDefinitionDTO(org.openhab.core.thing.dto.ChannelDefinitionDTO) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ArrayList(java.util.ArrayList) ChannelGroupDefinitionDTO(org.openhab.core.thing.dto.ChannelGroupDefinitionDTO) ChannelGroupType(org.openhab.core.thing.type.ChannelGroupType) ChannelGroupDefinition(org.openhab.core.thing.type.ChannelGroupDefinition)

Example 4 with ChannelDefinition

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

the class ThingTypeResource method convertToChannelDefinitionDTOs.

private List<ChannelDefinitionDTO> convertToChannelDefinitionDTOs(List<ChannelDefinition> channelDefinitions, Locale locale) {
    List<ChannelDefinitionDTO> channelDefinitionDTOs = new ArrayList<>();
    for (ChannelDefinition channelDefinition : channelDefinitions) {
        ChannelType channelType = channelTypeRegistry.getChannelType(channelDefinition.getChannelTypeUID(), locale);
        if (channelType == null) {
            logger.warn("Cannot find channel type: {}", channelDefinition.getChannelTypeUID());
        } else {
            // Default to the channelDefinition label to override the
            // channelType
            String label = channelDefinition.getLabel();
            if (label == null) {
                label = channelType.getLabel();
            }
            // Default to the channelDefinition description to override the
            // channelType
            String description = channelDefinition.getDescription();
            if (description == null) {
                description = channelType.getDescription();
            }
            ChannelDefinitionDTO channelDefinitionDTO = new ChannelDefinitionDTO(channelDefinition.getId(), channelDefinition.getChannelTypeUID().toString(), label, description, channelType.getTags(), channelType.getCategory(), channelType.getState(), channelType.isAdvanced(), channelDefinition.getProperties());
            channelDefinitionDTOs.add(channelDefinitionDTO);
        }
    }
    return channelDefinitionDTOs;
}
Also used : ChannelDefinitionDTO(org.openhab.core.thing.dto.ChannelDefinitionDTO) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ArrayList(java.util.ArrayList) ChannelType(org.openhab.core.thing.type.ChannelType)

Example 5 with ChannelDefinition

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

the class ChannelCommandDescriptionProviderOSGiTest method beforeEach.

@BeforeEach
public void beforeEach() throws Exception {
    Mockito.when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
    thingHandlerFactory.activate(componentContextMock);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    final StateDescriptionFragment stateDescriptionFragment = StateDescriptionFragmentBuilder.create().withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.valueOf(100)).withStep(BigDecimal.TEN).withPattern("%d Peek").withReadOnly(true).withOption(new StateOption("SOUND", "My great sound.")).build();
    final CommandDescription command = CommandDescriptionBuilder.create().withCommandOption(new CommandOption("COMMAND", "My command.")).build();
    final ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:state-as-command"), " ", CoreItemFactory.NUMBER).withStateDescriptionFragment(stateDescriptionFragment).build();
    final ChannelType channelType2 = ChannelTypeBuilder.state(new ChannelTypeUID("hue:static"), " ", CoreItemFactory.STRING).withTag("Light").withCommandDescription(command).build();
    final ChannelType channelType3 = ChannelTypeBuilder.state(CHANNEL_TYPE_UID, " ", CoreItemFactory.STRING).withTag("Light").build();
    List<ChannelType> channelTypes = new ArrayList<>();
    channelTypes.add(channelType1);
    channelTypes.add(channelType2);
    channelTypes.add(channelType3);
    registerService(new ChannelTypeProvider() {

        @Override
        public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
            return channelTypes;
        }

        @Override
        @Nullable
        public ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
            for (final ChannelType channelType : channelTypes) {
                if (channelType.getUID().equals(channelTypeUID)) {
                    return channelType;
                }
            }
            return null;
        }
    });
    testBundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
    assertThat(testBundle, is(notNullValue()));
    thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
    assertThat(thingStatusInfoI18nLocalizationService, is(notNullValue()));
    thingStatusInfoI18nLocalizationService.setBundleResolver(new BundleResolverImpl());
    List<ChannelDefinition> channelDefinitions = new ArrayList<>();
    channelDefinitions.add(new ChannelDefinitionBuilder("1", channelType1.getUID()).build());
    channelDefinitions.add(new ChannelDefinitionBuilder("7_1", channelType2.getUID()).build());
    channelDefinitions.add(new ChannelDefinitionBuilder("7_2", channelType3.getUID()).build());
    registerService(new SimpleThingTypeProvider(Set.of(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
    List<Item> items = new ArrayList<>();
    items.add(new NumberItem("TestItem1"));
    items.add(new NumberItem("TestItem7_1"));
    items.add(new NumberItem("TestItem7_2"));
    registerService(new TestItemProvider(items));
    linkRegistry = getService(ItemChannelLinkRegistry.class);
}
Also used : Locale(java.util.Locale) ChannelDefinitionBuilder(org.openhab.core.thing.type.ChannelDefinitionBuilder) CommandOption(org.openhab.core.types.CommandOption) ArrayList(java.util.ArrayList) ItemRegistry(org.openhab.core.items.ItemRegistry) NumberItem(org.openhab.core.library.items.NumberItem) Item(org.openhab.core.items.Item) ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) ItemChannelLinkRegistry(org.openhab.core.thing.link.ItemChannelLinkRegistry) StateDescriptionFragment(org.openhab.core.types.StateDescriptionFragment) ThingStatusInfoI18nLocalizationService(org.openhab.core.thing.i18n.ThingStatusInfoI18nLocalizationService) CommandDescription(org.openhab.core.types.CommandDescription) ThingHandlerFactory(org.openhab.core.thing.binding.ThingHandlerFactory) BaseThingHandlerFactory(org.openhab.core.thing.binding.BaseThingHandlerFactory) StateOption(org.openhab.core.types.StateOption) NumberItem(org.openhab.core.library.items.NumberItem) ChannelDefinition(org.openhab.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.openhab.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.openhab.core.thing.ThingTypeUID) ChannelType(org.openhab.core.thing.type.ChannelType) Nullable(org.eclipse.jdt.annotation.Nullable) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ChannelDefinition (org.openhab.core.thing.type.ChannelDefinition)24 ChannelType (org.openhab.core.thing.type.ChannelType)13 ThingType (org.openhab.core.thing.type.ThingType)12 ChannelTypeUID (org.openhab.core.thing.type.ChannelTypeUID)10 ArrayList (java.util.ArrayList)9 Test (org.junit.jupiter.api.Test)9 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)9 ChannelDefinitionBuilder (org.openhab.core.thing.type.ChannelDefinitionBuilder)9 ChannelGroupType (org.openhab.core.thing.type.ChannelGroupType)8 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)7 ChannelGroupDefinition (org.openhab.core.thing.type.ChannelGroupDefinition)7 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Collection (java.util.Collection)4 ChannelGroupTypeUID (org.openhab.core.thing.type.ChannelGroupTypeUID)4 URI (java.net.URI)3 List (java.util.List)3 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)3 CoreMatchers (org.hamcrest.CoreMatchers)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Channel (org.openhab.core.thing.Channel)3