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