]> git.basschouten.com Git - openhab-addons.git/blob
21fdf0934998255f9bb0c62cb809c43cecb4ae0f
[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 boolean sendUpDownForExtents;
60     protected PositionStateEnum emulatedState = PositionStateEnum.STOPPED;
61
62     public AbstractHomekitPositionAccessoryImpl(HomekitTaggedItem taggedItem,
63             List<HomekitTaggedItem> mandatoryCharacteristics, List<Characteristic> mandatoryRawCharacteristics,
64             HomekitAccessoryUpdater updater, HomekitSettings settings) {
65         super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
66         final boolean inverted = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.INVERTED, true);
67         emulateState = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.EMULATE_STOP_STATE, false);
68         emulateStopSameDirection = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.EMULATE_STOP_SAME_DIRECTION,
69                 false);
70         sendUpDownForExtents = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.SEND_UP_DOWN_FOR_EXTENTS, false);
71         closedPosition = inverted ? 0 : 100;
72         openPosition = inverted ? 100 : 0;
73         positionStateMapping = createMapping(POSITION_STATE, PositionStateEnum.class);
74     }
75
76     public CompletableFuture<Integer> getCurrentPosition() {
77         return CompletableFuture.completedFuture(convertPositionState(CURRENT_POSITION, openPosition, closedPosition));
78     }
79
80     public CompletableFuture<PositionStateEnum> getPositionState() {
81         return CompletableFuture.completedFuture(emulateState ? emulatedState
82                 : getKeyFromMapping(POSITION_STATE, positionStateMapping, PositionStateEnum.STOPPED));
83     }
84
85     public CompletableFuture<Integer> getTargetPosition() {
86         return CompletableFuture.completedFuture(convertPositionState(TARGET_POSITION, openPosition, closedPosition));
87     }
88
89     public CompletableFuture<Void> setTargetPosition(int value) {
90         getCharacteristic(TARGET_POSITION).ifPresentOrElse(taggedItem -> {
91             final Item item = taggedItem.getItem();
92             final int targetPosition = convertPosition(value, openPosition);
93             if (item instanceof RollershutterItem itemAsRollerShutterItem) {
94                 // HomeKit home app never sends STOP. we emulate stop if we receive 100% or 0% while the blind is moving
95                 if (emulateState && (targetPosition == 100 && emulatedState == PositionStateEnum.DECREASING)
96                         || ((targetPosition == 0 && emulatedState == PositionStateEnum.INCREASING))) {
97                     if (emulateStopSameDirection) {
98                         // some blinds devices do not support "STOP" but would stop if receive UP/DOWN while moving
99                         itemAsRollerShutterItem
100                                 .send(emulatedState == PositionStateEnum.INCREASING ? UpDownType.UP : UpDownType.DOWN);
101                     } else {
102                         itemAsRollerShutterItem.send(StopMoveType.STOP);
103                     }
104                     emulatedState = PositionStateEnum.STOPPED;
105                 } else {
106                     if (sendUpDownForExtents && targetPosition == 0) {
107                         itemAsRollerShutterItem.send(UpDownType.UP);
108                     } else if (sendUpDownForExtents && targetPosition == 100) {
109                         itemAsRollerShutterItem.send(UpDownType.DOWN);
110                     } else {
111                         itemAsRollerShutterItem.send(new PercentType(targetPosition));
112                     }
113                     if (emulateState) {
114                         @Nullable
115                         PercentType currentPosition = item.getStateAs(PercentType.class);
116                         emulatedState = currentPosition == null || currentPosition.intValue() == targetPosition
117                                 ? PositionStateEnum.STOPPED
118                                 : currentPosition.intValue() < targetPosition ? PositionStateEnum.INCREASING
119                                         : PositionStateEnum.DECREASING;
120                     }
121                 }
122             } else if (item instanceof DimmerItem itemAsDimmerItem) {
123                 itemAsDimmerItem.send(new PercentType(targetPosition));
124             } else if (item instanceof NumberItem itemAsNumberItem) {
125                 itemAsNumberItem.send(new DecimalType(targetPosition));
126             } else if (item instanceof GroupItem itemAsGroupItem
127                     && itemAsGroupItem.getBaseItem() instanceof RollershutterItem) {
128                 itemAsGroupItem.send(new PercentType(targetPosition));
129             } else if (item instanceof GroupItem itemAsGroupItem
130                     && itemAsGroupItem.getBaseItem() instanceof DimmerItem) {
131                 itemAsGroupItem.send(new PercentType(targetPosition));
132             } else if (item instanceof GroupItem itemAsGroupItem
133                     && itemAsGroupItem.getBaseItem() instanceof NumberItem) {
134                 itemAsGroupItem.send(new DecimalType(targetPosition));
135             } else {
136                 logger.warn(
137                         "Unsupported item type for characteristic {} at accessory {}. Expected Rollershutter, Dimmer or Number item, got {}",
138                         TARGET_POSITION, getName(), item.getClass());
139             }
140         }, () -> {
141             logger.warn("Mandatory characteristic {} not found at accessory {}. ", TARGET_POSITION, getName());
142         });
143         return CompletableFuture.completedFuture(null);
144     }
145
146     public void subscribeCurrentPosition(HomekitCharacteristicChangeCallback callback) {
147         subscribe(CURRENT_POSITION, callback);
148     }
149
150     public void subscribePositionState(HomekitCharacteristicChangeCallback callback) {
151         subscribe(POSITION_STATE, callback);
152     }
153
154     public void subscribeTargetPosition(HomekitCharacteristicChangeCallback callback) {
155         subscribe(TARGET_POSITION, callback);
156     }
157
158     public void unsubscribeCurrentPosition() {
159         unsubscribe(CURRENT_POSITION);
160     }
161
162     public void unsubscribePositionState() {
163         unsubscribe(POSITION_STATE);
164     }
165
166     public void unsubscribeTargetPosition() {
167         unsubscribe(TARGET_POSITION);
168     }
169
170     /**
171      * convert/invert position of door/window/blinds.
172      * openHAB Rollershutter is:
173      * - completely open if position is 0%,
174      * - completely closed if position is 100%.
175      * HomeKit mapping has inverted mapping
176      * From Specification: "For blinds/shades/awnings, a value of 0 indicates a position that permits the least light
177      * and a value
178      * of 100 indicates a position that allows most light.", i.e.
179      * HomeKit Blinds is
180      * - completely open if position is 100%,
181      * - completely closed if position is 0%.
182      *
183      * As openHAB rollershutter item is typically used for window covering, the binding has by default inverting
184      * mapping.
185      * One can override this default behaviour with inverted="false/no" flag. in this cases, openHAB item value will be
186      * sent to HomeKit with no changes.
187      *
188      * @param value source value
189      * @return target value
190      */
191     protected int convertPosition(int value, int openPosition) {
192         return Math.abs(openPosition - value);
193     }
194
195     protected int convertPositionState(HomekitCharacteristicType type, int openPosition, int closedPosition) {
196         @Nullable
197         DecimalType value = null;
198         final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(type);
199         if (taggedItem.isPresent()) {
200             final Item item = taggedItem.get().getItem();
201             final Item baseItem = taggedItem.get().getBaseItem();
202             if (baseItem instanceof RollershutterItem || baseItem instanceof DimmerItem) {
203                 value = item.getStateAs(PercentType.class);
204             } else {
205                 value = item.getStateAs(DecimalType.class);
206             }
207         }
208         return value != null ? convertPosition(value.intValue(), openPosition) : closedPosition;
209     }
210 }