Search in sources :

Example 16 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class HSBK method getHSB.

public HSBType getHSB() {
    DecimalType hue = hueToDecimalType(this.hue);
    PercentType saturation = saturationToPercentType(this.saturation);
    PercentType brightness = brightnessToPercentType(this.brightness);
    return new HSBType(hue, saturation, brightness);
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Example 17 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class LightStateConverter method toHSBType.

/**
 * Transforms Hue Light {@link State} into {@link HSBType} representing the
 * color.
 *
 * @param lightState light state
 * @return HSB type representing the color
 */
public static HSBType toHSBType(State lightState) {
    int hue = lightState.getHue();
    int saturationInPercent = (int) (lightState.getSaturation() / SATURATION_FACTOR);
    int brightnessInPercent = (int) (lightState.getBrightness() / BRIGHTNESS_FACTOR);
    saturationInPercent = restrictToBounds(saturationInPercent);
    brightnessInPercent = restrictToBounds(brightnessInPercent);
    return new HSBType(new DecimalType(hue / HUE_FACTOR), new PercentType(saturationInPercent), new PercentType(brightnessInPercent));
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Example 18 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project habot by ghys.

the class SetValueSkill method interpretSetColor.

private void interpretSetColor(Intent intent, String language, IntentInterpretation interpretation, Set<Item> matchedItems) {
    String colorString = intent.getEntities().get("color");
    // filter out the items which can't receive an HSB command
    List<Item> filteredItems = matchedItems.stream().filter(i -> i.getAcceptedCommandTypes().contains(HSBType.class)).collect(Collectors.toList());
    String hsbValue;
    try {
        ResourceBundle colors = ResourceBundle.getBundle("colors", new Locale(language));
        hsbValue = colors.getString("color_" + colorString);
    } catch (MissingResourceException e) {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_unknown", ImmutableMap.of("color", colorString)));
        return;
    }
    if (filteredItems.isEmpty()) {
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_no_item", ImmutableMap.of("color", colorString)));
        interpretation.setHint(answerFormatter.getStandardTagHint(intent.getEntities()));
    } else if (filteredItems.size() == 1) {
        interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
        eventPublisher.post(ItemEventFactory.createCommandEvent(filteredItems.get(0).getName(), new HSBType(hsbValue)));
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_single", ImmutableMap.of("color", colorString)));
    } else {
        interpretation.setCard(cardBuilder.buildCard(intent, filteredItems));
        for (Item item : filteredItems) {
            eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), new HSBType(hsbValue)));
        }
        interpretation.setAnswer(answerFormatter.getRandomAnswer("set_color_multiple", ImmutableMap.of("count", String.valueOf(filteredItems.size()), "color", colorString)));
    }
}
Also used : IntentInterpretation(org.openhab.ui.habot.nlp.IntentInterpretation) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) ImmutableMap(com.google.common.collect.ImmutableMap) MissingResourceException(java.util.MissingResourceException) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) CardBuilder(org.openhab.ui.habot.card.CardBuilder) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Collectors(java.util.stream.Collectors) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) List(java.util.List) HSBType(org.eclipse.smarthome.core.library.types.HSBType) ResourceBundle(java.util.ResourceBundle) Locale(java.util.Locale) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Skill(org.openhab.ui.habot.nlp.Skill) AbstractItemIntentInterpreter(org.openhab.ui.habot.nlp.AbstractItemIntentInterpreter) Reference(org.osgi.service.component.annotations.Reference) Intent(org.openhab.ui.habot.nlp.Intent) Locale(java.util.Locale) Item(org.eclipse.smarthome.core.items.Item) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Example 19 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class GroupItemTest method assertThatGroupItemWithColoritemBaseItemConversionWorks.

@Test
public void assertThatGroupItemWithColoritemBaseItemConversionWorks() {
    // initially this group has State UndefType.NULL
    GroupItem groupItem = new GroupItem("root", new ColorItem("myColor"));
    State groupStateAsPercent = groupItem.getStateAs(PercentType.class);
    // a state conversion from NULL to PercentType should not be possible
    assertNull(groupStateAsPercent);
    // init group
    groupItem.setState(new HSBType("200,80,80"));
    groupStateAsPercent = groupItem.getStateAs(PercentType.class);
    assertTrue(groupStateAsPercent instanceof PercentType);
    assertThat(((PercentType) groupStateAsPercent).intValue(), is(80));
}
Also used : State(org.eclipse.smarthome.core.types.State) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 20 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class ColorItemTest method testSetStateWithHSBType.

@Test
public void testSetStateWithHSBType() {
    ColorItem item = new ColorItem("test");
    item.setState(new HSBType(new DecimalType(75), new PercentType(75), new PercentType(75)));
    assertEquals(new HSBType(new DecimalType(75), new PercentType(75), new PercentType(75)), item.getState());
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Test(org.junit.Test)

Aggregations

HSBType (org.eclipse.smarthome.core.library.types.HSBType)34 Test (org.junit.Test)19 PercentType (org.eclipse.smarthome.core.library.types.PercentType)16 State (org.eclipse.smarthome.core.types.State)10 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)4 IncreaseDecreaseType (org.eclipse.smarthome.core.library.types.IncreaseDecreaseType)3 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)3 StringType (org.eclipse.smarthome.core.library.types.StringType)3 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)2 RefreshType (org.eclipse.smarthome.core.types.RefreshType)2 Colorpicker (org.eclipse.smarthome.model.sitemap.Colorpicker)2 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)2 Switch (org.eclipse.smarthome.model.sitemap.Switch)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Locale (java.util.Locale)1 MissingResourceException (java.util.MissingResourceException)1