Search in sources :

Example 1 with ActionConfig

use of org.openhab.binding.homematic.internal.config.binding.ActionConfig in project openhab1-addons by openhab.

the class ItemDisabler method run.

/**
     * Sends the OFF commands to the openHAB bus.
     */
@Override
public void run() {
    Iterator<Map.Entry<HomematicBindingConfig, Long>> iterator = itemsToDisable.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<HomematicBindingConfig, Long> entry = iterator.next();
        long diff = System.currentTimeMillis() - entry.getValue();
        if (diff > MIN_AGE) {
            new ProviderItemIterator().iterate(entry.getKey(), new ProviderItemIteratorCallback() {

                @Override
                public void next(HomematicBindingConfig providerBindingConfig, Item item, Converter<?> converter) {
                    HmValueItem hmValueItem = context.getStateHolder().getState(providerBindingConfig);
                    if (providerBindingConfig instanceof ProgramConfig || providerBindingConfig instanceof ActionConfig) {
                        context.getEventPublisher().postUpdate(item.getName(), OnOffType.OFF);
                    } else {
                        hmValueItem.setValue(converter.convertToBinding(OnOffType.OFF, hmValueItem));
                        State state = converter.convertFromBinding(hmValueItem);
                        context.getEventPublisher().postUpdate(item.getName(), state);
                    }
                    logger.debug("Disabled Item {} with binding {}", item.getName(), providerBindingConfig);
                }
            });
            iterator.remove();
        }
    }
}
Also used : ActionConfig(org.openhab.binding.homematic.internal.config.binding.ActionConfig) HmValueItem(org.openhab.binding.homematic.internal.model.HmValueItem) Item(org.openhab.core.items.Item) HomematicBindingConfig(org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig) State(org.openhab.core.types.State) ProviderItemIteratorCallback(org.openhab.binding.homematic.internal.communicator.ProviderItemIterator.ProviderItemIteratorCallback) ProgramConfig(org.openhab.binding.homematic.internal.config.binding.ProgramConfig) HashMap(java.util.HashMap) Map(java.util.Map) HmValueItem(org.openhab.binding.homematic.internal.model.HmValueItem)

Example 2 with ActionConfig

use of org.openhab.binding.homematic.internal.config.binding.ActionConfig in project openhab1-addons by openhab.

the class BindingConfigParser method parse.

/**
     * Parses the bindingConfig of an item and returns a HomematicBindingConfig.
     */
public HomematicBindingConfig parse(Item item, String bindingConfig) throws BindingConfigParseException {
    bindingConfig = StringUtils.trimToEmpty(bindingConfig);
    bindingConfig = StringUtils.removeStart(bindingConfig, "{");
    bindingConfig = StringUtils.removeEnd(bindingConfig, "}");
    String[] entries = bindingConfig.split("[,]");
    HomematicBindingConfigHelper helper = new HomematicBindingConfigHelper();
    for (String entry : entries) {
        String[] entryParts = StringUtils.trimToEmpty(entry).split("[=]");
        if (entryParts.length != 2) {
            throw new BindingConfigParseException("Each entry must have a key and a value");
        }
        String key = StringUtils.trim(entryParts[0]);
        // convert entry id to device if necessary
        if ("id".equalsIgnoreCase(key)) {
            logger.info("Please change the Homematic binding with the attribute 'id' to 'address' in entry: " + entry + " -> " + StringUtils.replace(entry, "id=", "address="));
            key = "address";
        }
        String value = StringUtils.trim(entryParts[1]);
        value = StringUtils.removeStart(value, "\"");
        value = StringUtils.removeEnd(value, "\"");
        try {
            helper.getClass().getDeclaredField(key).set(helper, value);
        } catch (Exception e) {
            throw new BindingConfigParseException("Could not set value " + value + " for attribute " + key);
        }
    }
    Converter<?> converter = null;
    // if (helper.isValidDatapoint() || helper.isValidVariable()) {
    // converter = instantiateConverter(helper.converter);
    // }
    BindingAction bindingAction = getBindingAction(item, helper.action);
    if (helper.isValidDatapoint()) {
        return new DatapointConfig(helper.address, helper.channel, helper.parameter, converter, bindingAction, helper.isForceUpdate(), NumberUtils.toDouble(helper.delay));
    } else if (helper.isValidVariable()) {
        return new VariableConfig(helper.variable, converter, bindingAction, helper.isForceUpdate(), NumberUtils.toDouble(helper.delay));
    } else if (helper.isValidProgram()) {
        if (!acceptsOnOffType(item)) {
            throw new BindingConfigParseException("Programs can only be attached to items which accepts OnOffType commands, ignoring item " + item.getName());
        }
        return new ProgramConfig(helper.program, bindingAction);
    } else if (bindingAction != null) {
        return new ActionConfig(bindingAction);
    } else {
        throw new BindingConfigParseException("Invalid binding: " + bindingConfig);
    }
}
Also used : VariableConfig(org.openhab.binding.homematic.internal.config.binding.VariableConfig) ActionConfig(org.openhab.binding.homematic.internal.config.binding.ActionConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DatapointConfig(org.openhab.binding.homematic.internal.config.binding.DatapointConfig) BindingAction(org.openhab.binding.homematic.internal.config.BindingAction) ProgramConfig(org.openhab.binding.homematic.internal.config.binding.ProgramConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 3 with ActionConfig

use of org.openhab.binding.homematic.internal.config.binding.ActionConfig in project openhab1-addons by openhab.

the class HomematicCommunicator method publishChangedItemToOpenhab.

/**
     * Called on startup or when some binding has changed, for example if a item
     * file is reloaded. Publishes the current States to openHAB.
     */
public void publishChangedItemToOpenhab(Item item, HomematicBindingConfig bindingConfig) {
    HmValueItem hmValueItem = context.getStateHolder().getState(bindingConfig);
    if (hmValueItem != null) {
        Converter<?> converter = context.getConverterFactory().createConverter(item, bindingConfig);
        if (converter != null) {
            State state = converter.convertFromBinding(hmValueItem);
            context.getEventPublisher().postUpdate(item.getName(), state);
        }
    } else if (bindingConfig instanceof ProgramConfig || bindingConfig instanceof ActionConfig) {
        context.getEventPublisher().postUpdate(item.getName(), OnOffType.OFF);
    } else {
        logger.warn("Can't find {}, value is not published to openHAB!", bindingConfig);
    }
}
Also used : ActionConfig(org.openhab.binding.homematic.internal.config.binding.ActionConfig) State(org.openhab.core.types.State) ProgramConfig(org.openhab.binding.homematic.internal.config.binding.ProgramConfig) HmValueItem(org.openhab.binding.homematic.internal.model.HmValueItem)

Aggregations

ActionConfig (org.openhab.binding.homematic.internal.config.binding.ActionConfig)3 ProgramConfig (org.openhab.binding.homematic.internal.config.binding.ProgramConfig)3 HmValueItem (org.openhab.binding.homematic.internal.model.HmValueItem)2 State (org.openhab.core.types.State)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ProviderItemIteratorCallback (org.openhab.binding.homematic.internal.communicator.ProviderItemIterator.ProviderItemIteratorCallback)1 BindingAction (org.openhab.binding.homematic.internal.config.BindingAction)1 DatapointConfig (org.openhab.binding.homematic.internal.config.binding.DatapointConfig)1 HomematicBindingConfig (org.openhab.binding.homematic.internal.config.binding.HomematicBindingConfig)1 VariableConfig (org.openhab.binding.homematic.internal.config.binding.VariableConfig)1 Item (org.openhab.core.items.Item)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1