2 * Copyright (c) 2010-2024 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.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;
27 import io.github.hapjava.accessories.LightbulbAccessory;
28 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
29 import io.github.hapjava.services.impl.LightbulbService;
32 * Implements Lightbulb using an Item that provides an On/Off state
34 * @author Andy Lintner - Initial contribution
36 class HomekitLightbulbImpl extends AbstractHomekitAccessoryImpl implements LightbulbAccessory {
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));
45 public CompletableFuture<Boolean> getLightbulbPowerState() {
46 OnOffType state = getStateAs(HomekitCharacteristicType.ON_STATE, OnOffType.class);
47 return CompletableFuture.completedFuture(state == OnOffType.ON);
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);
60 return CompletableFuture.completedFuture(null);
64 public void subscribeLightbulbPowerState(HomekitCharacteristicChangeCallback callback) {
65 subscribe(HomekitCharacteristicType.ON_STATE, callback);
69 public void unsubscribeLightbulbPowerState() {
70 unsubscribe(HomekitCharacteristicType.ON_STATE);