use of org.openhab.core.thing.type.ChannelType 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.ChannelType in project openhab-core by openhab.
the class GenericThingProviderTest3 method assertThatThingsAreUpdatedOnceTheXMLfilesHaveBeenProcessed.
@Test
public void assertThatThingsAreUpdatedOnceTheXMLfilesHaveBeenProcessed() {
assertThat(thingRegistry.getAll().size(), is(1));
Thing thing1 = thingRegistry.getAll().iterator().next();
assertThat(thing1.getUID().toString(), is("dumb:DUMB:boo"));
assertThat(thing1.getChannels().size(), is(1));
assertThat(thing1.getChannel("manual"), is(notNullValue()));
assertThat(thing1.getLabel(), is("Test Label"));
assertThat(thing1.getLocation(), is("Test Location"));
assertThat(thing1.getConfiguration().getProperties().get("testConf"), is("foo"));
// now become smart again...
dumbThingHandlerFactory.setDumb(false);
ChannelType channelType1 = ChannelTypeBuilder.state(new ChannelTypeUID(DumbThingHandlerFactory.BINDING_ID, "channel1"), "Channel 1", CoreItemFactory.STRING).build();
ChannelTypeProvider channelTypeProvider = mock(ChannelTypeProvider.class);
when(channelTypeProvider.getChannelType(any(), nullable(Locale.class))).thenAnswer(invocation -> {
if ("channel1".equals(channelType1.getUID().getId())) {
return channelType1;
}
return null;
});
registerService(channelTypeProvider);
// ensure thing type was considered and manual and predefined values are there.
waitForAssert(() -> {
Thing thing2 = thingRegistry.getAll().iterator().next();
assertThat(thing2.getLabel(), is("Test Label"));
assertThat(thing2.getLocation(), is("Test Location"));
assertThat(thing2.getChannels().size(), is(2));
assertThat(thing2.getChannel("manual"), is(notNullValue()));
assertThat(thing2.getChannel("channel1"), is(notNullValue()));
// there is a default, so make sure the manually configured one (from the DSL) wins
assertThat(thing2.getConfiguration().getProperties().get("testConf"), is("foo"));
// it's not manually configured, but the thing type defines a default, so ensure it's in
assertThat(thing2.getConfiguration().getProperties().get("testAdditional"), is("hello world"));
});
}
use of org.openhab.core.thing.type.ChannelType in project openhab-core by openhab.
the class CommunicationManagerOSGiTest method beforeEach.
@BeforeEach
public void beforeEach() {
safeCaller = getService(SafeCaller.class);
assertNotNull(safeCaller);
SystemProfileFactory profileFactory = getService(ProfileTypeProvider.class, SystemProfileFactory.class);
assertNotNull(profileFactory);
if (profileFactory == null) {
throw new IllegalStateException("thing is null");
}
manager = new CommunicationManager(autoUpdateManagerMock, channelTypeRegistryMock, profileFactory, iclRegistry, itemRegistryMock, itemStateConverterMock, eventPublisherMock, safeCaller, thingRegistryMock);
doAnswer(invocation -> {
switch(((Channel) invocation.getArguments()[0]).getKind()) {
case STATE:
return new ProfileTypeUID("test:state");
case TRIGGER:
return new ProfileTypeUID("test:trigger");
}
return null;
}).when(profileAdvisorMock).getSuggestedProfileTypeUID(isA(Channel.class), isA(String.class));
doAnswer(invocation -> {
switch(((ProfileTypeUID) invocation.getArguments()[0]).toString()) {
case "test:state":
return stateProfileMock;
case "test:trigger":
return triggerProfileMock;
}
return null;
}).when(profileFactoryMock).createProfile(isA(ProfileTypeUID.class), isA(ProfileCallback.class), isA(ProfileContext.class));
when(profileFactoryMock.getSupportedProfileTypeUIDs()).thenReturn(Stream.of(new ProfileTypeUID("test:state"), new ProfileTypeUID("test:trigger")).collect(Collectors.toList()));
manager.addProfileFactory(profileFactoryMock);
manager.addProfileAdvisor(profileAdvisorMock);
iclRegistry.addProvider(new ItemChannelLinkProvider() {
@Override
public void addProviderChangeListener(ProviderChangeListener<ItemChannelLink> listener) {
}
@Override
public void removeProviderChangeListener(ProviderChangeListener<ItemChannelLink> listener) {
}
@Override
public Collection<ItemChannelLink> getAll() {
return List.of(LINK_1_S1, LINK_1_S2, LINK_2_S2, LINK_1_T1, LINK_1_T2, LINK_2_T2, LINK_3_S3, LINK_4_S4);
}
});
when(itemRegistryMock.get(eq(ITEM_NAME_1))).thenReturn(ITEM_1);
when(itemRegistryMock.get(eq(ITEM_NAME_2))).thenReturn(ITEM_2);
when(itemRegistryMock.get(eq(ITEM_NAME_3))).thenReturn(ITEM_3);
when(itemRegistryMock.get(eq(ITEM_NAME_4))).thenReturn(ITEM_4);
ChannelType channelType4 = mock(ChannelType.class);
when(channelType4.getItemType()).thenReturn("Number:Temperature");
when(channelTypeRegistryMock.getChannelType(CHANNEL_TYPE_UID_4)).thenReturn(channelType4);
THING.setHandler(thingHandlerMock);
when(thingRegistryMock.get(eq(THING_UID))).thenReturn(THING);
manager.addItemFactory(new CoreItemFactory());
UnitProvider unitProvider = mock(UnitProvider.class);
when(unitProvider.getUnit(Temperature.class)).thenReturn(SIUnits.CELSIUS);
ITEM_3.setUnitProvider(unitProvider);
ITEM_4.setUnitProvider(unitProvider);
}
use of org.openhab.core.thing.type.ChannelType 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);
}
use of org.openhab.core.thing.type.ChannelType in project openhab-core by openhab.
the class ChannelStateDescriptionProvider method getStateDescription.
@Nullable
private StateDescription getStateDescription(String itemName, @Nullable Locale locale) {
Set<ChannelUID> boundChannels = itemChannelLinkRegistry.getBoundChannels(itemName);
if (!boundChannels.isEmpty()) {
ChannelUID channelUID = boundChannels.iterator().next();
Channel channel = thingRegistry.getChannel(channelUID);
if (channel != null) {
StateDescription stateDescription = null;
ChannelType channelType = thingTypeRegistry.getChannelType(channel, locale);
if (channelType != null) {
stateDescription = channelType.getState();
String itemType = channelType.getItemType();
if (itemType != null && (stateDescription == null || stateDescription.getPattern() == null)) {
String pattern = null;
if (CoreItemFactory.STRING.equalsIgnoreCase(itemType)) {
pattern = "%s";
} else if (itemType.startsWith(CoreItemFactory.NUMBER)) {
pattern = "%.0f";
}
if (pattern != null) {
logger.trace("Provide a default pattern {} for item {}", pattern, itemName);
StateDescriptionFragmentBuilder builder = (stateDescription == null) ? StateDescriptionFragmentBuilder.create() : StateDescriptionFragmentBuilder.create(stateDescription);
stateDescription = builder.withPattern(pattern).build().toStateDescription();
}
}
}
StateDescription dynamicStateDescription = getDynamicStateDescription(channel, stateDescription, locale);
if (dynamicStateDescription != null) {
return dynamicStateDescription;
}
return stateDescription;
}
}
return null;
}
Aggregations