2 * Copyright (c) 2010-2022 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 java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.Optional;
21 import java.util.concurrent.CompletableFuture;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.items.GenericItem;
26 import org.openhab.core.items.Item;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.OpenClosedType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.types.State;
31 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
32 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
33 import org.openhab.io.homekit.internal.HomekitSettings;
34 import org.openhab.io.homekit.internal.HomekitTaggedItem;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import io.github.hapjava.accessories.HomekitAccessory;
39 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
40 import io.github.hapjava.services.Service;
43 * Abstract class for Homekit Accessory implementations, this provides the
44 * accessory metadata using information from the underlying Item.
46 * @author Andy Lintner - Initial contribution
48 abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
49 private final Logger logger = LoggerFactory.getLogger(AbstractHomekitAccessoryImpl.class);
50 private final List<HomekitTaggedItem> characteristics;
51 private final HomekitTaggedItem accessory;
52 private final HomekitAccessoryUpdater updater;
53 private final HomekitSettings settings;
54 private final List<Service> services;
56 public AbstractHomekitAccessoryImpl(HomekitTaggedItem accessory, List<HomekitTaggedItem> characteristics,
57 HomekitAccessoryUpdater updater, HomekitSettings settings) {
58 this.characteristics = characteristics;
59 this.accessory = accessory;
60 this.updater = updater;
61 this.services = new ArrayList<>();
62 this.settings = settings;
66 protected Optional<HomekitTaggedItem> getCharacteristic(HomekitCharacteristicType type) {
67 return characteristics.stream().filter(c -> c.getCharacteristicType() == type).findAny();
72 return accessory.getId();
76 public CompletableFuture<String> getName() {
77 return CompletableFuture.completedFuture(accessory.getItem().getLabel());
81 public CompletableFuture<String> getManufacturer() {
82 return CompletableFuture.completedFuture("none");
86 public CompletableFuture<String> getModel() {
87 return CompletableFuture.completedFuture("none");
91 public CompletableFuture<String> getSerialNumber() {
92 return CompletableFuture.completedFuture(accessory.getItem().getName());
96 public CompletableFuture<String> getFirmwareRevision() {
97 return CompletableFuture.completedFuture("none");
101 public void identify() {
102 // We're not going to support this for now
105 public HomekitTaggedItem getRootAccessory() {
110 public Collection<Service> getServices() {
111 return this.services;
114 protected HomekitAccessoryUpdater getUpdater() {
118 protected HomekitSettings getSettings() {
123 protected void subscribe(HomekitCharacteristicType characteristicType,
124 HomekitCharacteristicChangeCallback callback) {
125 final Optional<HomekitTaggedItem> characteristic = getCharacteristic(characteristicType);
126 if (characteristic.isPresent()) {
127 getUpdater().subscribe((GenericItem) characteristic.get().getItem(), characteristicType.getTag(), callback);
129 logger.warn("Missing mandatory characteristic {}", characteristicType);
134 protected void unsubscribe(HomekitCharacteristicType characteristicType) {
135 final Optional<HomekitTaggedItem> characteristic = getCharacteristic(characteristicType);
136 if (characteristic.isPresent()) {
137 getUpdater().unsubscribe((GenericItem) characteristic.get().getItem(), characteristicType.getTag());
139 logger.warn("Missing mandatory characteristic {}", characteristicType);
143 protected @Nullable State getState(HomekitCharacteristicType characteristic) {
144 final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(characteristic);
145 if (taggedItem.isPresent()) {
146 return taggedItem.get().getItem().getState();
148 logger.debug("State for characteristic {} at accessory {} cannot be retrieved.", characteristic,
149 accessory.getName());
153 protected @Nullable <T extends State> T getStateAs(HomekitCharacteristicType characteristic, Class<T> type) {
154 final State state = getState(characteristic);
156 return state.as(type);
161 protected @Nullable Double getStateAsTemperature(HomekitCharacteristicType characteristic) {
162 return HomekitCharacteristicFactory.stateAsTemperature(getState(characteristic));
166 protected <T extends Item> Optional<T> getItem(HomekitCharacteristicType characteristic, Class<T> type) {
167 final Optional<HomekitTaggedItem> taggedItem = getCharacteristic(characteristic);
168 if (taggedItem.isPresent()) {
169 final Item item = taggedItem.get().getItem();
170 if (type.isInstance(item)) {
171 return Optional.of((T) item);
173 logger.warn("Unsupported item type for characteristic {} at accessory {}. Expected {}, got {}",
174 characteristic, accessory.getItem().getName(), type, taggedItem.get().getItem().getClass());
177 logger.warn("Mandatory characteristic {} not found at accessory {}. ", characteristic,
178 accessory.getItem().getName());
181 return Optional.empty();
185 * return configuration attached to the root accessory, e.g. groupItem.
186 * Note: result will be casted to the type of the default value.
187 * The type for number is BigDecimal.
189 * @param key configuration key
190 * @param defaultValue default value
191 * @param <T> expected type
192 * @return configuration value
195 protected <T> T getAccessoryConfiguration(String key, T defaultValue) {
196 return accessory.getConfiguration(key, defaultValue);
200 * return configuration attached to the root accessory, e.g. groupItem.
202 * @param key configuration key
203 * @param defaultValue default value
204 * @return configuration value
207 protected boolean getAccessoryConfigurationAsBoolean(String key, boolean defaultValue) {
208 return accessory.getConfigurationAsBoolean(key, defaultValue);
212 * return configuration of the characteristic item, e.g. currentTemperature.
213 * Note: result will be casted to the type of the default value.
214 * The type for number is BigDecimal.
216 * @param characteristicType characteristic type
217 * @param key configuration key
218 * @param defaultValue default value
219 * @param <T> expected type
220 * @return configuration value
223 protected <T> T getAccessoryConfiguration(HomekitCharacteristicType characteristicType, String key,
225 return getCharacteristic(characteristicType)
226 .map(homekitTaggedItem -> homekitTaggedItem.getConfiguration(key, defaultValue)).orElse(defaultValue);
230 * update mapping with values from item configuration.
231 * it checks for all keys from the mapping whether there is configuration at item with the same key and if yes,
234 * @param characteristicType characteristicType to identify item
235 * @param map mapping to update
236 * @param customEnumList list to store custom state enumeration
239 protected <T> void updateMapping(HomekitCharacteristicType characteristicType, Map<T, String> map,
240 @Nullable List<T> customEnumList) {
241 getCharacteristic(characteristicType).ifPresent(c -> {
242 final Map<String, Object> configuration = c.getConfiguration();
243 if (configuration != null) {
244 map.forEach((k, current_value) -> {
245 final Object new_value = configuration.get(k.toString());
246 if (new_value instanceof String) {
247 map.put(k, (String) new_value);
248 if (customEnumList != null) {
249 customEnumList.add(k);
258 protected <T> void updateMapping(HomekitCharacteristicType characteristicType, Map<T, String> map) {
259 updateMapping(characteristicType, map, null);
263 * takes item state as value and retrieves the key for that value from mapping.
264 * e.g. used to map StringItem value to HomeKit Enum
266 * @param characteristicType characteristicType to identify item
267 * @param mapping mapping
268 * @param defaultValue default value if nothing found in mapping
269 * @param <T> type of the result derived from
270 * @return key for the value
273 protected <T> T getKeyFromMapping(HomekitCharacteristicType characteristicType, Map<T, String> mapping,
275 final Optional<HomekitTaggedItem> c = getCharacteristic(characteristicType);
277 final State state = c.get().getItem().getState();
278 logger.trace("getKeyFromMapping: characteristic {}, state {}, mapping {}", characteristicType.getTag(),
280 if (state instanceof StringType) {
281 return mapping.entrySet().stream().filter(entry -> state.toString().equalsIgnoreCase(entry.getValue()))
282 .findAny().map(Entry::getKey).orElseGet(() -> {
284 "Wrong value {} for {} characteristic of the item {}. Expected one of following {}. Returning {}.",
285 state.toString(), characteristicType.getTag(), c.get().getName(), mapping.values(),
295 protected void addCharacteristic(HomekitTaggedItem characteristic) {
296 characteristics.add(characteristic);
300 * create boolean reader with ON state mapped to trueOnOffValue or trueOpenClosedValue depending of item type
302 * @param characteristicType characteristic id
303 * @param trueOnOffValue ON value for switch
304 * @param trueOpenClosedValue ON value for contact
305 * @return boolean read
306 * @throws IncompleteAccessoryException
309 protected BooleanItemReader createBooleanReader(HomekitCharacteristicType characteristicType,
310 OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) throws IncompleteAccessoryException {
311 return new BooleanItemReader(
312 getItem(characteristicType, GenericItem.class)
313 .orElseThrow(() -> new IncompleteAccessoryException(characteristicType)),
314 trueOnOffValue, trueOpenClosedValue);
318 * create boolean reader with default ON/OFF mapping considering inverted flag
320 * @param characteristicType characteristic id
321 * @return boolean reader
322 * @throws IncompleteAccessoryException
325 protected BooleanItemReader createBooleanReader(HomekitCharacteristicType characteristicType)
326 throws IncompleteAccessoryException {
327 final HomekitTaggedItem taggedItem = getCharacteristic(characteristicType)
328 .orElseThrow(() -> new IncompleteAccessoryException(characteristicType));
329 return new BooleanItemReader(taggedItem.getItem(), taggedItem.isInverted() ? OnOffType.OFF : OnOffType.ON,
330 taggedItem.isInverted() ? OpenClosedType.CLOSED : OpenClosedType.OPEN);