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