]> git.basschouten.com Git - openhab-addons.git/blob
827f3695090dd37c221c885b269110dcd1435a8b
[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.openhab.core.library.items.DimmerItem;
19 import org.openhab.core.library.items.SwitchItem;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
22 import org.openhab.io.homekit.internal.HomekitCharacteristicType;
23 import org.openhab.io.homekit.internal.HomekitCommandType;
24 import org.openhab.io.homekit.internal.HomekitSettings;
25 import org.openhab.io.homekit.internal.HomekitTaggedItem;
26
27 import io.github.hapjava.accessories.LightbulbAccessory;
28 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
29 import io.github.hapjava.services.impl.LightbulbService;
30
31 /**
32  * Implements Lightbulb using an Item that provides an On/Off state
33  *
34  * @author Andy Lintner - Initial contribution
35  */
36 class HomekitLightbulbImpl extends AbstractHomekitAccessoryImpl implements LightbulbAccessory {
37
38     public HomekitLightbulbImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
39             HomekitAccessoryUpdater updater, HomekitSettings settings) {
40         super(taggedItem, mandatoryCharacteristics, updater, settings);
41         this.getServices().add(new LightbulbService(this));
42     }
43
44     @Override
45     public CompletableFuture<Boolean> getLightbulbPowerState() {
46         OnOffType state = getStateAs(HomekitCharacteristicType.ON_STATE, OnOffType.class);
47         return CompletableFuture.completedFuture(state == OnOffType.ON);
48     }
49
50     @Override
51     public CompletableFuture<Void> setLightbulbPowerState(boolean value) {
52         getCharacteristic(HomekitCharacteristicType.ON_STATE).ifPresent(tItem -> {
53             final OnOffType onOffState = OnOffType.from(value);
54             if (tItem.getBaseItem() instanceof DimmerItem) {
55                 tItem.sendCommandProxy(HomekitCommandType.ON_COMMAND, onOffState);
56             } else if (tItem.getBaseItem() instanceof SwitchItem) {
57                 tItem.send(onOffState);
58             }
59         });
60         return CompletableFuture.completedFuture(null);
61     }
62
63     @Override
64     public void subscribeLightbulbPowerState(HomekitCharacteristicChangeCallback callback) {
65         subscribe(HomekitCharacteristicType.ON_STATE, callback);
66     }
67
68     @Override
69     public void unsubscribeLightbulbPowerState() {
70         unsubscribe(HomekitCharacteristicType.ON_STATE);
71     }
72 }