From 9f0ec10a7d17157db4fe25eebcb42b99850c065b Mon Sep 17 00:00:00 2001 From: eugen Date: Sat, 6 Feb 2021 17:59:14 +0100 Subject: [PATCH] [homekit] add support for dimmer item for slat accessories (#10019) Signed-off-by: Eugen Freiter --- bundles/org.openhab.io.homekit/README.md | 8 ++-- .../HomekitCharacteristicFactory.java | 38 +++++++++++++++++-- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.io.homekit/README.md b/bundles/org.openhab.io.homekit/README.md index cf53c6aed..e6b969ed3 100644 --- a/bundles/org.openhab.io.homekit/README.md +++ b/bundles/org.openhab.io.homekit/README.md @@ -562,10 +562,10 @@ Switch motionsensor_tampered "Motion Sensor Tampered" | | | Name | String | Name of the windows covering | | | | HoldPosition | Switch | Window covering should stop at its current position. A value of ON must hold the state of the accessory. A value of OFF should be ignored. | | | | ObstructionStatus | Switch, Contact | Current status of obstruction sensor. ON-obstruction detected, OFF - no obstruction | -| | | CurrentHorizontalTiltAngle | Number | current angle of horizontal slats for accessories windows. values -90 to 90. A value of 0 indicates that the slats are rotated to a fully open position. A value of -90 indicates that the slats are rotated all the way in a direction where the user-facing edge is higher than the window-facing edge. | -| | | TargetHorizontalTiltAngle | Number | target angle of horizontal slats | -| | | CurrentVerticalTiltAngle | Number | current angle of vertical slats | -| | | TargetVerticalTiltAngle | Number | target angle of vertical slats | +| | | CurrentHorizontalTiltAngle | Number, Dimmer | Number Item = current angle of horizontal slats. values -90 to 90. A value of 0 indicates that the slats are rotated to a fully open position. A value of -90 indicates that the slats are rotated all the way in a direction where the user-facing edge is higher than the window-facing edge. Dimmer Item = the percentage of openness (0%-100%) | +| | | TargetHorizontalTiltAngle | Number, Dimmer | Number Item = target angle of horizontal slats (-90 to +90). Dimmer Item = the percentage of openness (0%-100%) | +| | | CurrentVerticalTiltAngle | Number, Dimmer | Number Item = current angle of vertical slats (-90 to +90) . Dimmer Item = the percentage of openness (0%-100%) | +| | | TargetVerticalTiltAngle | Number, Dimmer | Number Item = target angle of vertical slats. Dimmer Item = the percentage of openness (0%-100%) | | Switchable | | | | An accessory that can be turned off and on. While similar to a lightbulb, this will be presented differently in the Siri grammar and iOS apps | | | OnState | | Switch | State of the switch - ON/OFF | | | | Name | String | Name of the switch | diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java index 54ba68112..85eec94e1 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java @@ -214,6 +214,22 @@ public class HomekitCharacteristicFactory { return value; } + /** special method for tilts. it converts percentage to angle */ + private static int getAngleFromItem(HomekitTaggedItem taggedItem) { + int value = 0; + final State state = taggedItem.getItem().getState(); + if (state instanceof PercentType) { + value = (int) ((((PercentType) state).intValue() * 90.0) / 50.0 - 90.0); + } else { + value = getIntFromItem(taggedItem); + } + return value; + } + + private static Supplier> getAngleSupplier(HomekitTaggedItem taggedItem) { + return () -> CompletableFuture.completedFuture(getAngleFromItem(taggedItem)); + } + private static Supplier> getIntSupplier(HomekitTaggedItem taggedItem) { return () -> CompletableFuture.completedFuture(getIntFromItem(taggedItem)); } @@ -242,6 +258,20 @@ public class HomekitCharacteristicFactory { }; } + private static ExceptionalConsumer setAngleConsumer(HomekitTaggedItem taggedItem) { + return (value) -> { + if (taggedItem.getItem() instanceof NumberItem) { + ((NumberItem) taggedItem.getItem()).send(new DecimalType(value)); + } else if (taggedItem.getItem() instanceof DimmerItem) { + value = (int) (value * 50.0 / 90.0 + 50.0); + ((DimmerItem) taggedItem.getItem()).send(new PercentType(value)); + } else { + logger.warn("Item type {} is not supported for {}. Only DimmerItem and NumberItem are supported.", + taggedItem.getItem().getType(), taggedItem.getName()); + } + }; + } + private static Supplier> getDoubleSupplier(HomekitTaggedItem taggedItem) { return () -> { final @Nullable DecimalType value = taggedItem.getItem().getStateAs(DecimalType.class); @@ -357,28 +387,28 @@ public class HomekitCharacteristicFactory { private static CurrentHorizontalTiltAngleCharacteristic createCurrentHorizontalTiltAngleCharacteristic( HomekitTaggedItem taggedItem, HomekitAccessoryUpdater updater) { - return new CurrentHorizontalTiltAngleCharacteristic(getIntSupplier(taggedItem), + return new CurrentHorizontalTiltAngleCharacteristic(getAngleSupplier(taggedItem), getSubscriber(taggedItem, CURRENT_HORIZONTAL_TILT_ANGLE, updater), getUnsubscriber(taggedItem, CURRENT_HORIZONTAL_TILT_ANGLE, updater)); } private static CurrentVerticalTiltAngleCharacteristic createCurrentVerticalTiltAngleCharacteristic( HomekitTaggedItem taggedItem, HomekitAccessoryUpdater updater) { - return new CurrentVerticalTiltAngleCharacteristic(getIntSupplier(taggedItem), + return new CurrentVerticalTiltAngleCharacteristic(getAngleSupplier(taggedItem), getSubscriber(taggedItem, CURRENT_VERTICAL_TILT_ANGLE, updater), getUnsubscriber(taggedItem, CURRENT_VERTICAL_TILT_ANGLE, updater)); } private static TargetHorizontalTiltAngleCharacteristic createTargetHorizontalTiltAngleCharacteristic( HomekitTaggedItem taggedItem, HomekitAccessoryUpdater updater) { - return new TargetHorizontalTiltAngleCharacteristic(getIntSupplier(taggedItem), setIntConsumer(taggedItem), + return new TargetHorizontalTiltAngleCharacteristic(getAngleSupplier(taggedItem), setAngleConsumer(taggedItem), getSubscriber(taggedItem, TARGET_HORIZONTAL_TILT_ANGLE, updater), getUnsubscriber(taggedItem, TARGET_HORIZONTAL_TILT_ANGLE, updater)); } private static TargetVerticalTiltAngleCharacteristic createTargetVerticalTiltAngleCharacteristic( HomekitTaggedItem taggedItem, HomekitAccessoryUpdater updater) { - return new TargetVerticalTiltAngleCharacteristic(getIntSupplier(taggedItem), setIntConsumer(taggedItem), + return new TargetVerticalTiltAngleCharacteristic(getAngleSupplier(taggedItem), setAngleConsumer(taggedItem), getSubscriber(taggedItem, TARGET_HORIZONTAL_TILT_ANGLE, updater), getUnsubscriber(taggedItem, TARGET_HORIZONTAL_TILT_ANGLE, updater)); }