From 11686093dbecbf846786c58c66b39fc4f6e37553 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Tue, 23 Jul 2024 15:22:38 -0600 Subject: [PATCH] [homekit] Make GarageDoorOpener.ObstructionStatus optional To simplify linking to "simple" things that don't have it. It will just always return "not obstructed" if not linked Signed-off-by: Cody Cutrer --- bundles/org.openhab.io.homekit/README.md | 2 +- .../accessories/HomekitAccessoryFactory.java | 3 +- .../HomekitCharacteristicFactory.java | 25 ++++++ .../HomekitGarageDoorOpenerImpl.java | 78 +++---------------- 4 files changed, 36 insertions(+), 72 deletions(-) diff --git a/bundles/org.openhab.io.homekit/README.md b/bundles/org.openhab.io.homekit/README.md index 622f51025b883..9c5082d2fdfaf 100644 --- a/bundles/org.openhab.io.homekit/README.md +++ b/bundles/org.openhab.io.homekit/README.md @@ -796,7 +796,7 @@ All accessories also support the following optional characteristic that can be l | | | FilterResetIndication | Switch | Send "filter reset" action triggered by user in iOS Home app to openHAB ("ON" = reset requested by user). | | | | GarageDoorOpener | | | | A garage door opener. | | | | | CurrentDoorState | | Contact, Switch, Number, String | Current door state. | inverted (false) | OPEN (0, ON, OPEN), CLOSED (1, OFF, CLOSED), OPENING (2), CLOSING (3), STOPPED (4) | -| | ObstructionStatus | | Contact, Switch | Current status of obstruction sensor. ON-obstruction detected, OFF - no obstruction | inverted (false) | | +| | | ObstructionStatus | Contact, Switch | Current status of obstruction sensor. ON-obstruction detected, OFF - no obstruction | inverted (false) | | | | TargetDoorState | | Contact, Switch, Number, String | Target door state. | inverted (false) | OPEN (0, ON, OPEN), CLOSED (1, OFF, CLOSED) | | HeaterCooler | | | | Heater or/and cooler device | | | | | ActiveStatus | | Dimmer, Switch | Accessory current working status. A value of "ON"/"OPEN" indicates that the accessory is active and is functioning without any errors. | | | diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java index 4896f8e017df8..f019eeb821e85 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java @@ -79,8 +79,7 @@ public class HomekitAccessoryFactory { put(FAN, new HomekitCharacteristicType[] { ACTIVE_STATUS }); put(FAUCET, new HomekitCharacteristicType[] { ACTIVE_STATUS }); put(FILTER_MAINTENANCE, new HomekitCharacteristicType[] { FILTER_CHANGE_INDICATION }); - put(GARAGE_DOOR_OPENER, - new HomekitCharacteristicType[] { CURRENT_DOOR_STATE, TARGET_DOOR_STATE, OBSTRUCTION_STATUS }); + put(GARAGE_DOOR_OPENER, new HomekitCharacteristicType[] { CURRENT_DOOR_STATE, TARGET_DOOR_STATE }); put(HEATER_COOLER, new HomekitCharacteristicType[] { ACTIVE_STATUS, CURRENT_HEATER_COOLER_STATE, TARGET_HEATER_COOLER_STATE, CURRENT_TEMPERATURE }); put(HUMIDITY_SENSOR, new HomekitCharacteristicType[] { RELATIVE_HUMIDITY }); 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 efd9be7e569fa..3d52d23c9dcd4 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 @@ -119,6 +119,10 @@ import io.github.hapjava.characteristics.impl.fan.TargetFanStateEnum; import io.github.hapjava.characteristics.impl.filtermaintenance.FilterLifeLevelCharacteristic; import io.github.hapjava.characteristics.impl.filtermaintenance.ResetFilterIndicationCharacteristic; +import io.github.hapjava.characteristics.impl.garagedoor.CurrentDoorStateCharacteristic; +import io.github.hapjava.characteristics.impl.garagedoor.CurrentDoorStateEnum; +import io.github.hapjava.characteristics.impl.garagedoor.TargetDoorStateCharacteristic; +import io.github.hapjava.characteristics.impl.garagedoor.TargetDoorStateEnum; import io.github.hapjava.characteristics.impl.humiditysensor.CurrentRelativeHumidityCharacteristic; import io.github.hapjava.characteristics.impl.humiditysensor.TargetRelativeHumidityCharacteristic; import io.github.hapjava.characteristics.impl.inputsource.CurrentVisibilityStateCharacteristic; @@ -197,6 +201,7 @@ public class HomekitCharacteristicFactory { put(CONFIGURED, HomekitCharacteristicFactory::createIsConfiguredCharacteristic); put(CONFIGURED_NAME, HomekitCharacteristicFactory::createConfiguredNameCharacteristic); put(COOLING_THRESHOLD_TEMPERATURE, HomekitCharacteristicFactory::createCoolingThresholdCharacteristic); + put(CURRENT_DOOR_STATE, HomekitCharacteristicFactory::createCurrentDoorStateCharacteristic); put(CURRENT_HEATING_COOLING_STATE, HomekitCharacteristicFactory::createCurrentHeatingCoolingStateCharacteristic); put(CURRENT_FAN_STATE, HomekitCharacteristicFactory::createCurrentFanStateCharacteristic); @@ -245,6 +250,7 @@ public class HomekitCharacteristicFactory { put(SULPHUR_DIOXIDE_DENSITY, HomekitCharacteristicFactory::createSulphurDioxideDensityCharacteristic); put(SWING_MODE, HomekitCharacteristicFactory::createSwingModeCharacteristic); put(TAMPERED_STATUS, HomekitCharacteristicFactory::createStatusTamperedCharacteristic); + put(TARGET_DOOR_STATE, HomekitCharacteristicFactory::createTargetDoorStateCharacteristic); put(TARGET_FAN_STATE, HomekitCharacteristicFactory::createTargetFanStateCharacteristic); put(TARGET_HEATING_COOLING_STATE, HomekitCharacteristicFactory::createTargetHeatingCoolingStateCharacteristic); @@ -821,6 +827,15 @@ private static CoolingThresholdTemperatureCharacteristic createCoolingThresholdC getUnsubscriber(taggedItem, COOLING_THRESHOLD_TEMPERATURE, updater)); } + private static CurrentDoorStateCharacteristic createCurrentDoorStateCharacteristic(HomekitTaggedItem taggedItem, + HomekitAccessoryUpdater updater) { + List validValues = new ArrayList<>(); + var map = createMapping(taggedItem, CurrentDoorStateEnum.class, validValues); + return new CurrentDoorStateCharacteristic(() -> getEnumFromItem(taggedItem, map, CurrentDoorStateEnum.CLOSED), + getSubscriber(taggedItem, CURRENT_DOOR_STATE, updater), + getUnsubscriber(taggedItem, CURRENT_DOOR_STATE, updater)); + } + private static CurrentHeatingCoolingStateCharacteristic createCurrentHeatingCoolingStateCharacteristic( HomekitTaggedItem taggedItem, HomekitAccessoryUpdater updater) { List validValues = new ArrayList<>(); @@ -1366,6 +1381,16 @@ private static SwingModeCharacteristic createSwingModeCharacteristic(HomekitTagg getUnsubscriber(taggedItem, SWING_MODE, updater)); } + private static TargetDoorStateCharacteristic createTargetDoorStateCharacteristic(HomekitTaggedItem taggedItem, + HomekitAccessoryUpdater updater) { + List validValues = new ArrayList<>(); + var map = createMapping(taggedItem, TargetDoorStateEnum.class, validValues); + return new TargetDoorStateCharacteristic(() -> getEnumFromItem(taggedItem, map, TargetDoorStateEnum.CLOSED), + (targetState) -> setValueFromEnum(taggedItem, targetState, map), + getSubscriber(taggedItem, TARGET_DOOR_STATE, updater), + getUnsubscriber(taggedItem, TARGET_DOOR_STATE, updater)); + } + private static TargetFanStateCharacteristic createTargetFanStateCharacteristic(HomekitTaggedItem taggedItem, HomekitAccessoryUpdater updater) { var map = createMapping(taggedItem, TargetFanStateEnum.class); diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitGarageDoorOpenerImpl.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitGarageDoorOpenerImpl.java index 8a43da9a8daeb..ec5c1395721bb 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitGarageDoorOpenerImpl.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitGarageDoorOpenerImpl.java @@ -12,12 +12,7 @@ */ package org.openhab.io.homekit.internal.accessories; -import static org.openhab.io.homekit.internal.HomekitCharacteristicType.CURRENT_DOOR_STATE; -import static org.openhab.io.homekit.internal.HomekitCharacteristicType.OBSTRUCTION_STATUS; -import static org.openhab.io.homekit.internal.HomekitCharacteristicType.TARGET_DOOR_STATE; - import java.util.List; -import java.util.Map; import java.util.concurrent.CompletableFuture; import org.openhab.io.homekit.internal.HomekitAccessoryUpdater; @@ -27,11 +22,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import io.github.hapjava.accessories.GarageDoorOpenerAccessory; import io.github.hapjava.characteristics.Characteristic; -import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback; -import io.github.hapjava.characteristics.impl.garagedoor.CurrentDoorStateEnum; -import io.github.hapjava.characteristics.impl.garagedoor.TargetDoorStateEnum; +import io.github.hapjava.characteristics.impl.common.ObstructionDetectedCharacteristic; +import io.github.hapjava.characteristics.impl.garagedoor.CurrentDoorStateCharacteristic; +import io.github.hapjava.characteristics.impl.garagedoor.TargetDoorStateCharacteristic; import io.github.hapjava.services.impl.GarageDoorOpenerService; /** @@ -39,78 +33,24 @@ * * @author Eugen Freiter - Initial contribution */ -public class HomekitGarageDoorOpenerImpl extends AbstractHomekitAccessoryImpl implements GarageDoorOpenerAccessory { +public class HomekitGarageDoorOpenerImpl extends AbstractHomekitAccessoryImpl { private final Logger logger = LoggerFactory.getLogger(HomekitGarageDoorOpenerImpl.class); - private final BooleanItemReader obstructionReader; - private final Map currentDoorStateMapping; - private final Map targetDoorStateMapping; public HomekitGarageDoorOpenerImpl(HomekitTaggedItem taggedItem, List mandatoryCharacteristics, List mandatoryRawCharacteristics, HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException { super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings); - obstructionReader = createBooleanReader(OBSTRUCTION_STATUS); - currentDoorStateMapping = createMapping(CURRENT_DOOR_STATE, CurrentDoorStateEnum.class, true); - targetDoorStateMapping = createMapping(TARGET_DOOR_STATE, TargetDoorStateEnum.class, true); } @Override public void init() throws HomekitException { super.init(); - addService(new GarageDoorOpenerService(this)); - } - - @Override - public CompletableFuture getCurrentDoorState() { - return CompletableFuture.completedFuture( - getKeyFromMapping(CURRENT_DOOR_STATE, currentDoorStateMapping, CurrentDoorStateEnum.CLOSED)); - } - @Override - public CompletableFuture getTargetDoorState() { - return CompletableFuture.completedFuture( - getKeyFromMapping(TARGET_DOOR_STATE, targetDoorStateMapping, TargetDoorStateEnum.CLOSED)); - } + var obstructionDetectedCharacteristic = getCharacteristic(ObstructionDetectedCharacteristic.class) + .orElseGet(() -> new ObstructionDetectedCharacteristic(() -> CompletableFuture.completedFuture(false), + null, null)); - @Override - public CompletableFuture getObstructionDetected() { - return CompletableFuture.completedFuture(obstructionReader.getValue()); - } - - @Override - public CompletableFuture setTargetDoorState(TargetDoorStateEnum targetDoorStateEnum) { - HomekitCharacteristicFactory.setValueFromEnum(getCharacteristic(TARGET_DOOR_STATE).get(), targetDoorStateEnum, - targetDoorStateMapping); - return CompletableFuture.completedFuture(null); - } - - @Override - public void subscribeCurrentDoorState(HomekitCharacteristicChangeCallback callback) { - subscribe(CURRENT_DOOR_STATE, callback); - } - - @Override - public void subscribeTargetDoorState(HomekitCharacteristicChangeCallback callback) { - subscribe(TARGET_DOOR_STATE, callback); - } - - @Override - public void subscribeObstructionDetected(HomekitCharacteristicChangeCallback callback) { - subscribe(OBSTRUCTION_STATUS, callback); - } - - @Override - public void unsubscribeCurrentDoorState() { - unsubscribe(CURRENT_DOOR_STATE); - } - - @Override - public void unsubscribeTargetDoorState() { - unsubscribe(TARGET_DOOR_STATE); - } - - @Override - public void unsubscribeObstructionDetected() { - unsubscribe(OBSTRUCTION_STATUS); + addService(new GarageDoorOpenerService(getCharacteristic(CurrentDoorStateCharacteristic.class).get(), + getCharacteristic(TargetDoorStateCharacteristic.class).get(), obstructionDetectedCharacteristic)); } }