Search in sources :

Example 36 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class HueBinding method execute.

/**
     * Get current hue settings of the bulbs and update the items that are connected with the bulb.
     * The refreshinterval determines the polling frequency.
     */
@Override
public void execute() {
    if (activeBridge != null) {
        // Get settings and update the bulbs
        // Observation : If the power of a hue lamp is removed, the status is not updated in hue hub.
        // The heartbeat functionality should fix this, but
        logger.debug("Start Hue data refresh");
        HueSettings settings = activeBridge.getSettings();
        if (settings == null) {
            logger.warn("Hue settings were null, maybe misconfigured bridge IP.");
            return;
        } else if (!settings.isAuthorized()) {
            logger.warn("openHAB not authorized to access Hue bridge");
            return;
        }
        Set<String> keys = settings.getKeys();
        for (String key : keys) {
            try {
                HueBulb bulb = bulbCache.get(key);
                if (bulb == null) {
                    bulb = new HueBulb(activeBridge, key, settings);
                    bulbCache.put(key, bulb);
                }
                bulb.getStatus(settings);
            } catch (NumberFormatException e) {
                logger.warn("lights index {} is not a number", key);
            }
        }
        // Multiple items of different types can be linked to one bulb.
        for (HueBindingProvider provider : this.providers) {
            for (String hueItemName : provider.getInBindingItemNames()) {
                HueBindingConfig deviceConfig = getConfigForItemName(hueItemName);
                if (deviceConfig != null) {
                    HueBulb bulb = bulbCache.get(deviceConfig.getDeviceId());
                    if (bulb != null) {
                        //
                        if ((bulb.getIsOn() == true) && (bulb.getIsReachable() == true)) {
                            if ((deviceConfig.itemStateOnOffType == null) || (deviceConfig.itemStateOnOffType.equals(OnOffType.ON) == false)) {
                                eventPublisher.postUpdate(hueItemName, OnOffType.ON);
                                deviceConfig.itemStateOnOffType = OnOffType.ON;
                            }
                        } else {
                            if ((deviceConfig.itemStateOnOffType == null) || (deviceConfig.itemStateOnOffType.equals(OnOffType.OFF) == false)) {
                                eventPublisher.postUpdate(hueItemName, OnOffType.OFF);
                                deviceConfig.itemStateOnOffType = OnOffType.OFF;
                            }
                        }
                        if (deviceConfig.getType().equals(BindingType.brightness)) {
                            if ((bulb.getIsOn() == true) && (bulb.getIsReachable() == true)) {
                                // Only postUpdate when bulb is on, otherwise dimmer item is not retaining state and
                                // shows to max brightness value
                                PercentType newPercent = new PercentType((int) Math.round((bulb.getBrightness() * (double) 100) / HueBulb.MAX_BRIGHTNESS));
                                if ((deviceConfig.itemStatePercentType == null) || (deviceConfig.itemStatePercentType.equals(newPercent) == false)) {
                                    eventPublisher.postUpdate(hueItemName, newPercent);
                                    deviceConfig.itemStatePercentType = newPercent;
                                }
                            }
                        } else if (deviceConfig.getType().equals(BindingType.rgb)) {
                            if ((bulb.getIsOn() == true) && (bulb.getIsReachable() == true)) {
                                // Only postUpdate when bulb is on, otherwise color item is not retaining state and
                                // shows to max brightness value
                                DecimalType decimalHue = new DecimalType(bulb.getHue() / (double) 182);
                                PercentType percentBrightness = new PercentType((int) Math.round((bulb.getBrightness() * (double) 100) / HueBulb.MAX_BRIGHTNESS));
                                PercentType percentSaturation = new PercentType((int) Math.round((bulb.getSaturation() * (double) 100) / HueBulb.MAX_SATURATION));
                                HSBType newHsb = new HSBType(decimalHue, percentSaturation, percentBrightness);
                                if ((deviceConfig.itemStateHSBType == null) || (deviceConfig.itemStateHSBType.equals(newHsb) == false)) {
                                    eventPublisher.postUpdate(hueItemName, newHsb);
                                    deviceConfig.itemStateHSBType = newHsb;
                                }
                            }
                        }
                    }
                }
            }
        }
        logger.debug("Done Hue data refresh.");
    }
}
Also used : HueSettings(org.openhab.binding.hue.internal.data.HueSettings) HueBulb(org.openhab.binding.hue.internal.hardware.HueBulb) DecimalType(org.openhab.core.library.types.DecimalType) HueBindingProvider(org.openhab.binding.hue.HueBindingProvider) PercentType(org.openhab.core.library.types.PercentType) HSBType(org.openhab.core.library.types.HSBType)

