]> git.basschouten.com Git - openhab-addons.git/blob
e4245dbee8f7900678c726f4b5c5894ff0e1fa4e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.OpenClosedType;
20 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
21 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
22 import org.openhab.io.homekit.internal.HomekitSettings;
23 import org.openhab.io.homekit.internal.HomekitTaggedItem;
24
25 import io.github.hapjava.accessories.SpeakerAccessory;
26 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
27 import io.github.hapjava.services.impl.SpeakerService;
28
29 /**
30  * Implements Speaker using an Item that provides an On/Off state for Mute.
31  *
32  * @author Eugen Freiter - Initial contribution
33  */
34 public class HomekitSpeakerImpl extends AbstractHomekitAccessoryImpl implements SpeakerAccessory {
35     private final BooleanItemReader muteReader;
36
37     public HomekitSpeakerImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
38             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
39         super(taggedItem, mandatoryCharacteristics, updater, settings);
40         muteReader = createBooleanReader(HomekitCharacteristicType.MUTE, OnOffType.ON, OpenClosedType.OPEN);
41         getServices().add(new SpeakerService(this));
42     }
43
44     @Override
45     public CompletableFuture<Boolean> isMuted() {
46         return CompletableFuture.completedFuture(muteReader.getValue());
47     }
48
49     @Override
50     public CompletableFuture<Void> setMute(boolean state) {
51         muteReader.setValue(state);
52         return CompletableFuture.completedFuture(null);
53     }
54
55     @Override
56     public void subscribeMuteState(HomekitCharacteristicChangeCallback callback) {
57         subscribe(HomekitCharacteristicType.MUTE, callback);
58     }
59
60     @Override
61     public void unsubscribeMuteState() {
62         unsubscribe(HomekitCharacteristicType.MUTE);
63     }
64 }