2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
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;
19 import java.util.List;
21 import java.util.Optional;
22 import java.util.concurrent.CompletableFuture;
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;
42 import io.github.hapjava.characteristics.Characteristic;
43 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
44 import io.github.hapjava.characteristics.impl.windowcovering.PositionStateEnum;
47 * Common methods for Door, Window and WindowCovering.
49 * @author Eugen Freiter - Initial contribution
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;
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,
69 closedPosition = inverted ? 0 : 100;
70 openPosition = inverted ? 100 : 0;
71 positionStateMapping = createMapping(POSITION_STATE, PositionStateEnum.class);
74 public CompletableFuture<Integer> getCurrentPosition() {
75 return CompletableFuture.completedFuture(convertPositionState(CURRENT_POSITION, openPosition, closedPosition));
78 public CompletableFuture<PositionStateEnum> getPositionState() {
79 return CompletableFuture.completedFuture(emulateState ? emulatedState
80 : getKeyFromMapping(POSITION_STATE, positionStateMapping, PositionStateEnum.STOPPED));
83 public CompletableFuture<Integer> getTargetPosition() {
84 return CompletableFuture.completedFuture(convertPositionState(TARGET_POSITION, openPosition, closedPosition));
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);
100 itemAsRollerShutterItem.send(StopMoveType.STOP);
102 emulatedState = PositionStateEnum.STOPPED;
104 itemAsRollerShutterItem.send(new PercentType(targetPosition));
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;
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));
129 "Unsupported item type for characteristic {} at accessory {}. Expected Rollershutter, Dimmer or Number item, got {}",
130 TARGET_POSITION, getName(), item.getClass());
133 logger.warn("Mandatory characteristic {} not found at accessory {}. ", TARGET_POSITION, getName());
135 return CompletableFuture.completedFuture(null);
138 public void subscribeCurrentPosition(HomekitCharacteristicChangeCallback callback) {
139 subscribe(CURRENT_POSITION, callback);
142 public void subscribePositionState(HomekitCharacteristicChangeCallback callback) {
143 subscribe(POSITION_STATE, callback);
146 public void subscribeTargetPosition(HomekitCharacteristicChangeCallback callback) {
147 subscribe(TARGET_POSITION, callback);
150 public void unsubscribeCurrentPosition() {
151 unsubscribe(CURRENT_POSITION);
154 public void unsubscribePositionState() {
155 unsubscribe(POSITION_STATE);
158 public void unsubscribeTargetPosition() {
159 unsubscribe(TARGET_POSITION);
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
170 * of 100 indicates a position that allows most light.", i.e.
172 * - completely open if position is 100%,
173 * - completely closed if position is 0%.
175 * As openHAB rollershutter item is typically used for window covering, the binding has by default inverting
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.
180 * @param value source value
181 * @return target value
183 protected int convertPosition(int value, int openPosition) {
184 return Math.abs(openPosition - value);
187 protected int convertPositionState(HomekitCharacteristicType type, int openPosition, int closedPosition) {
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);
197 value = item.getStateAs(DecimalType.class);
200 return value != null ? convertPosition(value.intValue(), openPosition) : closedPosition;