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