use of org.eclipse.smarthome.core.types.StateDescriptionProvider in project smarthome by eclipse.
the class StateDescriptionServiceImpl method getStateDescription.
@Override
@Nullable
public StateDescription getStateDescription(String itemName, Locale locale) {
StateDescription result = null;
List<StateOption> stateOptions = Collections.emptyList();
if (stateDescriptionProviders != null) {
for (StateDescriptionProvider stateDescriptionProvider : stateDescriptionProviders) {
StateDescription stateDescription = stateDescriptionProvider.getStateDescription(itemName, locale);
// as long as no valid StateDescription is provided we reassign here:
if (result == null) {
result = stateDescription;
}
// here
if (stateDescription != null && !stateDescription.getOptions().isEmpty() && stateOptions.isEmpty()) {
stateOptions = stateDescription.getOptions();
}
}
}
// are given:
if (result != null && !stateOptions.isEmpty()) {
result = new StateDescription(result.getMinimum(), result.getMaximum(), result.getStep(), result.getPattern(), result.isReadOnly(), stateOptions);
}
return result;
}
use of org.eclipse.smarthome.core.types.StateDescriptionProvider in project smarthome by eclipse.
the class ChannelStateDescriptionProviderOSGiTest method setup.
@Before
public void setup() {
initMocks(this);
Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
registerVolatileStorageService();
itemRegistry = getService(ItemRegistry.class);
assertNotNull(itemRegistry);
final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
thingHandlerFactory.activate(componentContext);
registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
final StateDescription state = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(100), BigDecimal.TEN, "%d Peek", true, Collections.singletonList(new StateOption("SOUND", "My great sound.")));
final StateDescription state2 = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(256), BigDecimal.valueOf(8), null, false, null);
final ChannelType channelType = new ChannelType(new ChannelTypeUID("hue:alarm"), false, "Number", " ", "", null, null, state, null);
final ChannelType channelType2 = new ChannelType(new ChannelTypeUID("hue:num"), false, "Number", " ", "", null, null, state2, null);
final ChannelType channelType3 = new ChannelType(new ChannelTypeUID("hue:info"), true, "String", " ", "", null, null, null, null);
final ChannelType channelType4 = new ChannelType(new ChannelTypeUID("hue:color"), false, "Color", "Color", "", "ColorLight", null, null, null);
final ChannelType channelType5 = new ChannelType(new ChannelTypeUID("hue:brightness"), false, "Dimmer", "Brightness", "", "DimmableLight", null, null, null);
final ChannelType channelType6 = new ChannelType(new ChannelTypeUID("hue:switch"), false, "Switch", "Switch", "", "Light", null, null, null);
final ChannelType channelType7 = new ChannelType(new ChannelTypeUID("hue:num-dynamic"), false, "Number", " ", "", "Light", null, state, null);
List<ChannelType> channelTypes = new ArrayList<>();
channelTypes.add(channelType);
channelTypes.add(channelType2);
channelTypes.add(channelType3);
channelTypes.add(channelType4);
channelTypes.add(channelType5);
channelTypes.add(channelType6);
channelTypes.add(channelType7);
registerService(new ChannelTypeProvider() {
@Override
public Collection<ChannelType> getChannelTypes(Locale locale) {
return channelTypes;
}
@Override
public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
for (final ChannelType channelType : channelTypes) {
if (channelType.getUID().equals(channelTypeUID)) {
return channelType;
}
}
return null;
}
@Override
public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
return null;
}
@Override
public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
return Collections.emptySet();
}
});
registerService(new DynamicStateDescriptionProvider() {
final StateDescription newState = new StateDescription(BigDecimal.valueOf(10), BigDecimal.valueOf(100), BigDecimal.valueOf(5), "VALUE %d", false, Arrays.asList(new StateOption("value0", "label0"), new StateOption("value1", "label1")));
@Override
@Nullable
public StateDescription getStateDescription(@NonNull Channel channel, @Nullable StateDescription original, @Nullable Locale locale) {
String id = channel.getUID().getIdWithoutGroup();
if ("7_1".equals(id)) {
assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
return newState;
} else if ("7_2".equals(id)) {
assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
StateDescription newState2 = new StateDescription(original.getMinimum().add(BigDecimal.ONE), original.getMaximum().add(BigDecimal.ONE), original.getStep().add(BigDecimal.TEN), "NEW " + original.getPattern(), true, original.getOptions());
return newState2;
}
return null;
}
});
List<ChannelDefinition> channelDefinitions = new ArrayList<>();
channelDefinitions.add(new ChannelDefinition("1", channelType.getUID()));
channelDefinitions.add(new ChannelDefinition("2", channelType2.getUID()));
channelDefinitions.add(new ChannelDefinition("3", channelType3.getUID()));
channelDefinitions.add(new ChannelDefinition("4", channelType4.getUID()));
channelDefinitions.add(new ChannelDefinition("5", channelType5.getUID()));
channelDefinitions.add(new ChannelDefinition("6", channelType6.getUID()));
channelDefinitions.add(new ChannelDefinition("7_1", channelType7.getUID()));
channelDefinitions.add(new ChannelDefinition("7_2", channelType7.getUID()));
registerService(new SimpleThingTypeProvider(Collections.singleton(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
List<Item> items = new ArrayList<>();
items.add(new NumberItem("TestItem"));
items.add(new NumberItem("TestItem2"));
items.add(new StringItem("TestItem3"));
items.add(new ColorItem("TestItem4"));
items.add(new DimmerItem("TestItem5"));
items.add(new SwitchItem("TestItem6"));
items.add(new NumberItem("TestItem7_1"));
items.add(new NumberItem("TestItem7_2"));
registerService(new TestItemProvider(items));
linkRegistry = getService(ItemChannelLinkRegistry.class);
stateDescriptionProvider = getService(StateDescriptionProvider.class);
assertNotNull(stateDescriptionProvider);
}
use of org.eclipse.smarthome.core.types.StateDescriptionProvider in project smarthome by eclipse.
the class StateDescriptionServiceImplTest method testServiceWithOneStateDescriptionProvider.
@Test
public void testServiceWithOneStateDescriptionProvider() {
StateDescriptionProvider stateDescriptionProviderDefault = mock(StateDescriptionProvider.class);
when(stateDescriptionProviderDefault.getRank()).thenReturn(STATE_DESCRIPTION_PROVIDER_DEFAULT_SERVICE_RANKING);
StateDescription stateDescription1 = new StateDescription(STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE, STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE, STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP, STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN, STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY, STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS);
when(stateDescriptionProviderDefault.getStateDescription(ITEM_NAME, null)).thenReturn(stateDescription1);
mergingService.addStateDescriptionProvider(stateDescriptionProviderDefault);
item = new NumberItem(ITEM_NAME);
item.setStateDescriptionService(mergingService);
StateDescription finalStateDescription = item.getStateDescription();
assertThat(finalStateDescription.getMinimum(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_MIN_VALUE));
assertThat(finalStateDescription.getMaximum(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_MAX_VALUE));
assertThat(finalStateDescription.getStep(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_STEP));
assertThat(finalStateDescription.getPattern(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_PATTERN));
assertThat(finalStateDescription.isReadOnly(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_IS_READONLY));
assertThat(finalStateDescription.getOptions(), is(STATE_DESCRIPTION_PROVIDER_DEFAULT_OPTIONS));
}
use of org.eclipse.smarthome.core.types.StateDescriptionProvider in project smarthome by eclipse.
the class StateDescriptionServiceImplTest method mergeStateDescriptions.
private StateDescription mergeStateDescriptions(StateDescription stateDescription1, StateDescription stateDescription2, int stateDescriptionProvider1ServiceRanking, int stateDescriptionProvider2ServiceRanking) {
StateDescriptionProvider stateDescriptionProvider1 = mock(StateDescriptionProvider.class);
StateDescriptionProvider stateDescriptionProvider2 = mock(StateDescriptionProvider.class);
when(stateDescriptionProvider1.getRank()).thenReturn(stateDescriptionProvider1ServiceRanking);
when(stateDescriptionProvider2.getRank()).thenReturn(stateDescriptionProvider2ServiceRanking);
when(stateDescriptionProvider1.getStateDescription(ITEM_NAME, null)).thenReturn(stateDescription1);
when(stateDescriptionProvider2.getStateDescription(ITEM_NAME, null)).thenReturn(stateDescription2);
mergingService.addStateDescriptionProvider(stateDescriptionProvider1);
mergingService.addStateDescriptionProvider(stateDescriptionProvider2);
item = new NumberItem(ITEM_NAME);
item.setStateDescriptionService(mergingService);
StateDescription finalStateDescription = item.getStateDescription();
return finalStateDescription;
}
Aggregations