]> git.basschouten.com Git - openhab-addons.git/blob
774533086027ac6a363384300fbccca425b95a51
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.concurrent.CompletableFuture;
24
25 import javax.measure.Quantity;
26 import javax.measure.Unit;
27
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.core.items.GenericItem;
31 import org.openhab.core.items.Item;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.OpenClosedType;
34 import org.openhab.core.library.types.StringType;
35 import org.openhab.core.library.unit.ImperialUnits;
36 import org.openhab.core.library.unit.SIUnits;
37 import org.openhab.core.types.State;
38 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
39 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
40 import org.openhab.io.homekit.internal.HomekitSettings;
41 import org.openhab.io.homekit.internal.HomekitTaggedItem;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import io.github.hapjava.accessories.HomekitAccessory;
46 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
47 import io.github.hapjava.services.Service;
48
49 /**
50  * Abstract class for Homekit Accessory implementations, this provides the
51  * accessory metadata using information from the underlying Item.
52  *
53  * @author Andy Lintner - Initial contribution
54  */
55 abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
56     private final Logger logger = LoggerFactory.getLogger(AbstractHomekitAccessoryImpl.class);
57     private final List<HomekitTaggedItem> characteristics;
58     private final HomekitTaggedItem accessory;
59     private final HomekitAccessoryUpdater updater;
60     private final HomekitSettings settings;
61     private final List<Service> services;
62
63     public AbstractHomekitAccessoryImpl(HomekitTaggedItem accessory, List<HomekitTaggedItem> characteristics,
64             HomekitAccessoryUpdater updater, HomekitSettings settings) {
65         this.characteristics = characteristics;
66         this.accessory = accessory;
67         this.updater = updater;
68         this.services = new ArrayList<>();
69         this.settings = settings;
70     }
71
72     @NonNullByDefault
73     protected Optional<HomekitTaggedItem> getCharacteristic(HomekitCharacteristicType type) {
74         return characteristics.stream().filter(c -> c.getCharacteristicType() == type).findAny();
75     }
76
77     @Override
78     public int getId() {
79         return accessory.getId();
80     }
81
82     @Override
83     public CompletableFuture<String> getName() {
84         return CompletableFuture.completedFuture(accessory.getItem().getLabel());
85     }
86
87     @Override
88     public CompletableFuture<String> getManufacturer() {
89         return CompletableFuture.completedFuture("none");
90     }
91
92     @Override
93     public CompletableFuture<String> getModel() {
94         return CompletableFuture.completedFuture("none");
95     }
96
97     @Override
98     public CompletableFuture<String> getSerialNumber() {
99         return CompletableFuture.completedFuture(accessory.getItem().getName());
100     }
101
102     @Override
103     public CompletableFuture<String> getFirmwareRevision() {
104         return CompletableFuture.completedFuture("none");
105     }
106
107     @Override
108     public void identify() {
109         // We're not going to support this for now
110     }
111
112     public HomekitTaggedItem getRootAccessory() {
113         return accessory;
114     }
115
116     @Override
117     public Collection<Service> getServices() {
118         return this.services;
119     }
120
121     protected HomekitAccessoryUpdater getUpdater() {
122         return updater;
123     }
124
125     protected HomekitSettings getSettings() {
126         return settings;
127     }
128
129     @NonNullByDefault
130     protected void subscribe(HomekitCharacteristicType characteristicType,
131             HomekitCharacteristicChangeCallback callback) {
132         final Optional<HomekitTaggedItem> characteristic = getCharacteristic(characteristicType);
133         if (characteristic.isPresent()) {
134             getUpdater().subscribe((GenericItem) characteristic.get().getItem(), characteristicType.getTag(), callback);
135         } else {
136             logger.warn("Missing mandatory characteristic {}", characteristicType);
137         }
138     }
139
140     @NonNullByDefault
141     protected void unsubscribe(HomekitCharacteristicType characteristicType) {
142         final Optional<HomekitTaggedItem> characteristic = getCharacteristic(characteristicType);
143         if (characteristic.isPresent()) {
144             getUpdater().unsubscribe((GenericItem) characteristic.get().getItem(), characteristicType.getTag());
145         } else {
146             logger.warn("Missing mandatory characteristic {}", characteristicType);
147         }
148     }
149
150     protected @Nullable <T extends State> T getStateAs(HomekitCharacteristicType characteristic, Class<T> type) {
151         final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(characteristic);
152         if (taggedItem.isPresent()) {
153             final State state = taggedItem.get().getItem().getStateAs(type);
154             if (state != null) {
155                 return state.as(type);
156             }
157         }
158         logger.debug("State for characteristic {} at accessory {} cannot be retrieved.", characteristic,
159                 accessory.getName());
160         return null;
161     }
162
163     @NonNullByDefault
164     protected <T extends Item> Optional<T> getItem(HomekitCharacteristicType characteristic, Class<T> type) {
165         final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(characteristic);
166         if (taggedItem.isPresent()) {
167             final Item item = taggedItem.get().getItem();
168             if (type.isInstance(item)) {
169                 return Optional.of((T) item);
170             } else {
171                 logger.warn("Unsupported item type for characteristic {} at accessory {}. Expected {}, got {}",
172                         characteristic, accessory.getItem().getName(), type, taggedItem.get().getItem().getClass());
173             }
174         } else {
175             logger.warn("Mandatory characteristic {} not found at accessory {}. ", characteristic,
176                     accessory.getItem().getName());
177
178         }
179         return Optional.empty();
180     }
181
182     /**
183      * return configuration attached to the root accessory, e.g. groupItem.
184      * Note: result will be casted to the type of the default value.
185      * The type for number is BigDecimal.
186      *
187      * @param key configuration key
188      * @param defaultValue default value
189      * @param <T> expected type
190      * @return configuration value
191      */
192     @NonNullByDefault
193     protected <T> T getAccessoryConfiguration(String key, T defaultValue) {
194         return accessory.getConfiguration(key, defaultValue);
195     }
196
197     /**
198      * return configuration of the characteristic item, e.g. currentTemperature.
199      * Note: result will be casted to the type of the default value.
200      * The type for number is BigDecimal.
201      *
202      * @param characteristicType characteristic type
203      * @param key configuration key
204      * @param defaultValue default value
205      * @param <T> expected type
206      * @return configuration value
207      */
208     @NonNullByDefault
209     protected <T> T getAccessoryConfiguration(HomekitCharacteristicType characteristicType, String key,
210             T defaultValue) {
211         return getCharacteristic(characteristicType)
212                 .map(homekitTaggedItem -> homekitTaggedItem.getConfiguration(key, defaultValue)).orElse(defaultValue);
213     }
214
215     /**
216      * update mapping with values from item configuration.
217      * it checks for all keys from the mapping whether there is configuration at item with the same key and if yes,
218      * replace the value.
219      *
220      * @param characteristicType characteristicType to identify item
221      * @param map mapping to update
222      * @param customEnumList list to store custom state enumeration
223      */
224     @NonNullByDefault
225     protected <T> void updateMapping(HomekitCharacteristicType characteristicType, Map<T, String> map,
226             @Nullable List<T> customEnumList) {
227         getCharacteristic(characteristicType).ifPresent(c -> {
228             final Map<String, Object> configuration = c.getConfiguration();
229             if (configuration != null) {
230                 map.forEach((k, current_value) -> {
231                     final Object new_value = configuration.get(k.toString());
232                     if (new_value instanceof String) {
233                         map.put(k, (String) new_value);
234                         if (customEnumList != null) {
235                             customEnumList.add(k);
236                         }
237                     }
238                 });
239             }
240         });
241     }
242
243     @NonNullByDefault
244     protected <T> void updateMapping(HomekitCharacteristicType characteristicType, Map<T, String> map) {
245         updateMapping(characteristicType, map, null);
246     }
247
248     /**
249      * takes item state as value and retrieves the key for that value from mapping.
250      * e.g. used to map StringItem value to HomeKit Enum
251      *
252      * @param characteristicType characteristicType to identify item
253      * @param mapping mapping
254      * @param defaultValue default value if nothing found in mapping
255      * @param <T> type of the result derived from
256      * @return key for the value
257      */
258     @NonNullByDefault
259     protected <T> T getKeyFromMapping(HomekitCharacteristicType characteristicType, Map<T, String> mapping,
260             T defaultValue) {
261         final Optional<HomekitTaggedItem> c = getCharacteristic(characteristicType);
262         if (c.isPresent()) {
263             final State state = c.get().getItem().getState();
264             logger.trace("getKeyFromMapping: characteristic {}, state {}, mapping {}", characteristicType.getTag(),
265                     state, mapping);
266             if (state instanceof StringType) {
267                 return mapping.entrySet().stream().filter(entry -> state.toString().equalsIgnoreCase(entry.getValue()))
268                         .findAny().map(Entry::getKey).orElseGet(() -> {
269                             logger.warn(
270                                     "Wrong value {} for {} characteristic of the item {}. Expected one of following {}. Returning {}.",
271                                     state.toString(), characteristicType.getTag(), c.get().getName(), mapping.values(),
272                                     defaultValue);
273                             return defaultValue;
274                         });
275             }
276         }
277         return defaultValue;
278     }
279
280     @NonNullByDefault
281     protected void addCharacteristic(HomekitTaggedItem characteristic) {
282         characteristics.add(characteristic);
283     }
284
285     @NonNullByDefault
286     private <T extends Quantity<T>> double convertAndRound(double value, Unit<T> from, Unit<T> to) {
287         double rawValue = from.equals(to) ? value : from.getConverterTo(to).convert(value);
288         return new BigDecimal(rawValue).setScale(1, RoundingMode.HALF_UP).doubleValue();
289     }
290
291     @NonNullByDefault
292     protected double convertToCelsius(double degrees) {
293         return convertAndRound(degrees,
294                 getSettings().useFahrenheitTemperature ? ImperialUnits.FAHRENHEIT : SIUnits.CELSIUS, SIUnits.CELSIUS);
295     }
296
297     @NonNullByDefault
298     protected double convertFromCelsius(double degrees) {
299         return convertAndRound(degrees,
300                 getSettings().useFahrenheitTemperature ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT,
301                 ImperialUnits.FAHRENHEIT);
302     }
303
304     /**
305      * create boolean reader with ON state mapped to trueOnOffValue or trueOpenClosedValue depending of item type
306      *
307      * @param characteristicType characteristic id
308      * @param trueOnOffValue ON value for switch
309      * @param trueOpenClosedValue ON value for contact
310      * @return boolean readed
311      * @throws IncompleteAccessoryException
312      */
313     @NonNullByDefault
314     protected BooleanItemReader createBooleanReader(HomekitCharacteristicType characteristicType,
315             OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) throws IncompleteAccessoryException {
316         return new BooleanItemReader(
317                 getItem(characteristicType, GenericItem.class)
318                         .orElseThrow(() -> new IncompleteAccessoryException(characteristicType)),
319                 trueOnOffValue, trueOpenClosedValue);
320     }
321
322     /**
323      * create boolean reader with default ON/OFF mapping considering inverted flag
324      *
325      * @param characteristicType characteristic id
326      * @return boolean reader
327      * @throws IncompleteAccessoryException
328      */
329     @NonNullByDefault
330     protected BooleanItemReader createBooleanReader(HomekitCharacteristicType characteristicType)
331             throws IncompleteAccessoryException {
332         final HomekitTaggedItem taggedItem = getCharacteristic(characteristicType)
333                 .orElseThrow(() -> new IncompleteAccessoryException(characteristicType));
334         return new BooleanItemReader(taggedItem.getItem(), taggedItem.isInverted() ? OnOffType.OFF : OnOffType.ON,
335                 taggedItem.isInverted() ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
336     }
337 }