2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
15 import java.util.List;
16 import java.util.concurrent.CompletableFuture;
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;
29 import io.github.hapjava.accessories.LightbulbAccessory;
30 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
31 import io.github.hapjava.services.impl.LightbulbService;
34 * Implements Lightbulb using an Item that provides an On/Off state
36 * @author Andy Lintner - Initial contribution
38 class HomekitLightbulbImpl extends AbstractHomekitAccessoryImpl implements LightbulbAccessory {
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));
47 public CompletableFuture<Boolean> getLightbulbPowerState() {
48 OnOffType state = getStateAs(HomekitCharacteristicType.ON_STATE, OnOffType.class);
49 return CompletableFuture.completedFuture(state == OnOffType.ON);
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);
65 return CompletableFuture.completedFuture(null);
69 public void subscribeLightbulbPowerState(HomekitCharacteristicChangeCallback callback) {
70 subscribe(HomekitCharacteristicType.ON_STATE, callback);
74 public void unsubscribeLightbulbPowerState() {
75 unsubscribe(HomekitCharacteristicType.ON_STATE);