Skip to content

Commit

Permalink
Add group name to GroupStateTriggerHandler (#3536)
Browse files Browse the repository at this point in the history
* Add group name to GroupStateTriggerHandler

When triggering on state change/update of group member, the group is not available in the rule context (because the event is the `ItemStateChanged/UpdatedEvent` that caused the group to change/update. This adds a new element `triggeringGroup` to the rule context.

This is useful for generalized rules that trigger on different groups.

Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored May 27, 2023
1 parent ed392ee commit 7843b68
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ public void receive(Event event) {
if (event instanceof ItemCommandEvent icEvent) {
String itemName = icEvent.getItemName();
Item item = itemRegistry.get(itemName);
Item group = itemRegistry.get(groupName);
if (item != null && item.getGroupNames().contains(groupName)) {
String command = this.command;
Command itemCommand = icEvent.getItemCommand();
if (command == null || command.equals(itemCommand.toFullString())) {
if (group != null) {
values.put("triggeringGroup", group);
}
values.put("triggeringItem", item);
values.put("command", itemCommand);
values.put("event", event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ public void receive(Event event) {
if (event instanceof ItemStateUpdatedEvent isEvent && UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
String itemName = isEvent.getItemName();
Item item = itemRegistry.get(itemName);
Item group = itemRegistry.get(groupName);
if (item != null && item.getGroupNames().contains(groupName)) {
State state = isEvent.getItemState();
if ((this.state == null || state.toFullString().equals(this.state))) {
Map<String, Object> values = new HashMap<>();
if (group != null) {
values.put("triggeringGroup", group);
}
values.put("triggeringItem", item);
values.put("state", state);
values.put("event", event);
Expand All @@ -129,12 +133,16 @@ public void receive(Event event) {
&& CHANGE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
String itemName = iscEvent.getItemName();
Item item = itemRegistry.get(itemName);
Item group = itemRegistry.get(groupName);
if (item != null && item.getGroupNames().contains(groupName)) {
State state = iscEvent.getItemState();
State oldState = iscEvent.getOldItemState();

if (stateMatches(this.state, state) && stateMatches(this.previousState, oldState)) {
Map<String, Object> values = new HashMap<>();
if (group != null) {
values.put("triggeringGroup", group);
}
values.put("triggeringItem", item);
values.put("oldState", oldState);
values.put("newState", state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@
}
],
"outputs": [
{
"name": "triggeringGroup",
"type": "org.openhab.core.items.Item",
"description": "the group that the item belongs to",
"label": "Triggering Group",
"tags": [
"item"
]
},
{
"name": "triggeringItem",
"type": "org.openhab.core.items.Item",
Expand Down Expand Up @@ -366,6 +375,15 @@
}
],
"outputs": [
{
"name": "triggeringGroup",
"type": "org.openhab.core.items.Item",
"description": "the group that the item belongs to",
"label": "Triggering Group",
"tags": [
"item"
]
},
{
"name": "triggeringItem",
"type": "org.openhab.core.items.Item",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ module-type.core.GroupCommandTrigger.config.command.option.OPEN = OPEN
module-type.core.GroupCommandTrigger.config.command.option.CLOSED = CLOSED
module-type.core.GroupCommandTrigger.config.command.option.UP = UP
module-type.core.GroupCommandTrigger.config.command.option.DOWN = DOWN
module-type.core.GroupCommandTrigger.output.triggeringGroup.label = Triggering Group
module-type.core.GroupCommandTrigger.output.triggeringGroup.description = the group that the item belongs to
module-type.core.GroupCommandTrigger.output.triggeringItem.label = Triggering Item
module-type.core.GroupCommandTrigger.output.triggeringItem.description = the member of the group that received the command
module-type.core.GroupCommandTrigger.output.command.label = Command
Expand Down Expand Up @@ -118,6 +120,8 @@ module-type.core.GroupStateUpdateTrigger.config.state.option.OPEN = OPEN
module-type.core.GroupStateUpdateTrigger.config.state.option.CLOSED = CLOSED
module-type.core.GroupStateUpdateTrigger.config.state.option.UP = UP
module-type.core.GroupStateUpdateTrigger.config.state.option.DOWN = DOWN
module-type.core.GroupStateUpdateTrigger.output.triggeringGroup.label = Triggering Group
module-type.core.GroupStateUpdateTrigger.output.triggeringGroup.description = the group that the item belongs to
module-type.core.GroupStateUpdateTrigger.output.triggeringItem.label = Triggering Item
module-type.core.GroupStateUpdateTrigger.output.triggeringItem.description = the member of the group that updated its state
module-type.core.GroupStateUpdateTrigger.output.state.label = State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.xtext.xbase.interpreter.IEvaluationContext;
import org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext;
import org.openhab.core.automation.module.script.ScriptExtensionAccessor;
import org.openhab.core.items.Item;
import org.openhab.core.items.events.ItemEvent;
import org.openhab.core.model.script.engine.Script;
import org.openhab.core.model.script.engine.ScriptExecutionException;
Expand Down Expand Up @@ -63,7 +64,8 @@ public class DSLScriptEngine implements javax.script.ScriptEngine {
private static final Map<String, String> IMPLICIT_VARS = Map.of("command",
ScriptJvmModelInferrer.VAR_RECEIVED_COMMAND, "state", ScriptJvmModelInferrer.VAR_NEW_STATE, "newState",
ScriptJvmModelInferrer.VAR_NEW_STATE, "oldState", ScriptJvmModelInferrer.VAR_PREVIOUS_STATE,
"triggeringItem", ScriptJvmModelInferrer.VAR_TRIGGERING_ITEM, "input", ScriptJvmModelInferrer.VAR_INPUT);
"triggeringItem", ScriptJvmModelInferrer.VAR_TRIGGERING_ITEM, "triggeringGroup",
ScriptJvmModelInferrer.VAR_TRIGGERING_GROUP, "input", ScriptJvmModelInferrer.VAR_INPUT);

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

Expand Down Expand Up @@ -179,6 +181,11 @@ private DefaultEvaluationContext createEvaluationContext(Script script, IEvaluat
if (value instanceof ItemEvent event) {
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_ITEM_NAME),
event.getItemName());
Object group = context.getAttribute(ScriptJvmModelInferrer.VAR_TRIGGERING_GROUP);
if (group instanceof Item groupItem) {
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_GROUP_NAME),
groupItem.getName());
}
}
if (value instanceof ThingStatusInfoChangedEvent event) {
evalContext.newValue(QualifiedName.create(ScriptJvmModelInferrer.VAR_TRIGGERING_THING),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class ScriptJvmModelInferrer extends AbstractModelInferrer {
/** Variable name for the input string in a "script transformation" or "script profile" */
public static final String VAR_INPUT = "input";

/** Variable name for the group in a "member of state triggered" or "member of command triggered" rule */
public static final String VAR_TRIGGERING_GROUP = "triggeringGroup";

/** Variable name for the group in a "member of state triggered" or "member of command triggered" rule */
public static final String VAR_TRIGGERING_GROUP_NAME = "triggeringGroupName";

/** Variable name for the item in a "state triggered" or "command triggered" rule */
public static final String VAR_TRIGGERING_ITEM = "triggeringItem";

Expand Down Expand Up @@ -131,6 +137,10 @@ class ScriptJvmModelInferrer extends AbstractModelInferrer {
static = true
val inputTypeRef = script.newTypeRef(String)
parameters += script.toParameter(VAR_INPUT, inputTypeRef)
val groupTypeRef = script.newTypeRef(Item)
parameters += script.toParameter(VAR_TRIGGERING_GROUP, groupTypeRef)
val groupNameRef = script.newTypeRef(String)
parameters += script.toParameter(VAR_TRIGGERING_GROUP_NAME, groupNameRef)
val itemTypeRef = script.newTypeRef(Item)
parameters += script.toParameter(VAR_TRIGGERING_ITEM, itemTypeRef)
val itemNameRef = script.newTypeRef(String)
Expand Down

0 comments on commit 7843b68

Please sign in to comment.