]> git.basschouten.com Git - openhab-addons.git/blob
d7ca648512206a376738524cd30f209cd47a38f1
[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 static org.openhab.io.homekit.internal.HomekitCharacteristicType.ON_STATE;
16
17 import java.util.List;
18 import java.util.concurrent.CompletableFuture;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
22 import org.openhab.io.homekit.internal.HomekitSettings;
23 import org.openhab.io.homekit.internal.HomekitTaggedItem;
24
25 import io.github.hapjava.accessories.BasicFanAccessory;
26 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
27 import io.github.hapjava.services.impl.BasicFanService;
28
29 /**
30  * Implements Fan using an Item that provides an On/Off state
31  *
32  * @author Cody Cutrer - Initial contribution
33  */
34 @NonNullByDefault({})
35 class HomekitBasicFanImpl extends AbstractHomekitAccessoryImpl implements BasicFanAccessory {
36     private final BooleanItemReader onReader;
37
38     public HomekitBasicFanImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
39             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
40         super(taggedItem, mandatoryCharacteristics, updater, settings);
41         onReader = createBooleanReader(ON_STATE);
42         this.getServices().add(new BasicFanService(this));
43     }
44
45     @Override
46     public CompletableFuture<Boolean> isOn() {
47         return CompletableFuture.completedFuture(onReader.getValue());
48     }
49
50     @Override
51     public CompletableFuture<Void> setOn(boolean state) {
52         onReader.setValue(state);
53         return CompletableFuture.completedFuture(null);
54     }
55
56     @Override
57     public void subscribeOn(HomekitCharacteristicChangeCallback callback) {
58         subscribe(ON_STATE, callback);
59     }
60
61     @Override
62     public void unsubscribeOn() {
63         unsubscribe(ON_STATE);
64     }
65 }