mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 21:31:53 +01:00
[automation] Added ItemStateUpdate action (#1970)
* Added ItemUpdate action Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
7f3de9ae02
commit
f83fc07c6a
@ -32,6 +32,7 @@ import org.openhab.core.automation.internal.module.handler.ItemCommandActionHand
|
|||||||
import org.openhab.core.automation.internal.module.handler.ItemCommandTriggerHandler;
|
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.ItemStateConditionHandler;
|
||||||
import org.openhab.core.automation.internal.module.handler.ItemStateTriggerHandler;
|
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.RuleEnablementActionHandler;
|
||||||
import org.openhab.core.automation.internal.module.handler.RunRuleActionHandler;
|
import org.openhab.core.automation.internal.module.handler.RunRuleActionHandler;
|
||||||
import org.openhab.core.automation.internal.module.handler.SystemTriggerHandler;
|
import org.openhab.core.automation.internal.module.handler.SystemTriggerHandler;
|
||||||
@ -63,10 +64,11 @@ public class CoreModuleHandlerFactory extends BaseModuleHandlerFactory implement
|
|||||||
ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID, GroupStateTriggerHandler.UPDATE_MODULE_TYPE_ID,
|
ItemStateTriggerHandler.CHANGE_MODULE_TYPE_ID, GroupStateTriggerHandler.UPDATE_MODULE_TYPE_ID,
|
||||||
GroupStateTriggerHandler.CHANGE_MODULE_TYPE_ID, ThingStatusTriggerHandler.UPDATE_MODULE_TYPE_ID,
|
GroupStateTriggerHandler.CHANGE_MODULE_TYPE_ID, ThingStatusTriggerHandler.UPDATE_MODULE_TYPE_ID,
|
||||||
ThingStatusTriggerHandler.CHANGE_MODULE_TYPE_ID, ItemStateConditionHandler.ITEM_STATE_CONDITION,
|
ThingStatusTriggerHandler.CHANGE_MODULE_TYPE_ID, ItemStateConditionHandler.ITEM_STATE_CONDITION,
|
||||||
ItemCommandActionHandler.ITEM_COMMAND_ACTION, GenericEventTriggerHandler.MODULE_TYPE_ID,
|
ItemCommandActionHandler.ITEM_COMMAND_ACTION, ItemStateUpdateActionHandler.ITEM_STATE_UPDATE_ACTION,
|
||||||
ChannelEventTriggerHandler.MODULE_TYPE_ID, GenericEventConditionHandler.MODULETYPE_ID,
|
GenericEventTriggerHandler.MODULE_TYPE_ID, ChannelEventTriggerHandler.MODULE_TYPE_ID,
|
||||||
GenericEventConditionHandler.MODULETYPE_ID, CompareConditionHandler.MODULE_TYPE,
|
GenericEventConditionHandler.MODULETYPE_ID, GenericEventConditionHandler.MODULETYPE_ID,
|
||||||
SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID, RuleEnablementActionHandler.UID, RunRuleActionHandler.UID);
|
CompareConditionHandler.MODULE_TYPE, SystemTriggerHandler.STARTLEVEL_MODULE_TYPE_ID,
|
||||||
|
RuleEnablementActionHandler.UID, RunRuleActionHandler.UID);
|
||||||
|
|
||||||
private ItemRegistry itemRegistry;
|
private ItemRegistry itemRegistry;
|
||||||
private EventPublisher eventPublisher;
|
private EventPublisher eventPublisher;
|
||||||
@ -208,6 +210,8 @@ public class CoreModuleHandlerFactory extends BaseModuleHandlerFactory implement
|
|||||||
postCommandActionHandler.setEventPublisher(eventPublisher);
|
postCommandActionHandler.setEventPublisher(eventPublisher);
|
||||||
postCommandActionHandler.setItemRegistry(itemRegistry);
|
postCommandActionHandler.setItemRegistry(itemRegistry);
|
||||||
return postCommandActionHandler;
|
return postCommandActionHandler;
|
||||||
|
} else if (ItemStateUpdateActionHandler.ITEM_STATE_UPDATE_ACTION.equals(moduleTypeUID)) {
|
||||||
|
return new ItemStateUpdateActionHandler((Action) module, eventPublisher, itemRegistry);
|
||||||
} else if (RuleEnablementActionHandler.UID.equals(moduleTypeUID)) {
|
} else if (RuleEnablementActionHandler.UID.equals(moduleTypeUID)) {
|
||||||
return new RuleEnablementActionHandler((Action) module);
|
return new RuleEnablementActionHandler((Action) module);
|
||||||
} else if (RunRuleActionHandler.UID.equals(moduleTypeUID)) {
|
} else if (RunRuleActionHandler.UID.equals(moduleTypeUID)) {
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -56,6 +56,63 @@
|
|||||||
"description": "command that will be sent to the item."
|
"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."
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user