From 4cd2b8bae99429f219ba7e5c517a8766ccc93618 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Tue, 23 Sep 2025 10:37:33 -0600 Subject: [PATCH] [homekit] Send a source with all commands to items Signed-off-by: Cody Cutrer --- .../homekit/internal/HomekitOHItemProxy.java | 13 ++++---- .../homekit/internal/HomekitTaggedItem.java | 33 ++++++++++--------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitOHItemProxy.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitOHItemProxy.java index 131aa27af739b..1e2fcda92555b 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitOHItemProxy.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitOHItemProxy.java @@ -14,6 +14,7 @@ import static org.openhab.io.homekit.internal.HomekitCommandType.*; import static org.openhab.io.homekit.internal.HomekitDimmerMode.*; +import static org.openhab.io.homekit.internal.HomekitTaggedItem.HOMEKIT_SOURCE; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -110,9 +111,9 @@ private void sendCommand() { && ((brightness == null) || (brightness.intValue() == 100)))) { logger.trace("send OnOff command for item {} with value {}", item, on); if (item instanceof GroupItem groupItem) { - groupItem.send(on); + groupItem.send(on, HOMEKIT_SOURCE); } else { - ((DimmerItem) item).send(on); + ((DimmerItem) item).send(on, HOMEKIT_SOURCE); } } } @@ -134,9 +135,9 @@ private void sendCommand() { if (item instanceof ColorItem colorItem) { sendHSBCommand(colorItem, hue, saturation, brightness); } else if (item instanceof GroupItem groupItem) { - groupItem.send(brightness); + groupItem.send(brightness, HOMEKIT_SOURCE); } else { - ((DimmerItem) item).send(brightness); + ((DimmerItem) item).send(brightness, HOMEKIT_SOURCE); } } } @@ -152,9 +153,9 @@ private void sendHSBCommand(Item item, @Nullable DecimalType hue, @Nullable Perc final PercentType targetBrightness = brightness != null ? brightness : currentState.getBrightness(); final HSBType command = new HSBType(targetHue, targetSaturation, targetBrightness); if (item instanceof GroupItem groupItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); } else { - ((ColorItem) item).send(command); + ((ColorItem) item).send(command, HOMEKIT_SOURCE); } logger.trace("send HSB command for item {} with following values hue={} saturation={} brightness={}", item, targetHue, targetSaturation, targetBrightness); diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitTaggedItem.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitTaggedItem.java index e8d622390f60e..629dc0e6e8030 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitTaggedItem.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitTaggedItem.java @@ -66,6 +66,9 @@ public class HomekitTaggedItem { public static final String EMULATE_STOP_SAME_DIRECTION = "stopSameDirection"; public static final String SEND_UP_DOWN_FOR_EXTENTS = "sendUpDownForExtents"; + // The "source" sent with command events so rules can identify them as coming from HomeKit + public static final String HOMEKIT_SOURCE = "org.openhab.io.homekit"; + private static final Map CREATED_ACCESSORY_IDS = new ConcurrentHashMap<>(); // proxy item used to group commands for complex item types like Color or Dimmer @@ -181,10 +184,10 @@ public HomekitOHItemProxy getProxyItem() { */ public void send(DecimalType command) { if (getItem() instanceof GroupItem groupItem && getBaseItem() instanceof NumberItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof NumberItem numberItem) { - numberItem.send(command); + numberItem.send(command, HOMEKIT_SOURCE); return; } logger.warn("Received DecimalType command for item {} that doesn't support it. This is probably a bug.", @@ -198,10 +201,10 @@ public void send(DecimalType command) { */ public void send(QuantityType command) { if (getItem() instanceof GroupItem groupItem && getBaseItem() instanceof NumberItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof NumberItem numberItem) { - numberItem.send(command); + numberItem.send(command, HOMEKIT_SOURCE); return; } logger.warn("Received QuantityType command for item {} that doesn't support it. This is probably a bug.", @@ -215,10 +218,10 @@ public void send(QuantityType command) { */ public void send(OnOffType command) { if (getItem() instanceof GroupItem groupItem && getBaseItem() instanceof SwitchItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof SwitchItem switchItem) { - switchItem.send(command); + switchItem.send(command, HOMEKIT_SOURCE); return; } logger.warn("Received OnOffType command for item {} that doesn't support it. This is probably a bug.", @@ -230,10 +233,10 @@ public void send(OnOffType command) { */ public void send(IncreaseDecreaseType command) { if (getItem() instanceof GroupItem groupItem && getBaseItem() instanceof DimmerItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof DimmerItem dimmerItem) { - dimmerItem.send(command); + dimmerItem.send(command, HOMEKIT_SOURCE); return; } logger.warn( @@ -249,13 +252,13 @@ public void send(IncreaseDecreaseType command) { public void send(PercentType command) { if (getItem() instanceof GroupItem groupItem && (getBaseItem() instanceof DimmerItem || getBaseItem() instanceof RollershutterItem)) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof DimmerItem dimmerItem) { - dimmerItem.send(command); + dimmerItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof RollershutterItem rollerShutterItem) { - rollerShutterItem.send(command); + rollerShutterItem.send(command, HOMEKIT_SOURCE); return; } logger.warn("Received PercentType command for item {} that doesn't support it. This is probably a bug.", @@ -269,10 +272,10 @@ public void send(PercentType command) { */ public void send(StringType command) { if (getItem() instanceof GroupItem groupItem && getBaseItem() instanceof StringItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof StringItem stringItem) { - stringItem.send(command); + stringItem.send(command, HOMEKIT_SOURCE); return; } logger.warn("Received StringType command for item {} that doesn't support it. This is probably a bug.", @@ -284,10 +287,10 @@ public void send(StringType command) { */ public void send(UpDownType command) { if (getItem() instanceof GroupItem groupItem && getBaseItem() instanceof RollershutterItem) { - groupItem.send(command); + groupItem.send(command, HOMEKIT_SOURCE); return; } else if (getItem() instanceof RollershutterItem rollershutterItem) { - rollershutterItem.send(command); + rollershutterItem.send(command, HOMEKIT_SOURCE); return; } logger.warn("Received UpDownType command for item {} that doesn't support it. This is probably a bug.",