Search in sources :

Example 51 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class DigitalSTROMBinding method updateItemState.

private void updateItemState(Item item) {
    if (item != null) {
        Device device = deviceMap.get(item.getName());
        if (device != null) {
            State state = null;
            if (item instanceof DimmerItem) {
                state = new PercentType(getPercent(device.getMaxOutPutValue(), device.getOutputValue()));
            } else if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
                state = device.getOutputValue() > 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof NumberItem) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
                if (confItem != null) {
                    if (confItem.sensor != null) {
                        int value = -1;
                        switch(confItem.sensor) {
                            case TEMPERATURE_INDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_INDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            case TEMPERATURE_OUTDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_OUTDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            default:
                                break;
                        }
                    } else if (confItem.consumption != null) {
                        int value = -1;
                        switch(confItem.consumption) {
                            case ACTIVE_POWER:
                                value = device.getPowerConsumption();
                                break;
                            case OUTPUT_CURRENT:
                                value = device.getEnergyMeterValue();
                                if (value != -1) {
                                    state = new DecimalType(value);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            } else if (item instanceof RollershutterItem) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
                if (confItem != null) {
                    if (confItem.context != null && confItem.context.equals(ContextConfig.slat)) {
                        int output = getPercent(device.getMaxSlatPosition(), device.getSlatPosition());
                        state = new PercentType(100 - output);
                    } else if (confItem.context != null && confItem.context.equals(ContextConfig.awning)) {
                        int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
                        state = new PercentType(output);
                    } else {
                        int output = getPercent(device.getMaxOutPutValue(), device.getOutputValue());
                        state = new PercentType(100 - output);
                    }
                }
            } else if (item instanceof StringItem) {
                DigitalSTROMBindingConfig confItem = getConfigForItemName(item.getName());
                if (confItem != null) {
                    if (confItem.sensor != null) {
                        int value = -1;
                        switch(confItem.sensor) {
                            case TEMPERATURE_INDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_INDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            case TEMPERATURE_OUTDOORS:
                                value = device.getTemperatureSensorValue();
                                if (value != -1) {
                                    // Celsius
                                    // TODO use resolution
                                    state = new DecimalType((double) (value) / 40 - 43.15);
                                // from sensor
                                }
                                break;
                            case RELATIVE_HUMIDITY_OUTDOORS:
                                value = device.getHumiditySensorValue();
                                if (value != -1) {
                                    // Percent
                                    // TODO use resolution from
                                    state = new DecimalType((double) (value) / 40);
                                // sensor
                                }
                                break;
                            default:
                                break;
                        }
                    } else if (confItem.consumption != null) {
                        int value = -1;
                        switch(confItem.consumption) {
                            case ACTIVE_POWER:
                                value = device.getPowerConsumption();
                                if (value != -1) {
                                    state = new DecimalType(value);
                                }
                                break;
                            case OUTPUT_CURRENT:
                                value = device.getEnergyMeterValue();
                                if (value != -1) {
                                    state = new DecimalType(value);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            eventPublisher.postUpdate(item.getName(), state);
        } else {
            logger.error("couldn't update item state, because device is null: " + item.getName());
        }
    } else {
        logger.error("couldn't update item state, because item is null");
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Device(org.openhab.binding.digitalstrom.internal.client.entity.Device) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DigitalSTROMBindingConfig(org.openhab.binding.digitalstrom.internal.config.DigitalSTROMBindingConfig) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 52 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class BticinoDevice method handleEvent.

public void handleEvent(ProtocolRead p_protocol_read) throws Exception {
    // the events on the bus are now received
    // map them to events on the openhab bus
    logger.debug("Gateway [" + m_gateway_id + "], Bticino WHO [" + p_protocol_read.getProperty("who") + "], WHAT [" + p_protocol_read.getProperty("what") + "], WHERE [" + p_protocol_read.getProperty("where") + "]");
    // Get all the configs that are connected to this (who,where), multiple
    // possible
    List<BticinoBindingConfig> l_binding_configs = m_bticino_binding.getItemForBticinoBindingConfig(p_protocol_read.getProperty("who"), p_protocol_read.getProperty("where"));
    // log it when an event has occured that no item is bound to
    if (l_binding_configs.isEmpty()) {
        logger.debug("Gateway [" + m_gateway_id + "], No Item found for bticino event, WHO [" + p_protocol_read.getProperty("who") + "], WHAT [" + p_protocol_read.getProperty("what") + "], WHERE [" + p_protocol_read.getProperty("where") + "]");
    }
    // every item associated with this who/where update the status
    for (BticinoBindingConfig l_binding_config : l_binding_configs) {
        // Get the Item out of the config
        Item l_item = l_binding_config.getItem();
        if (l_item instanceof SwitchItem) {
            // Lights
            if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("lighting")) {
                logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR SwitchItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
                if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Light ON")) {
                    eventPublisher.postUpdate(l_item.getName(), OnOffType.ON);
                } else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Light OFF")) {
                    eventPublisher.postUpdate(l_item.getName(), OnOffType.OFF);
                }
            } else // CENs
            if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("CEN Basic and Evolved")) {
                // Pushbutton virtual address must match
                if (l_binding_config.what.equalsIgnoreCase(p_protocol_read.getProperty("what"))) {
                    logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR SwitchItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
                    if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual pressure")) {
                        // only returns when finished
                        eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
                    } else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual release after short pressure")) {
                        // only returns when finished
                        eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
                    } else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual release after an extended pressure")) {
                        // only returns when finished
                        eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
                    } else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Virtual extended pressure")) {
                        // only returns when finished
                        eventPublisher.sendCommand(l_item.getName(), OnOffType.ON);
                    }
                }
            }
        } else if (l_item instanceof RollershutterItem) {
            logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR RollershutterItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
            if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("automation")) {
                if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Automation UP")) {
                    eventPublisher.postUpdate(l_item.getName(), UpDownType.UP);
                } else if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Automation DOWN")) {
                    eventPublisher.postUpdate(l_item.getName(), UpDownType.DOWN);
                }
            }
        } else if (l_item instanceof NumberItem) {
            logger.debug("Gateway [" + m_gateway_id + "], RECEIVED EVENT FOR NumberItem [" + l_item.getName() + "], TRANSLATE TO OPENHAB BUS EVENT");
            // THERMOREGULATION
            if (p_protocol_read.getProperty("messageType").equalsIgnoreCase("thermoregulation")) {
                if (p_protocol_read.getProperty("messageDescription").equalsIgnoreCase("Temperature value")) {
                    eventPublisher.postUpdate(l_item.getName(), DecimalType.valueOf(p_protocol_read.getProperty("temperature")));
                }
            }
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) NumberItem(org.openhab.core.library.items.NumberItem) BticinoBindingConfig(org.openhab.binding.bticino.internal.BticinoGenericBindingProvider.BticinoBindingConfig) RollershutterItem(org.openhab.core.library.items.RollershutterItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 53 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class FritzahaGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    FritzahaDevice config = null;
    TimeDef timedef = null;
    String[] configParts = bindingConfig.trim().split(",");
    if (configParts.length < 2) {
        throw new BindingConfigParseException("FritzAHA items must start with <hostID>,<deviceID/AIN>");
    }
    if (item instanceof SwitchItem) {
        if (configParts.length != 2) {
            throw new BindingConfigParseException("FritzAHA switches must be of format <hostID>,<deviceID/AIN>");
        }
        if (configParts[1].length() > 8) {
            config = new FritzahaWebserviceSwitch(configParts[0], configParts[1]);
        } else {
            config = new FritzahaQueryscriptSwitch(configParts[0], configParts[1]);
        }
    } else if (item instanceof NumberItem) {
        if (configParts.length < 3 || configParts.length > 4) {
            throw new BindingConfigParseException("FritzAHA meters must be of format <hostID>,<deviceID/AIN>,<valueToMeasure>[,timespec]");
        } else if (configParts.length == 4 && !"energy".equalsIgnoreCase(configParts[2])) {
            throw new BindingConfigParseException("FritzAHA non-energy meters must be of format <hostID>,<deviceID/AIN>,<valueToMeasure>");
        } else if (configParts[1].length() > 8) {
            if ("power".equalsIgnoreCase(configParts[2])) {
                config = new FritzahaWebserviceMeter(configParts[0], configParts[1], MeterType.POWER);
            } else if ("energy".equalsIgnoreCase(configParts[2])) {
                config = new FritzahaWebserviceMeter(configParts[0], configParts[1], MeterType.ENERGY);
            } else if ("temperature".equalsIgnoreCase(configParts[2])) {
                config = new FritzahaWebserviceMeter(configParts[0], configParts[1], MeterType.TEMPERATURE);
            } else {
                logger.warn("Could not configure item " + item + " - Unsupported meter type for webservice");
                return;
            }
        } else {
            if ("voltage".equalsIgnoreCase(configParts[2])) {
                config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.VOLTAGE);
            } else if ("current".equalsIgnoreCase(configParts[2])) {
                config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.CURRENT);
            } else if ("power".equalsIgnoreCase(configParts[2])) {
                config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.POWER);
            } else if ("energy".equalsIgnoreCase(configParts[2])) {
                if (configParts.length > 3) {
                    if ("mins".equalsIgnoreCase(configParts[3])) {
                        timedef = TimeDef.MINUTES;
                    } else if ("day".equalsIgnoreCase(configParts[3])) {
                        timedef = TimeDef.DAY;
                    } else if ("month".equalsIgnoreCase(configParts[3])) {
                        timedef = TimeDef.MONTH;
                    } else if ("year".equalsIgnoreCase(configParts[3])) {
                        timedef = TimeDef.YEAR;
                    } else {
                        timedef = TimeDef.YEAR;
                        logger.warn("Timedef of item " + item + "is set to default YEAR. " + "Please check your syntax. Shall be year, month, day or mins.");
                    }
                } else {
                    timedef = TimeDef.YEAR;
                    logger.debug("Timedef of item " + item + "is set to default YEAR because no timespec was given.");
                }
                config = new FritzahaQueryscriptMeter(configParts[0], configParts[1], MeterType.ENERGY, timedef);
            } else {
                logger.warn("Could not configure item " + item + " - Unsupported meter type for query script");
                return;
            }
        }
    } else {
        logger.warn("Could not configure item " + item + " - Unsupported item type");
    }
    if (config != null) {
        addBindingConfig(item, config);
    } else {
        logger.error("Could not configure item " + item + " - An error occurred");
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) FritzahaQueryscriptMeter(org.openhab.binding.fritzaha.internal.hardware.devices.FritzahaQueryscriptMeter) FritzahaQueryscriptSwitch(org.openhab.binding.fritzaha.internal.hardware.devices.FritzahaQueryscriptSwitch) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) FritzahaWebserviceSwitch(org.openhab.binding.fritzaha.internal.hardware.devices.FritzahaWebserviceSwitch) FritzahaWebserviceMeter(org.openhab.binding.fritzaha.internal.hardware.devices.FritzahaWebserviceMeter) FritzahaDevice(org.openhab.binding.fritzaha.internal.hardware.interfaces.FritzahaDevice) SwitchItem(org.openhab.core.library.items.SwitchItem) TimeDef(org.openhab.binding.fritzaha.internal.hardware.interfaces.FritzahaOutletMeter.TimeDef)

