]> git.basschouten.com Git - openhab-addons.git/blob
516e305e564b981fa79565d7c2a22593ce9b62ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.EnumMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.concurrent.CompletableFuture;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
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.io.homekit.internal.HomekitAccessoryUpdater;
34 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
35 import org.openhab.io.homekit.internal.HomekitSettings;
36 import org.openhab.io.homekit.internal.HomekitTaggedItem;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
41 import io.github.hapjava.characteristics.impl.windowcovering.PositionStateEnum;
42
43 /**
44  * Common methods for Door, Window and WindowCovering.
45  * 
46  * @author Eugen Freiter - Initial contribution
47  */
48 @NonNullByDefault
49 abstract class AbstractHomekitPositionAccessoryImpl extends AbstractHomekitAccessoryImpl {
50     private final Logger logger = LoggerFactory.getLogger(AbstractHomekitPositionAccessoryImpl.class);
51     protected int closedPosition;
52     protected int openPosition;
53     private final Map<PositionStateEnum, String> positionStateMapping;
54
55     public AbstractHomekitPositionAccessoryImpl(HomekitTaggedItem taggedItem,
56             List<HomekitTaggedItem> mandatoryCharacteristics, HomekitAccessoryUpdater updater,
57             HomekitSettings settings) {
58         super(taggedItem, mandatoryCharacteristics, updater, settings);
59         final String invertedConfig = getAccessoryConfiguration(HomekitTaggedItem.INVERTED, "true");
60         final boolean inverted = invertedConfig.equalsIgnoreCase("yes") || invertedConfig.equalsIgnoreCase("true");
61         closedPosition = inverted ? 0 : 100;
62         openPosition = inverted ? 100 : 0;
63         positionStateMapping = new EnumMap<>(PositionStateEnum.class);
64         positionStateMapping.put(PositionStateEnum.DECREASING, "DECREASING");
65         positionStateMapping.put(PositionStateEnum.INCREASING, "INCREASING");
66         positionStateMapping.put(PositionStateEnum.STOPPED, "STOPPED");
67         updateMapping(POSITION_STATE, positionStateMapping);
68     }
69
70     public CompletableFuture<Integer> getCurrentPosition() {
71         return CompletableFuture.completedFuture(convertPositionState(CURRENT_POSITION, openPosition, closedPosition));
72     }
73
74     public CompletableFuture<PositionStateEnum> getPositionState() {
75         return CompletableFuture
76                 .completedFuture(getKeyFromMapping(POSITION_STATE, positionStateMapping, PositionStateEnum.STOPPED));
77     }
78
79     public CompletableFuture<Integer> getTargetPosition() {
80         return CompletableFuture.completedFuture(convertPositionState(TARGET_POSITION, openPosition, closedPosition));
81     }
82
83     public CompletableFuture<Void> setTargetPosition(int value) {
84         getCharacteristic(TARGET_POSITION).ifPresentOrElse(taggedItem -> {
85             final Item item = taggedItem.getItem();
86             final int targetPosition = convertPosition(value, openPosition);
87             if (item instanceof RollershutterItem) {
88                 ((RollershutterItem) item).send(new PercentType(targetPosition));
89             } else if (item instanceof DimmerItem) {
90                 ((DimmerItem) item).send(new PercentType(targetPosition));
91             } else if (item instanceof NumberItem) {
92                 ((NumberItem) item).send(new DecimalType(targetPosition));
93             } else {
94                 logger.warn(
95                         "Unsupported item type for characteristic {} at accessory {}. Expected Rollershutter, Dimmer or Number item, got {}",
96                         TARGET_POSITION, getName(), item.getClass());
97             }
98         }, () -> {
99             logger.warn("Mandatory characteristic {} not found at accessory {}. ", TARGET_POSITION, getName());
100         });
101         return CompletableFuture.completedFuture(null);
102     }
103
104     public void subscribeCurrentPosition(HomekitCharacteristicChangeCallback callback) {
105         subscribe(CURRENT_POSITION, callback);
106     }
107
108     public void subscribePositionState(HomekitCharacteristicChangeCallback callback) {
109         subscribe(POSITION_STATE, callback);
110     }
111
112     public void subscribeTargetPosition(HomekitCharacteristicChangeCallback callback) {
113         subscribe(TARGET_POSITION, callback);
114     }
115
116     public void unsubscribeCurrentPosition() {
117         unsubscribe(CURRENT_POSITION);
118     }
119
120     public void unsubscribePositionState() {
121         unsubscribe(POSITION_STATE);
122     }
123
124     public void unsubscribeTargetPosition() {
125         unsubscribe(TARGET_POSITION);
126     }
127
128     /**
129      * convert/invert position of door/window/blinds.
130      * openHAB Rollershutter is:
131      * - completely open if position is 0%,
132      * - completely closed if position is 100%.
133      * HomeKit mapping has inverted mapping
134      * From Specification: "For blinds/shades/awnings, a value of 0 indicates a position that permits the least light
135      * and a value
136      * of 100 indicates a position that allows most light.", i.e.
137      * HomeKit Blinds is
138      * - completely open if position is 100%,
139      * - completely closed if position is 0%.
140      *
141      * As openHAB rollershutter item is typically used for window covering, the binding has by default inverting
142      * mapping.
143      * One can override this default behaviour with inverted="false/no" flag. in this cases, openHAB item value will be
144      * sent to HomeKit with no changes.
145      *
146      * @param value source value
147      * @return target value
148      */
149     protected int convertPosition(int value, int openPosition) {
150         return Math.abs(openPosition - value);
151     }
152
153     protected int convertPositionState(HomekitCharacteristicType type, int openPosition, int closedPosition) {
154         @Nullable
155         DecimalType value = null;
156         final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(type);
157         if (taggedItem.isPresent()) {
158             final Item item = taggedItem.get().getItem();
159             if ((item instanceof RollershutterItem) || ((item instanceof DimmerItem))) {
160                 value = item.getStateAs(PercentType.class);
161             } else {
162                 value = item.getStateAs(DecimalType.class);
163             }
164         }
165         return value != null ? convertPosition(value.intValue(), openPosition) : closedPosition;
166     }
167 }