Skip to content

Commit

Permalink
Fix wrong event subscription in GroupStateTrigger and ItemStateTrigger (
Browse files Browse the repository at this point in the history
#3533)

The `GroupStateTrigger` subscibed to `ItemStateEvent` instead of `ItemStateUpdatedEvent` (while `ItemStateTrigger` uses `ItemStateUpdatedEvent`), this is confusiing and results in different behavior of both triggers.

The `ItemStateTrigger` did not subscribe to `GroupStateUpdatedEvent` which results in the trigger not firing for group state UPDATES. This is wrong because it fires for item UPDATES and for both, group and item CHANGES.

Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K authored Apr 15, 2023
1 parent 016828c commit 9bbe456
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.openhab.core.items.events.ItemAddedEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.items.events.ItemStateChangedEvent;
import org.openhab.core.items.events.ItemStateEvent;
import org.openhab.core.items.events.ItemStateUpdatedEvent;
import org.openhab.core.types.State;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
Expand Down Expand Up @@ -72,7 +72,7 @@ public GroupStateTriggerHandler(Trigger module, String ruleUID, BundleContext bu
this.state = (String) module.getConfiguration().get(CFG_STATE);
this.previousState = (String) module.getConfiguration().get(CFG_PREVIOUS_STATE);
if (UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
this.types = Set.of(ItemStateEvent.TYPE, ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
this.types = Set.of(ItemStateUpdatedEvent.TYPE, ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
} else {
this.types = Set.of(ItemStateChangedEvent.TYPE, GroupItemStateChangedEvent.TYPE, ItemAddedEvent.TYPE,
ItemRemovedEvent.TYPE);
Expand Down Expand Up @@ -113,8 +113,8 @@ public void receive(Event event) {
TriggerHandlerCallback cb = (TriggerHandlerCallback) callback;
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(),
event.getTopic(), event.getType(), event.getPayload());
if (event instanceof ItemStateEvent && UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
ItemStateEvent isEvent = (ItemStateEvent) event;
if (event instanceof ItemStateUpdatedEvent && UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
ItemStateUpdatedEvent isEvent = (ItemStateUpdatedEvent) event;
String itemName = isEvent.getItemName();
Item item = itemRegistry.get(itemName);
if (item != null && item.getGroupNames().contains(groupName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openhab.core.events.TopicPrefixEventFilter;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.GroupItemStateChangedEvent;
import org.openhab.core.items.events.GroupStateUpdatedEvent;
import org.openhab.core.items.events.ItemAddedEvent;
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.items.events.ItemStateChangedEvent;
Expand Down Expand Up @@ -77,7 +78,8 @@ public ItemStateTriggerHandler(Trigger module, String ruleUID, BundleContext bun
this.previousState = (String) module.getConfiguration().get(CFG_PREVIOUS_STATE);
this.ruleUID = ruleUID;
if (UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
this.types = Set.of(ItemStateUpdatedEvent.TYPE, ItemAddedEvent.TYPE, ItemRemovedEvent.TYPE);
this.types = Set.of(ItemStateUpdatedEvent.TYPE, GroupStateUpdatedEvent.TYPE, ItemAddedEvent.TYPE,
ItemRemovedEvent.TYPE);
} else {
this.types = Set.of(ItemStateChangedEvent.TYPE, GroupItemStateChangedEvent.TYPE, ItemAddedEvent.TYPE,
ItemRemovedEvent.TYPE);
Expand Down

0 comments on commit 9bbe456

Please sign in to comment.