Example 54 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class ExecGenericBindingProviderTest method testParseBindingConfig.

@Test
public void testParseBindingConfig() throws BindingConfigParseException {
    ExecBindingConfig config = new ExecGenericBindingProvider.ExecBindingConfig();
    String bindingConfig = "ON:some command to execute, OFF: 'other command with comma\\, and \\'quotes\\' and slashes \\\\ ', *:and a fallback";
    SwitchItem item = new SwitchItem("");
    provider.parseLegacyOutBindingConfig(item, bindingConfig, config);
    Assert.assertEquals(3, config.size());
    Assert.assertEquals("some command to execute", config.get(OnOffType.ON).commandLine);
    Assert.assertEquals("other command with comma, and 'quotes' and slashes \\ ", config.get(OnOffType.OFF).commandLine);
    Assert.assertEquals("and a fallback", config.get(StringType.valueOf("*")).commandLine);
}
Also used : ExecBindingConfig(org.openhab.binding.exec.internal.ExecGenericBindingProvider.ExecBindingConfig) SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Example 55 with SwitchItem

use of org.openhab.core.library.items.SwitchItem in project openhab1-addons by openhab.

the class ExecGenericBindingProviderTest method testParseBindingConfigIn.

@Test
public void testParseBindingConfigIn() throws BindingConfigParseException {
    String cmdLine = "/usr/bin/uptime";
    String itemName = "Switch";
    SwitchItem item = new SwitchItem(itemName);
    String bindingConfig = "<[" + cmdLine + ":60000:]";
    provider.processBindingConfiguration("New", item, bindingConfig);
    Assert.assertTrue(provider.providesBinding());
    Assert.assertTrue(provider.providesBindingFor(itemName));
    Assert.assertEquals(cmdLine, provider.getCommandLine(itemName));
    Assert.assertEquals(60000, provider.getRefreshInterval(itemName));
    Assert.assertEquals("", provider.getTransformation(itemName));
    List<String> itemNames = provider.getInBindingItemNames();
    Assert.assertEquals(itemName, itemNames.get(0));
}
Also used : SwitchItem(org.openhab.core.library.items.SwitchItem) Test(org.junit.Test)

Aggregations

SwitchItem (org.openhab.core.library.items.SwitchItem)55 NumberItem (org.openhab.core.library.items.NumberItem)25 DimmerItem (org.openhab.core.library.items.DimmerItem)21 Test (org.junit.Test)18 ContactItem (org.openhab.core.library.items.ContactItem)17 Item (org.openhab.core.items.Item)15 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 DecimalType (org.openhab.core.library.types.DecimalType)15 ColorItem (org.openhab.core.library.items.ColorItem)12 StringItem (org.openhab.core.library.items.StringItem)12 PercentType (org.openhab.core.library.types.PercentType)11 StringType (org.openhab.core.library.types.StringType)10 State (org.openhab.core.types.State)8 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)8 DateTimeItem (org.openhab.core.library.items.DateTimeItem)7 DateTimeType (org.openhab.core.library.types.DateTimeType)7 Calendar (java.util.Calendar)6 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)5 HSBType (org.openhab.core.library.types.HSBType)5 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)4