]> git.basschouten.com Git - openhab-addons.git/blob
dcca031e0c913aefd186a29fc7f50f7611c8fed1
[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 java.util.List;
16 import java.util.concurrent.CompletableFuture;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
20 import org.openhab.io.homekit.internal.HomekitException;
21 import org.openhab.io.homekit.internal.HomekitSettings;
22 import org.openhab.io.homekit.internal.HomekitTaggedItem;
23
24 import io.github.hapjava.accessories.HomekitAccessory;
25 import io.github.hapjava.characteristics.impl.common.ConfiguredNameCharacteristic;
26 import io.github.hapjava.characteristics.impl.common.IdentifierCharacteristic;
27 import io.github.hapjava.characteristics.impl.common.IsConfiguredCharacteristic;
28 import io.github.hapjava.characteristics.impl.common.IsConfiguredEnum;
29 import io.github.hapjava.characteristics.impl.inputsource.CurrentVisibilityStateCharacteristic;
30 import io.github.hapjava.characteristics.impl.inputsource.CurrentVisibilityStateEnum;
31 import io.github.hapjava.characteristics.impl.inputsource.InputSourceTypeCharacteristic;
32 import io.github.hapjava.characteristics.impl.inputsource.InputSourceTypeEnum;
33 import io.github.hapjava.services.impl.InputSourceService;
34
35 /**
36  * Implements Input Source
37  * 
38  * This is a little different in that we don't implement the accessory interface.
39  * This is because several of the "mandatory" characteristics we don't require,
40  * and wait until all optional attributes are added and if they don't exist
41  * it will create "default" values for them.
42  *
43  * @author Cody Cutrer - Initial contribution
44  */
45 @NonNullByDefault({})
46 public class HomekitInputSourceImpl extends AbstractHomekitAccessoryImpl {
47
48     public HomekitInputSourceImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
49             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
50         super(taggedItem, mandatoryCharacteristics, updater, settings);
51     }
52
53     @Override
54     public void init() throws HomekitException {
55         super.init();
56
57         // these charactereristics are technically mandatory, but we provide defaults if they're not provided
58         var configuredNameCharacteristic = getCharacteristic(ConfiguredNameCharacteristic.class)
59                 .orElseGet(() -> new ConfiguredNameCharacteristic(() -> getName(), v -> {
60                 }, v -> {
61                 }, () -> {
62                 }));
63         var inputSourceTypeCharacteristic = getCharacteristic(InputSourceTypeCharacteristic.class)
64                 .orElseGet(() -> new InputSourceTypeCharacteristic(
65                         () -> CompletableFuture.completedFuture(InputSourceTypeEnum.OTHER), v -> {
66                         }, () -> {
67                         }));
68         var isConfiguredCharacteristic = getCharacteristic(IsConfiguredCharacteristic.class)
69                 .orElseGet(() -> new IsConfiguredCharacteristic(
70                         () -> CompletableFuture.completedFuture(IsConfiguredEnum.CONFIGURED), v -> {
71                         }, v -> {
72                         }, () -> {
73                         }));
74         var currentVisibilityStateCharacteristic = getCharacteristic(CurrentVisibilityStateCharacteristic.class)
75                 .orElseGet(() -> new CurrentVisibilityStateCharacteristic(
76                         () -> CompletableFuture.completedFuture(CurrentVisibilityStateEnum.SHOWN), v -> {
77                         }, () -> {
78                         }));
79
80         var service = new InputSourceService(configuredNameCharacteristic, inputSourceTypeCharacteristic,
81                 isConfiguredCharacteristic, currentVisibilityStateCharacteristic);
82
83         var identifierCharacteristic = getCharacteristic(IdentifierCharacteristic.class);
84         if (identifierCharacteristic.isEmpty()) {
85             service.addOptionalCharacteristic(new IdentifierCharacteristic(() -> CompletableFuture.completedFuture(1)));
86         }
87
88         addService(service);
89     }
90
91     @Override
92     public boolean isLinkable(HomekitAccessory parentAccessory) {
93         return parentAccessory instanceof HomekitTelevisionImpl;
94     }
95 }