Skip to content

Commit

Permalink
[automation] Added ItemStateUpdate action (openhab#1970)
Browse files Browse the repository at this point in the history
* Added ItemUpdate action

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored Dec 20, 2020
1 parent 7f3de9a commit f83fc07
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openhab.core.automation.internal.module.handler.ItemCommandTriggerHandler;
import org.openhab.core.automation.internal.module.handler.ItemStateConditionHandler;
import org.openhab.core.automation.internal.module.handler.ItemStateTriggerHandler;
import org.openhab.core.automation.internal.module.handler.ItemStateUpdateActionHandler;
import org.openhab.core.automation.internal.module.handler.RuleEnablementActionHandler;
import org.openhab.core.automation.internal.module.handler.RunRuleActionHandler;
import org.openhab.core.automation.internal.module.handler.SystemTriggerHandler;
Expand Down Expand Up @@ -63,10 +64,11 @@ public class CoreModuleHandlerFactory extends BaseModuleHandlerFactory implement
ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID, GroupStateTriggerHandler.UPDATE_MODULE_TYPE_ID,
GroupStateTriggerHandler.CHANGE_MODULE_TYPE_ID, ThingStatusTriggerHandler.UPDATE_MODULE_TYPE_ID,
ThingStatusTriggerHandler.CHANGE_MODULE_TYPE_ID, ItemStateConditionHandler.ITEM_STATE_CONDITION,
ItemCommandActionHandler.ITEM_COMMAND_ACTION, GenericEventTriggerHandler.MODULE_TYPE_ID,
ChannelEventTriggerHandler.MODULE_TYPE_ID, GenericEventConditionHandler.MODULETYPE_ID,
GenericEventConditionHandler.MODULETYPE_ID, CompareConditionHandler.MODULE_TYPE,
SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID, RuleEnablementActionHandler.UID, RunRuleActionHandler.UID);
ItemCommandActionHandler.ITEM_COMMAND_ACTION, ItemStateUpdateActionHandler.ITEM_STATE_UPDATE_ACTION,
GenericEventTriggerHandler.MODULE_TYPE_ID, ChannelEventTriggerHandler.MODULE_TYPE_ID,
GenericEventConditionHandler.MODULETYPE_ID, GenericEventConditionHandler.MODULETYPE_ID,
CompareConditionHandler.MODULE_TYPE, SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID,
RuleEnablementActionHandler.UID, RunRuleActionHandler.UID);

private ItemRegistry itemRegistry;
private EventPublisher eventPublisher;
Expand Down Expand Up @@ -208,6 +210,8 @@ protected synchronized ModuleHandler internalCreate(final Module module, final S
postCommandActionHandler.setEventPublisher(eventPublisher);
postCommandActionHandler.setItemRegistry(itemRegistry);
return postCommandActionHandler;
} else if (ItemStateUpdateActionHandler.ITEM_STATE_UPDATE_ACTION.equals(moduleTypeUID)) {
return new ItemStateUpdateActionHandler((Action) module, eventPublisher, itemRegistry);
} else if (RuleEnablementActionHandler.UID.equals(moduleTypeUID)) {
return new RuleEnablementActionHandler((Action) module);
} else if (RunRuleActionHandler.UID.equals(moduleTypeUID)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright (c) 2010-2020 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.internal.module.handler;

import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.Action;
import org.openhab.core.automation.handler.BaseActionModuleHandler;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.ItemEventFactory;
import org.openhab.core.items.events.ItemStateEvent;
import org.openhab.core.types.State;
import org.openhab.core.types.TypeParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This is an implementation of an ActionHandler. It sends state updates for items.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class ItemStateUpdateActionHandler extends BaseActionModuleHandler {

public static final String ITEM_STATE_UPDATE_ACTION = "core.ItemStateUpdateAction";
public static final String ITEM_NAME = "itemName";
public static final String STATE = "state";

private final Logger logger = LoggerFactory.getLogger(ItemStateUpdateActionHandler.class);

private final EventPublisher eventPublisher;
private final ItemRegistry itemRegistry;

public ItemStateUpdateActionHandler(Action module, EventPublisher eventPublisher, ItemRegistry itemRegistry) {
super(module);

this.eventPublisher = eventPublisher;
this.itemRegistry = itemRegistry;
}

@Override
public @Nullable Map<String, Object> execute(Map<String, Object> inputs) {
Configuration config = module.getConfiguration();
String itemName = (String) config.get(ITEM_NAME);
String state = (String) config.get(STATE);

if (itemName != null) {
try {
Item item = itemRegistry.getItem(itemName);

State stateObj = null;
final Object st = inputs.get(STATE);

if (st instanceof State) {
if (item.getAcceptedDataTypes().contains(st.getClass())) {
stateObj = (State) st;
}
} else {
stateObj = TypeParser.parseState(item.getAcceptedDataTypes(), state);
}
if (stateObj != null) {
final ItemStateEvent itemStateEvent = (ItemStateEvent) ItemEventFactory.createStateEvent(itemName,
stateObj);
logger.debug("Executing ItemStateEvent on Item {} with State {}", itemStateEvent.getItemName(),
itemStateEvent.getItemState());
eventPublisher.post(itemStateEvent);
} else {
logger.warn("State '{}' is not valid for item '{}'.", state, itemName);
}
} catch (ItemNotFoundException e) {
logger.error("Item with name {} not found in ItemRegistry.", itemName);
}
} else {
logger.error(
"Item state was not updated because the configuration was not correct: ItemName: {}, State: {}",
itemName, state);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,63 @@
"description": "command that will be sent to the item."
}
]
},
{
"uid": "core.ItemStateUpdateAction",
"label": "update an item state",
"description": "Updates the state of a specified item.",
"configDescriptions": [
{
"name": "itemName",
"type": "TEXT",
"label": "Item",
"context": "item",
"description": "the name of the item",
"required": true
},
{
"name": "state",
"type": "TEXT",
"label": "State",
"description": "the default state to be used to update the item if none is passed as an input value",
"required": true,
"limitToOptions": false,
"options": [
{
"label": "ON",
"value": "ON"
},
{
"label": "OFF",
"value": "OFF"
},
{
"label": "OPEN",
"value": "OPEN"
},
{
"label": "CLOSED",
"value": "CLOSED"
},
{
"label": "UP",
"value": "UP"
},
{
"label": "DOWN",
"value": "DOWN"
}
]
}
],
"inputs": [
{
"name": "state",
"type": "state",
"label": "State",
"description": "state that the item will be set to."
}
]
}
]
}

0 comments on commit f83fc07

Please sign in to comment.