]> git.basschouten.com Git - openhab-addons.git/commitdiff
[homekit] Make GarageDoorOpener.ObstructionStatus optional (#17137)
authorCody Cutrer <cody@cutrer.us>
Wed, 24 Jul 2024 07:02:58 +0000 (01:02 -0600)
committerGitHub <noreply@github.com>
Wed, 24 Jul 2024 07:02:58 +0000 (09:02 +0200)
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 <cody@cutrer.us>
bundles/org.openhab.io.homekit/README.md
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitAccessoryFactory.java
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitGarageDoorOpenerImpl.java

index 622f51025b883b4de924f047fe983b44445e0937..610da1a0f8501498257e11be79480f41408e17d5 100644 (file)
@@ -796,8 +796,8 @@ 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)                                                      |                                                                                                             |
 |                             | TargetDoorState             |                             | Contact, Switch, Number, String         | Target door state.                                                                                                                                                                                                                                                                                                                                            | inverted (false)                                                      | OPEN (0, ON, OPEN), CLOSED (1, OFF, CLOSED)                                                                 |
+|                             |                             | ObstructionStatus           | Contact, Switch                         | Current status of obstruction sensor. ON-obstruction detected, OFF - no obstruction                                                                                                                                                                                                                                                                           | inverted (false)                                                      |                                                                                                             |
 | 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.                                                                                                                                                                                                                        |                                                                       |                                                                                                             |
 |                             | CurrentHeaterCoolerState    |                             | Number, String                          | Current heater/cooler mode.                                                                                                                                                                                                                                                                                                                                   |                                                                       | INACTIVE (0), IDLE (1), HEATING (2), COOLING (3)                                                            |
index 4896f8e017df80d0725992e6d8d225b9f88db98d..f019eeb821e85cdef67707ab5e0a7cb5957f5fbb 100644 (file)
@@ -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 });
index efd9be7e569fa8e31c485667fd9bb6eded210cfa..3d52d23c9dcd444e14878fcf8a8e4c58686f2eab 100644 (file)
@@ -119,6 +119,10 @@ import io.github.hapjava.characteristics.impl.fan.TargetFanStateCharacteristic;
 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 @@ public class HomekitCharacteristicFactory {
                 getUnsubscriber(taggedItem, COOLING_THRESHOLD_TEMPERATURE, updater));
     }
 
+    private static CurrentDoorStateCharacteristic createCurrentDoorStateCharacteristic(HomekitTaggedItem taggedItem,
+            HomekitAccessoryUpdater updater) {
+        List<CurrentDoorStateEnum> 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<CurrentHeatingCoolingStateEnum> validValues = new ArrayList<>();
@@ -1366,6 +1381,16 @@ public class HomekitCharacteristicFactory {
                 getUnsubscriber(taggedItem, SWING_MODE, updater));
     }
 
+    private static TargetDoorStateCharacteristic createTargetDoorStateCharacteristic(HomekitTaggedItem taggedItem,
+            HomekitAccessoryUpdater updater) {
+        List<TargetDoorStateEnum> 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);
index 8a43da9a8daebd07b56a890791f9c496fbe352a8..f6c55f984cc6dcbe3db3987bb1a2b981cf0d104b 100644 (file)
  */
 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.openhab.io.homekit.internal.HomekitTaggedItem;
 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,25 @@ import io.github.hapjava.services.impl.GarageDoorOpenerService;
  *
  * @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<CurrentDoorStateEnum, String> currentDoorStateMapping;
-    private final Map<TargetDoorStateEnum, String> targetDoorStateMapping;
 
     public HomekitGarageDoorOpenerImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
             List<Characteristic> 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<CurrentDoorStateEnum> getCurrentDoorState() {
-        return CompletableFuture.completedFuture(
-                getKeyFromMapping(CURRENT_DOOR_STATE, currentDoorStateMapping, CurrentDoorStateEnum.CLOSED));
-    }
 
-    @Override
-    public CompletableFuture<TargetDoorStateEnum> getTargetDoorState() {
-        return CompletableFuture.completedFuture(
-                getKeyFromMapping(TARGET_DOOR_STATE, targetDoorStateMapping, TargetDoorStateEnum.CLOSED));
-    }
+        var obstructionDetectedCharacteristic = getCharacteristic(ObstructionDetectedCharacteristic.class).orElseGet(
+                () -> new ObstructionDetectedCharacteristic(() -> CompletableFuture.completedFuture(false), (cb) -> {
+                }, () -> {
+                }));
 
-    @Override
-    public CompletableFuture<Boolean> getObstructionDetected() {
-        return CompletableFuture.completedFuture(obstructionReader.getValue());
-    }
-
-    @Override
-    public CompletableFuture<Void> 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));
     }
 }