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;
}
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);
}
}
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;
}
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;
}
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);
}
Aggregations