Example 37 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class DeviceFeatureListener method publishState.

/**
     * Publish the state. In the case of PercentType, if the value is
     * 0, send a OnOffType.OFF and if the value is 100, send a OnOffType.ON.
     * That way an OpenHAB Switch will work properly with a Insteon dimmer,
     * as long it is used like a switch (On/Off). An openHAB DimmerItem will
     * internally convert the ON back to 100% and OFF back to 0, so there is
     * no need to send both 0/OFF and 100/ON.
     *
     * @param state the new state of the feature
     */
private void publishState(State state) {
    State publishState = state;
    if (state instanceof PercentType) {
        if (state.equals(PercentType.ZERO)) {
            publishState = OnOffType.OFF;
        } else if (state.equals(PercentType.HUNDRED)) {
            publishState = OnOffType.ON;
        }
    }
    pollRelatedDevices();
    m_eventPublisher.postUpdate(m_itemName, publishState);
}
Also used : State(org.openhab.core.types.State) PercentType(org.openhab.core.library.types.PercentType)

Example 38 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class TinkerforgeBinding method postUpdate.

private void postUpdate(String uid, String subId, TinkerforgeValue sensorValue) {
    // TODO undef handling
    logger.trace("postUpdate called for uid {} subid {}", uid, subId);
    Map<String, TinkerforgeBindingProvider> providerMap = getBindingProviders(uid, subId);
    if (providerMap.size() == 0) {
        logger.debug("{} found no item for uid {}, subid {}", LoggerConstants.TFMODELUPDATE, uid, subId);
    }
    for (Entry<String, TinkerforgeBindingProvider> entry : providerMap.entrySet()) {
        String itemName = entry.getKey();
        TinkerforgeBindingProvider provider = entry.getValue();
        Class<? extends Item> itemType = provider.getItemType(itemName);
        State value = UnDefType.UNDEF;
        if (sensorValue instanceof DecimalValue) {
            if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                value = DecimalType.valueOf(String.valueOf(sensorValue));
                logger.trace("found item to update for DecimalValue {}", itemName);
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = sensorValue.equals(DecimalValue.ZERO) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = sensorValue.equals(DecimalValue.ZERO) ? OnOffType.OFF : OnOffType.ON;
            } else {
                logger.trace("no update for DecimalValue for item {}", itemName);
                continue;
            }
        } else if (sensorValue instanceof HighLowValue) {
            if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                value = sensorValue == HighLowValue.HIGH ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = sensorValue == HighLowValue.HIGH ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = sensorValue == HighLowValue.HIGH ? OnOffType.ON : OnOffType.OFF;
            } else {
                continue;
            }
        } else if (sensorValue instanceof OnOffValue) {
            if (itemType.isAssignableFrom(NumberItem.class) || itemType.isAssignableFrom(StringItem.class)) {
                value = sensorValue == OnOffValue.ON ? DecimalType.valueOf("1") : DecimalType.valueOf("0");
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = sensorValue == OnOffValue.ON ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = sensorValue == OnOffValue.ON ? OnOffType.ON : OnOffType.OFF;
            } else {
                continue;
            }
        } else if (sensorValue instanceof PercentValue) {
            if (itemType.isAssignableFrom(SwitchItem.class)) {
                value = ((PercentValue) sensorValue).toBigDecimal().compareTo(BigDecimal.ZERO) == 1 ? OnOffType.ON : OnOffType.OFF;
                logger.debug("switch found {}", itemName);
            } else if (itemType.isAssignableFrom(RollershutterItem.class) || itemType.isAssignableFrom(DimmerItem.class)) {
                value = new PercentType(((PercentValue) sensorValue).toBigDecimal());
                logger.debug("Rollershutter or dimmer found {} {}", itemName);
            } else if (itemType.isAssignableFrom(ContactItem.class)) {
                value = ((PercentValue) sensorValue).toBigDecimal().compareTo(BigDecimal.ZERO) == -1 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                logger.debug("contact found {}", itemName);
            } else {
                continue;
            }
        } else if (sensorValue instanceof DirectionValue) {
            if (itemType.isAssignableFrom(RollershutterItem.class)) {
                value = sensorValue == DirectionValue.RIGHT ? UpDownType.UP : UpDownType.DOWN;
                logger.trace("found item to update for UpDownValue {}", itemName);
            } else {
                continue;
            }
        } else if (sensorValue instanceof HSBValue) {
            if (itemType.isAssignableFrom(ColorItem.class)) {
                logger.trace("found item to update for HSBValue {}", itemName);
                value = ((HSBValue) sensorValue).getHsbValue();
            }
        } else if (sensorValue == UnDefValue.UNDEF || sensorValue == null) {
            value = UnDefType.UNDEF;
        }
        eventPublisher.postUpdate(itemName, value);
        logger.debug("{} postupdate: found sensorValue: {} for item {}", LoggerConstants.TFMODELUPDATE, sensorValue, itemName);
    }
}
Also used : OnOffValue(org.openhab.binding.tinkerforge.internal.types.OnOffValue) PercentValue(org.openhab.binding.tinkerforge.internal.types.PercentValue) DirectionValue(org.openhab.binding.tinkerforge.internal.types.DirectionValue) ContactItem(org.openhab.core.library.items.ContactItem) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) TinkerforgeBindingProvider(org.openhab.binding.tinkerforge.TinkerforgeBindingProvider) DecimalValue(org.openhab.binding.tinkerforge.internal.types.DecimalValue) State(org.openhab.core.types.State) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DimmerItem(org.openhab.core.library.items.DimmerItem) HSBValue(org.openhab.binding.tinkerforge.internal.types.HSBValue) HighLowValue(org.openhab.binding.tinkerforge.internal.types.HighLowValue) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 39 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class TellstickBinding method resolveCommand.

