From f83fc07c6aa58b423a4c2a77dd5d651b6db06b63 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sun, 20 Dec 2020 13:53:09 +0100 Subject: [PATCH] [automation] Added ItemStateUpdate action (#1970) * Added ItemUpdate action Signed-off-by: Christoph Weitkamp --- .../factory/CoreModuleHandlerFactory.java | 12 ++- .../handler/ItemStateUpdateActionHandler.java | 96 +++++++++++++++++++ .../automation/moduletypes/ItemActions.json | 57 +++++++++++ 3 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/ItemStateUpdateActionHandler.java diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/CoreModuleHandlerFactory.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/CoreModuleHandlerFactory.java index c3d75f747c..9d146e26be 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/CoreModuleHandlerFactory.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/CoreModuleHandlerFactory.java @@ -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.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; @@ -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; @@ -208,6 +210,8 @@ public class CoreModuleHandlerFactory extends BaseModuleHandlerFactory implement 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)) { diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/ItemStateUpdateActionHandler.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/ItemStateUpdateActionHandler.java new file mode 100644 index 0000000000..76ef607a7c --- /dev/null +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/ItemStateUpdateActionHandler.java @@ -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 execute(Map 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; + } +} diff --git a/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/ItemActions.json b/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/ItemActions.json index 42523fa824..5dc3cbc4cf 100644 --- a/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/ItemActions.json +++ b/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/ItemActions.json @@ -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." + } + ] } ] }