2 * Copyright (c) 2010-2023 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.List;
16 import java.util.Optional;
17 import java.util.concurrent.CompletableFuture;
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;
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;
35 * Implements Television Speaker
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.
42 * @author Cody Cutrer - Initial contribution
45 public class HomekitTelevisionSpeakerImpl extends AbstractHomekitAccessoryImpl {
47 public HomekitTelevisionSpeakerImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
48 HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
49 super(taggedItem, mandatoryCharacteristics, updater, settings);
53 public void init() throws HomekitException {
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);
64 if (!volumeControlTypeCharacteristic.isPresent()) {
65 VolumeControlTypeEnum type;
66 if (volumeCharacteristic.isPresent()) {
67 type = VolumeControlTypeEnum.ABSOLUTE;
68 } else if (volumeSelectorCharacteristic.isPresent()) {
69 type = VolumeControlTypeEnum.RELATIVE;
71 type = VolumeControlTypeEnum.NONE;
73 volumeControlTypeCharacteristic = Optional
74 .of(new VolumeControlTypeCharacteristic(() -> CompletableFuture.completedFuture(type), v -> {
79 var service = new TelevisionSpeakerService(muteCharacteristic);
81 getCharacteristic(ActiveCharacteristic.class).ifPresent(c -> service.addOptionalCharacteristic(c));
82 volumeCharacteristic.ifPresent(c -> service.addOptionalCharacteristic(c));
83 service.addOptionalCharacteristic(volumeControlTypeCharacteristic.get());
85 getServices().add(service);