Skip to content

Commit d132a38

Browse files
committed
Pass event source to profiles
So that a user can define a profile that takes different actions depending on what's generating the command. Signed-off-by: Cody Cutrer <cody@cutrer.us>
1 parent 478a7de commit d132a38

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/CommunicationManager.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@ private void receiveUpdate(ItemStateUpdatedEvent updateEvent) {
346346

347347
@FunctionalInterface
348348
private interface ProfileAction<T extends Type> {
349-
void applyProfile(Profile profile, Thing thing, T type);
349+
void applyProfile(Profile profile, Thing thing, T type, @Nullable String source);
350350
}
351351

352-
private void applyProfileForUpdate(Profile profile, Thing thing, State convertedState) {
352+
private void applyProfileForUpdate(Profile profile, Thing thing, State convertedState, @Nullable String source) {
353353
CacheKey key = new CacheKey("UPDATE", profile, thing);
354354
Profile p = profileSafeCallCache.computeIfAbsent(key, (k) -> safeCaller.create(k.profile, Profile.class) //
355355
.withAsync() //
@@ -363,7 +363,8 @@ private void applyProfileForUpdate(Profile profile, Thing thing, State converted
363363
}
364364
}
365365

366-
private void applyProfileForCommand(Profile profile, Thing thing, Command convertedCommand) {
366+
private void applyProfileForCommand(Profile profile, Thing thing, Command convertedCommand,
367+
@Nullable String source) {
367368
if (profile instanceof StateProfile) {
368369
CacheKey key = new CacheKey("COMMAND", profile, thing);
369370
Profile p = profileSafeCallCache.computeIfAbsent(key,
@@ -373,7 +374,7 @@ private void applyProfileForCommand(Profile profile, Thing thing, Command conver
373374
.withTimeout(THINGHANDLER_EVENT_TIMEOUT) //
374375
.build());
375376
if (p instanceof StateProfile profileP) {
376-
profileP.onCommandFromItem(convertedCommand);
377+
profileP.onCommandFromItem(convertedCommand, source);
377378
} else {
378379
throw new IllegalStateException("ExpiringCache didn't provide a StateProfile instance!");
379380
}
@@ -404,7 +405,7 @@ private <T extends Type> void handleEvent(String itemName, T type, @Nullable Str
404405
@Nullable
405406
T uomType = fixUoM(type, channel, item);
406407
Profile profile = getProfile(link, item, thing);
407-
action.applyProfile(profile, thing, uomType != null ? uomType : type);
408+
action.applyProfile(profile, thing, uomType != null ? uomType : type, source);
408409
}
409410
} else {
410411
logger.debug("Received event '{}' for non-existing channel '{}', not forwarding it to the handler",

bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/profiles/StateProfile.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.openhab.core.thing.profiles;
1414

1515
import org.eclipse.jdt.annotation.NonNullByDefault;
16+
import org.eclipse.jdt.annotation.Nullable;
1617
import org.openhab.core.types.Command;
1718
import org.openhab.core.types.State;
1819

@@ -31,6 +32,16 @@ public interface StateProfile extends Profile {
3132
*/
3233
void onCommandFromItem(Command command);
3334

35+
/**
36+
* Will be called if a command should be forwarded to the binding.
37+
*
38+
* @param command
39+
* @param source the source of the command event
40+
*/
41+
default void onCommandFromItem(Command command, @Nullable String source) {
42+
onCommandFromItem(command);
43+
}
44+
3445
/**
3546
* If a binding issued a command to a channel, this method will be called for each linked item.
3647
*

itests/org.openhab.core.thing.tests/src/main/java/org/openhab/core/thing/internal/CommunicationManagerOSGiTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ public void testTimeSeriesMultiLink() {
301301

302302
@Test
303303
public void testItemCommandEventSingleLink() {
304-
manager.receive(ItemEventFactory.createCommandEvent(ITEM_NAME_2, OnOffType.ON));
304+
manager.receive(ItemEventFactory.createCommandEvent(ITEM_NAME_2, OnOffType.ON, "mysource"));
305305
waitForAssert(() -> {
306-
verify(stateProfileMock).onCommandFromItem(eq(OnOffType.ON));
306+
verify(stateProfileMock).onCommandFromItem(eq(OnOffType.ON), eq("mysource"));
307307
});
308308
verifyNoMoreInteractions(stateProfileMock);
309309
verifyNoMoreInteractions(triggerProfileMock);

0 commit comments

Comments
 (0)