use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.
the class ChannelLinkNotifierOSGiTest method addItemsAndLinks.
private void addItemsAndLinks(Thing thing, String itemSuffix) {
forEachThingChannelUID(thing, channelUID -> {
String itemName = getItemName(thing, channelUID, itemSuffix);
managedItemProvider.add(new NumberItem(itemName));
managedItemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
});
}
use of org.openhab.core.thing.link.ItemChannelLink 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.link.ItemChannelLink in project openhab-core by openhab.
the class ChannelCommandDescriptionProviderOSGiTest method wrongItemCommandDescription.
@Test
public void wrongItemCommandDescription() throws ItemNotFoundException {
ThingRegistry thingRegistry = getService(ThingRegistry.class);
assertNotNull(thingRegistry);
ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
assertNotNull(managedThingProvider);
registerService(new MalfunctioningDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
registerService(new TestDynamicCommandDescriptionProvider(), DynamicCommandDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
linkRegistry.add(link);
//
final Collection<Item> items = itemRegistry.getItems();
assertFalse(items.isEmpty());
Item item = itemRegistry.getItem("TestItem7_2");
CommandDescription command = item.getCommandDescription();
assertNotNull(command);
List<CommandOption> opts = command.getCommandOptions();
assertNotNull(opts);
assertEquals(1, opts.size());
final CommandOption opt2 = opts.get(0);
assertEquals("NEW COMMAND", opt2.getCommand());
assertEquals("My new command.", opt2.getLabel());
}
use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.
the class ChannelStateDescriptionProviderOSGiTest method presentItemStateDescription.
/**
* Assert that item's state description is present.
*/
@Test
public void presentItemStateDescription() throws ItemNotFoundException {
ThingRegistry thingRegistry = getService(ThingRegistry.class);
assertNotNull(thingRegistry);
ManagedThingProvider managedThingProvider = getService(ManagedThingProvider.class);
assertNotNull(managedThingProvider);
registerService(new TestDynamicStateDescriptionProvider(), DynamicStateDescriptionProvider.class.getName());
Thing thing = thingRegistry.createThingOfType(new ThingTypeUID("hue:lamp"), new ThingUID("hue:lamp:lamp1"), null, "test thing", new Configuration());
assertNotNull(thing);
if (thing == null) {
throw new IllegalStateException("thing is null");
}
managedThingProvider.add(thing);
ItemChannelLink link = new ItemChannelLink("TestItem", getChannel(thing, "1").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem2", getChannel(thing, "2").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem3", getChannel(thing, "3").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem4", getChannel(thing, "4").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem5", getChannel(thing, "5").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem6", getChannel(thing, "6").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem7_1", getChannel(thing, "7_1").getUID());
linkRegistry.add(link);
link = new ItemChannelLink("TestItem7_2", getChannel(thing, "7_2").getUID());
linkRegistry.add(link);
//
final Collection<Item> items = itemRegistry.getItems();
assertFalse(items.isEmpty());
Item item = itemRegistry.getItem("TestItem");
assertEquals(CoreItemFactory.NUMBER, item.getType());
StateDescription state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.ZERO, state.getMinimum());
assertEquals(BigDecimal.valueOf(100), state.getMaximum());
assertEquals(BigDecimal.TEN, state.getStep());
assertEquals("%d Peek", state.getPattern());
assertTrue(state.isReadOnly());
List<StateOption> opts = state.getOptions();
assertEquals(1, opts.size());
final StateOption opt = opts.get(0);
assertEquals("SOUND", opt.getValue());
assertEquals("My great sound.", opt.getLabel());
item = itemRegistry.getItem("TestItem2");
assertEquals(CoreItemFactory.NUMBER, item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.ZERO, state.getMinimum());
assertEquals(BigDecimal.valueOf(256), state.getMaximum());
assertEquals(BigDecimal.valueOf(8), state.getStep());
assertEquals("%.0f", state.getPattern());
assertFalse(state.isReadOnly());
opts = state.getOptions();
assertEquals(0, opts.size());
item = itemRegistry.getItem("TestItem3");
assertEquals(CoreItemFactory.STRING, item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertNull(state.getMinimum());
assertNull(state.getMaximum());
assertNull(state.getStep());
assertEquals("%s", state.getPattern());
assertFalse(state.isReadOnly());
opts = state.getOptions();
assertEquals(0, opts.size());
item = itemRegistry.getItem("TestItem4");
assertEquals(CoreItemFactory.COLOR, item.getType());
state = item.getStateDescription();
assertNull(state);
item = itemRegistry.getItem("TestItem5");
assertEquals(CoreItemFactory.DIMMER, item.getType());
state = item.getStateDescription();
assertNull(state);
item = itemRegistry.getItem("TestItem6");
assertEquals(CoreItemFactory.SWITCH, item.getType());
state = item.getStateDescription();
assertNull(state);
item = itemRegistry.getItem("TestItem7_1");
assertEquals(CoreItemFactory.NUMBER, item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.valueOf(10), state.getMinimum());
assertEquals(BigDecimal.valueOf(100), state.getMaximum());
assertEquals(BigDecimal.valueOf(5), state.getStep());
assertEquals("VALUE %d", state.getPattern());
assertFalse(state.isReadOnly());
opts = state.getOptions();
assertNotNull(opts);
assertEquals(2, opts.size());
final StateOption opt0 = opts.get(0);
assertNotNull(opt0);
assertEquals(opt0.getValue(), "value0");
assertEquals(opt0.getLabel(), "label0");
final StateOption opt1 = opts.get(1);
assertNotNull(opt1);
assertEquals(opt1.getValue(), "value1");
assertEquals(opt1.getLabel(), "label1");
item = itemRegistry.getItem("TestItem7_2");
assertEquals(CoreItemFactory.NUMBER, item.getType());
state = item.getStateDescription();
assertNotNull(state);
assertEquals(BigDecimal.valueOf(1), state.getMinimum());
assertEquals(BigDecimal.valueOf(101), state.getMaximum());
assertEquals(BigDecimal.valueOf(20), state.getStep());
assertEquals("NEW %d Peek", state.getPattern());
assertFalse(state.isReadOnly());
opts = state.getOptions();
assertNotNull(opts);
assertEquals(1, opts.size());
final StateOption opt2 = opts.get(0);
assertEquals("NEW SOUND", opt2.getValue());
assertEquals("My great new sound.", opt2.getLabel());
}
use of org.openhab.core.thing.link.ItemChannelLink in project openhab-core by openhab.
the class GenericItemChannelLinkProvider method createItemChannelLink.
private void createItemChannelLink(String context, String itemName, String channelUID, Configuration configuration) throws BindingConfigParseException {
ChannelUID channelUIDObject = null;
try {
channelUIDObject = new ChannelUID(channelUID);
} catch (IllegalArgumentException e) {
throw new BindingConfigParseException(e.getMessage());
}
ItemChannelLink itemChannelLink = new ItemChannelLink(itemName, channelUIDObject, configuration);
Set<String> itemNames = contextMap.get(context);
if (itemNames == null) {
itemNames = new HashSet<>();
contextMap.put(context, itemNames);
}
itemNames.add(itemName);
if (previousItemNames != null) {
previousItemNames.remove(itemName);
}
Set<ItemChannelLink> links = itemChannelLinkMap.get(itemName);
if (links == null) {
itemChannelLinkMap.put(itemName, links = new HashSet<>());
}
if (!links.contains(itemChannelLink)) {
links.add(itemChannelLink);
notifyListenersAboutAddedElement(itemChannelLink);
} else {
notifyListenersAboutUpdatedElement(itemChannelLink, itemChannelLink);
}
}
Aggregations