]> git.basschouten.com Git - openhab-addons.git/blob
db4ccc7426327309d60927d0dafdad253e09297a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Optional;
17 import java.util.concurrent.CompletableFuture;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
21 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
22 import org.openhab.io.homekit.internal.HomekitException;
23 import org.openhab.io.homekit.internal.HomekitSettings;
24 import org.openhab.io.homekit.internal.HomekitTaggedItem;
25
26 import io.github.hapjava.characteristics.impl.audio.MuteCharacteristic;
27 import io.github.hapjava.characteristics.impl.audio.VolumeCharacteristic;
28 import io.github.hapjava.characteristics.impl.common.ActiveCharacteristic;
29 import io.github.hapjava.characteristics.impl.televisionspeaker.VolumeControlTypeCharacteristic;
30 import io.github.hapjava.characteristics.impl.televisionspeaker.VolumeControlTypeEnum;
31 import io.github.hapjava.characteristics.impl.televisionspeaker.VolumeSelectorCharacteristic;
32 import io.github.hapjava.services.impl.TelevisionSpeakerService;
33
34 /**
35  * Implements Television Speaker
36  * 
37  * This is a little different in that we don't implement the accessory interface.
38  * This is because several of the "mandatory" characteristics we don't require,
39  * and wait until all optional attributes are added and if they don't exist
40  * it will create "default" values for them.
41  *
42  * @author Cody Cutrer - Initial contribution
43  */
44 @NonNullByDefault({})
45 public class HomekitTelevisionSpeakerImpl extends AbstractHomekitAccessoryImpl {
46
47     public HomekitTelevisionSpeakerImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
48             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
49         super(taggedItem, mandatoryCharacteristics, updater, settings);
50     }
51
52     @Override
53     public void init() throws HomekitException {
54         super.init();
55
56         var muteCharacteristic = (MuteCharacteristic) HomekitCharacteristicFactory
57                 .createCharacteristic(getCharacteristic(HomekitCharacteristicType.MUTE).get(), getUpdater());
58         // this characteristic is technically optional, but we provide a default implementation if it's not provided
59         var volumeControlTypeCharacteristic = getCharacteristic(VolumeControlTypeCharacteristic.class);
60         // optional characteristics
61         var volumeCharacteristic = getCharacteristic(VolumeCharacteristic.class);
62         var volumeSelectorCharacteristic = getCharacteristic(VolumeSelectorCharacteristic.class);
63
64         if (!volumeControlTypeCharacteristic.isPresent()) {
65             VolumeControlTypeEnum type;
66             if (volumeCharacteristic.isPresent()) {
67                 type = VolumeControlTypeEnum.ABSOLUTE;
68             } else if (volumeSelectorCharacteristic.isPresent()) {
69                 type = VolumeControlTypeEnum.RELATIVE;
70             } else {
71                 type = VolumeControlTypeEnum.NONE;
72             }
73             volumeControlTypeCharacteristic = Optional
74                     .of(new VolumeControlTypeCharacteristic(() -> CompletableFuture.completedFuture(type), v -> {
75                     }, () -> {
76                     }));
77         }
78
79         var service = new TelevisionSpeakerService(muteCharacteristic);
80
81         getCharacteristic(ActiveCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
82         volumeCharacteristic.ifPresent(c -> service.addOptionalCharacteristic(c));
83         service.addOptionalCharacteristic(volumeControlTypeCharacteristic.get());
84
85         getServices().add(service);
86     }
87 }