]> git.basschouten.com Git - openhab-addons.git/blob
08ba0be13de798f8c00c604d132547456e7289b5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal.accessories;
14
15 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.CURRENT_POSITION;
16 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.POSITION_STATE;
17 import static org.openhab.io.homekit.internal.HomekitCharacteristicType.TARGET_POSITION;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.concurrent.CompletableFuture;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.items.GroupItem;
27 import org.openhab.core.items.Item;
28 import org.openhab.core.library.items.DimmerItem;
29 import org.openhab.core.library.items.NumberItem;
30 import org.openhab.core.library.items.RollershutterItem;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.PercentType;
33 import org.openhab.core.library.types.StopMoveType;
34 import org.openhab.core.library.types.UpDownType;
35 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
36 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
37 import org.openhab.io.homekit.internal.HomekitSettings;
38 import org.openhab.io.homekit.internal.HomekitTaggedItem;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import io.github.hapjava.characteristics.Characteristic;
43 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
44 import io.github.hapjava.characteristics.impl.windowcovering.PositionStateEnum;
45
46 /**
47  * Common methods for Door, Window and WindowCovering.
48  * 
49  * @author Eugen Freiter - Initial contribution
50  */
51 @NonNullByDefault
52 abstract class AbstractHomekitPositionAccessoryImpl extends AbstractHomekitAccessoryImpl {
53     private final Logger logger = LoggerFactory.getLogger(AbstractHomekitPositionAccessoryImpl.class);
54     protected int closedPosition;
55     protected int openPosition;
56     private final Map<PositionStateEnum, String> positionStateMapping;
57     protected boolean emulateState;
58     protected boolean emulateStopSameDirection;
59     protected PositionStateEnum emulatedState = PositionStateEnum.STOPPED;
60
61     public AbstractHomekitPositionAccessoryImpl(HomekitTaggedItem taggedItem,
62             List<HomekitTaggedItem> mandatoryCharacteristics, List<Characteristic> mandatoryRawCharacteristics,
63             HomekitAccessoryUpdater updater, HomekitSettings settings) {
64         super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
65         final boolean inverted = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.INVERTED, true);
66         emulateState = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.EMULATE_STOP_STATE, false);
67         emulateStopSameDirection = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.EMULATE_STOP_SAME_DIRECTION,
68                 false);
69         closedPosition = inverted ? 0 : 100;
70         openPosition = inverted ? 100 : 0;
71         positionStateMapping = createMapping(POSITION_STATE, PositionStateEnum.class);
72     }
73
74     public CompletableFuture<Integer> getCurrentPosition() {
75         return CompletableFuture.completedFuture(convertPositionState(CURRENT_POSITION, openPosition, closedPosition));
76     }
77
78     public CompletableFuture<PositionStateEnum> getPositionState() {
79         return CompletableFuture.completedFuture(emulateState ? emulatedState
80                 : getKeyFromMapping(POSITION_STATE, positionStateMapping, PositionStateEnum.STOPPED));
81     }
82
83     public CompletableFuture<Integer> getTargetPosition() {
84         return CompletableFuture.completedFuture(convertPositionState(TARGET_POSITION, openPosition, closedPosition));
85     }
86
87     public CompletableFuture<Void> setTargetPosition(int value) {
88         getCharacteristic(TARGET_POSITION).ifPresentOrElse(taggedItem -> {
89             final Item item = taggedItem.getItem();
90             final int targetPosition = convertPosition(value, openPosition);
91             if (item instanceof RollershutterItem itemAsRollerShutterItem) {
92                 // HomeKit home app never sends STOP. we emulate stop if we receive 100% or 0% while the blind is moving
93                 if (emulateState && (targetPosition == 100 && emulatedState == PositionStateEnum.DECREASING)
94                         || ((targetPosition == 0 && emulatedState == PositionStateEnum.INCREASING))) {
95                     if (emulateStopSameDirection) {
96                         // some blinds devices do not support "STOP" but would stop if receive UP/DOWN while moving
97                         itemAsRollerShutterItem
98                                 .send(emulatedState == PositionStateEnum.INCREASING ? UpDownType.UP : UpDownType.DOWN);
99                     } else {
100                         itemAsRollerShutterItem.send(StopMoveType.STOP);
101                     }
102                     emulatedState = PositionStateEnum.STOPPED;
103                 } else {
104                     itemAsRollerShutterItem.send(new PercentType(targetPosition));
105                     if (emulateState) {
106                         @Nullable
107                         PercentType currentPosition = item.getStateAs(PercentType.class);
108                         emulatedState = currentPosition == null || currentPosition.intValue() == targetPosition
109                                 ? PositionStateEnum.STOPPED
110                                 : currentPosition.intValue() < targetPosition ? PositionStateEnum.INCREASING
111                                         : PositionStateEnum.DECREASING;
112                     }
113                 }
114             } else if (item instanceof DimmerItem itemAsDimmerItem) {
115                 itemAsDimmerItem.send(new PercentType(targetPosition));
116             } else if (item instanceof NumberItem itemAsNumberItem) {
117                 itemAsNumberItem.send(new DecimalType(targetPosition));
118             } else if (item instanceof GroupItem itemAsGroupItem
119                     && itemAsGroupItem.getBaseItem() instanceof RollershutterItem) {
120                 itemAsGroupItem.send(new PercentType(targetPosition));
121             } else if (item instanceof GroupItem itemAsGroupItem
122                     && itemAsGroupItem.getBaseItem() instanceof DimmerItem) {
123                 itemAsGroupItem.send(new PercentType(targetPosition));
124             } else if (item instanceof GroupItem itemAsGroupItem
125                     && itemAsGroupItem.getBaseItem() instanceof NumberItem) {
126                 itemAsGroupItem.send(new DecimalType(targetPosition));
127             } else {
128                 logger.warn(
129                         "Unsupported item type for characteristic {} at accessory {}. Expected Rollershutter, Dimmer or Number item, got {}",
130                         TARGET_POSITION, getName(), item.getClass());
131             }
132         }, () -> {
133             logger.warn("Mandatory characteristic {} not found at accessory {}. ", TARGET_POSITION, getName());
134         });
135         return CompletableFuture.completedFuture(null);
136     }
137
138     public void subscribeCurrentPosition(HomekitCharacteristicChangeCallback callback) {
139         subscribe(CURRENT_POSITION, callback);
140     }
141
142     public void subscribePositionState(HomekitCharacteristicChangeCallback callback) {
143         subscribe(POSITION_STATE, callback);
144     }
145
146     public void subscribeTargetPosition(HomekitCharacteristicChangeCallback callback) {
147         subscribe(TARGET_POSITION, callback);
148     }
149
150     public void unsubscribeCurrentPosition() {
151         unsubscribe(CURRENT_POSITION);
152     }
153
154     public void unsubscribePositionState() {
155         unsubscribe(POSITION_STATE);
156     }
157
158     public void unsubscribeTargetPosition() {
159         unsubscribe(TARGET_POSITION);
160     }
161
162     /**
163      * convert/invert position of door/window/blinds.
164      * openHAB Rollershutter is:
165      * - completely open if position is 0%,
166      * - completely closed if position is 100%.
167      * HomeKit mapping has inverted mapping
168      * From Specification: "For blinds/shades/awnings, a value of 0 indicates a position that permits the least light
169      * and a value
170      * of 100 indicates a position that allows most light.", i.e.
171      * HomeKit Blinds is
172      * - completely open if position is 100%,
173      * - completely closed if position is 0%.
174      *
175      * As openHAB rollershutter item is typically used for window covering, the binding has by default inverting
176      * mapping.
177      * One can override this default behaviour with inverted="false/no" flag. in this cases, openHAB item value will be
178      * sent to HomeKit with no changes.
179      *
180      * @param value source value
181      * @return target value
182      */
183     protected int convertPosition(int value, int openPosition) {
184         return Math.abs(openPosition - value);
185     }
186
187     protected int convertPositionState(HomekitCharacteristicType type, int openPosition, int closedPosition) {
188         @Nullable
189         DecimalType value = null;
190         final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(type);
191         if (taggedItem.isPresent()) {
192             final Item item = taggedItem.get().getItem();
193             final Item baseItem = taggedItem.get().getBaseItem();
194             if (baseItem instanceof RollershutterItem || baseItem instanceof DimmerItem) {
195                 value = item.getStateAs(PercentType.class);
196             } else {
197                 value = item.getStateAs(DecimalType.class);
198             }
199         }
200         return value != null ? convertPosition(value.intValue(), openPosition) : closedPosition;
201     }
202 }