private State resolveCommand(Method method, String data) {
    State cmd = null;
    if (method == Method.TURNON) {
        cmd = OnOffType.ON;
    } else if (method == Method.TURNOFF) {
        cmd = OnOffType.OFF;
    } else if (method == Method.DIM) {
        double value = ((Double.valueOf(data) * 100) / 255);
        cmd = new PercentType((int) Math.round(value));
    }
    return cmd;
}
Also used : State(org.openhab.core.types.State) PercentType(org.openhab.core.library.types.PercentType)

Example 40 with PercentType

use of org.openhab.core.library.types.PercentType in project openhab1-addons by openhab.

the class TellstickController method increaseDecrease.

private void increaseDecrease(TellstickDevice dev, IncreaseDecreaseType increaseDecreaseType) throws TellstickException {
    String strValue = dev.getData();
    double value = 0;
    if (strValue != null) {
        value = Double.valueOf(strValue);
    }
    int percent = (int) Math.round((value / 255) * 100);
    if (IncreaseDecreaseType.INCREASE == increaseDecreaseType) {
        percent = Math.min(percent + 10, 100);
    } else if (IncreaseDecreaseType.DECREASE == increaseDecreaseType) {
        percent = Math.max(percent - 10, 0);
    }
    dim(dev, new PercentType(percent));
}
Also used : PercentType(org.openhab.core.library.types.PercentType)

Aggregations

PercentType (org.openhab.core.library.types.PercentType)105 DecimalType (org.openhab.core.library.types.DecimalType)43 State (org.openhab.core.types.State)29 StringType (org.openhab.core.library.types.StringType)27 DimmerItem (org.openhab.core.library.items.DimmerItem)22 Test (org.junit.Test)21 HSBType (org.openhab.core.library.types.HSBType)19 OnOffType (org.openhab.core.library.types.OnOffType)19 RollershutterItem (org.openhab.core.library.items.RollershutterItem)17 NumberItem (org.openhab.core.library.items.NumberItem)16 BigDecimal (java.math.BigDecimal)14 SwitchItem (org.openhab.core.library.items.SwitchItem)13 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)13 ColorItem (org.openhab.core.library.items.ColorItem)11 DateTimeType (org.openhab.core.library.types.DateTimeType)11 Calendar (java.util.Calendar)9 ContactItem (org.openhab.core.library.items.ContactItem)9 UpDownType (org.openhab.core.library.types.UpDownType)9 StopMoveType (org.openhab.core.library.types.StopMoveType)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6