Search in sources :

Example 1 with StateDescription

use of org.eclipse.smarthome.core.types.StateDescription in project smarthome by eclipse.

the class ItemStateConverterImplTest method numberItemWitDimensionShouldConvertToItemStateDescriptionUnit.

@Test
public void numberItemWitDimensionShouldConvertToItemStateDescriptionUnit() {
    NumberItem item = mock(NumberItem.class);
    StateDescription stateDescription = mock(StateDescription.class);
    when(item.getStateDescription()).thenReturn(stateDescription);
    doReturn(Temperature.class).when(item).getDimension();
    when(stateDescription.getPattern()).thenReturn("%.1f K");
    State originalState = new QuantityType<>("12.34 °C");
    State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState, is(new QuantityType<>("285.49 K")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Test(org.junit.Test)

Example 2 with StateDescription

use of org.eclipse.smarthome.core.types.StateDescription in project smarthome by eclipse.

the class EnrichedItemDTOMapper method map.

private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, URI uri, boolean drillDown, Predicate<Item> itemFilter, Locale locale) {
    String state = item.getState().toFullString();
    String transformedState = considerTransformation(state, item.getStateDescription(locale));
    if (transformedState != null && transformedState.equals(state)) {
        transformedState = null;
    }
    StateDescription stateDescription = considerTransformation(item.getStateDescription(locale));
    String link = null != uri ? uri.toASCIIString() + ItemResource.PATH_ITEMS + "/" + itemDTO.name : null;
    EnrichedItemDTO enrichedItemDTO = null;
    if (item instanceof GroupItem) {
        GroupItem groupItem = (GroupItem) item;
        EnrichedItemDTO[] memberDTOs;
        if (drillDown) {
            Collection<EnrichedItemDTO> members = new LinkedHashSet<>();
            for (Item member : groupItem.getMembers()) {
                if (itemFilter == null || itemFilter.test(member)) {
                    members.add(map(member, drillDown, itemFilter, uri, locale));
                }
            }
            memberDTOs = members.toArray(new EnrichedItemDTO[members.size()]);
        } else {
            memberDTOs = new EnrichedItemDTO[0];
        }
        enrichedItemDTO = new EnrichedGroupItemDTO(itemDTO, memberDTOs, link, state, transformedState, stateDescription);
    } else {
        enrichedItemDTO = new EnrichedItemDTO(itemDTO, link, state, transformedState, stateDescription);
    }
    return enrichedItemDTO;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 3 with StateDescription

use of org.eclipse.smarthome.core.types.StateDescription in project smarthome by eclipse.

the class XmlChannelTypeProvider method localize.

@Override
protected ChannelType localize(Bundle bundle, ChannelType channelType, Locale locale) {
    if (thingTypeI18nUtil == null) {
        return null;
    }
    ChannelTypeUID channelTypeUID = channelType.getUID();
    String label = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, channelType.getLabel(), locale);
    String description = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID, channelType.getDescription(), locale);
    StateDescription state = createLocalizedChannelState(bundle, channelType, channelTypeUID, locale);
    return new ChannelType(channelTypeUID, channelType.isAdvanced(), channelType.getItemType(), channelType.getKind(), label, description, channelType.getCategory(), channelType.getTags(), state, channelType.getEvent(), channelType.getConfigDescriptionURI());
}
Also used : ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 4 with StateDescription

use of org.eclipse.smarthome.core.types.StateDescription in project smarthome by eclipse.

the class XmlChannelTypeProvider method createLocalizedChannelState.

private StateDescription createLocalizedChannelState(Bundle bundle, ChannelType channelType, ChannelTypeUID channelTypeUID, Locale locale) {
    StateDescription state = channelType.getState();
    if (state != null) {
        String pattern = this.thingTypeI18nUtil.getChannelStatePattern(bundle, channelTypeUID, state.getPattern(), locale);
        List<StateOption> localizedOptions = new ArrayList<>();
        List<StateOption> options = state.getOptions();
        for (StateOption stateOption : options) {
            String optionLabel = this.thingTypeI18nUtil.getChannelStateOption(bundle, channelTypeUID, stateOption.getValue(), stateOption.getLabel(), locale);
            localizedOptions.add(new StateOption(stateOption.getValue(), optionLabel));
        }
        return new StateDescription(state.getMinimum(), state.getMaximum(), state.getStep(), pattern, state.isReadOnly(), localizedOptions);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) StateOption(org.eclipse.smarthome.core.types.StateOption) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 5 with StateDescription

use of org.eclipse.smarthome.core.types.StateDescription in project smarthome by eclipse.

the class DefaultSystemChannelTypeProvider method createLocalizedChannelType.

private ChannelType createLocalizedChannelType(Bundle bundle, ChannelType channelType, Locale locale) {
    LocalizedChannelTypeKey localizedChannelTypeKey = getLocalizedChannelTypeKey(channelType.getUID(), locale);
    ChannelType cachedEntry = localizedChannelTypeCache.get(localizedChannelTypeKey);
    if (cachedEntry != null) {
        return cachedEntry;
    }
    if (thingTypeI18nUtil != null) {
        ChannelTypeUID channelTypeUID = channelType.getUID();
        String label = thingTypeI18nUtil.getChannelLabel(bundle, channelTypeUID, channelType.getLabel(), locale);
        String description = thingTypeI18nUtil.getChannelDescription(bundle, channelTypeUID, channelType.getDescription(), locale);
        StateDescription state = createLocalizedChannelState(bundle, channelType, channelTypeUID, locale);
        ChannelType localizedChannelType = new ChannelType(channelTypeUID, channelType.isAdvanced(), channelType.getItemType(), channelType.getKind(), label, description, channelType.getCategory(), channelType.getTags(), state, channelType.getEvent(), channelType.getConfigDescriptionURI());
        localizedChannelTypeCache.put(localizedChannelTypeKey, localizedChannelType);
        return localizedChannelType;
    }
    return channelType;
}
Also used : ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Aggregations

StateDescription (org.eclipse.smarthome.core.types.StateDescription)31 Test (org.junit.Test)15 StateOption (org.eclipse.smarthome.core.types.StateOption)13 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)7 ArrayList (java.util.ArrayList)6 StringType (org.eclipse.smarthome.core.library.types.StringType)6 ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)6 BigDecimal (java.math.BigDecimal)4 Item (org.eclipse.smarthome.core.items.Item)4 ChannelTypeUID (org.eclipse.smarthome.core.thing.type.ChannelTypeUID)4 StateDescriptionProvider (org.eclipse.smarthome.core.types.StateDescriptionProvider)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 GroupItem (org.eclipse.smarthome.core.items.GroupItem)3 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)3 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)3 StringItem (org.eclipse.smarthome.core.library.items.StringItem)3 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Before (org.junit.Before)3 Collection (java.util.Collection)2