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.HomekitException;
25 import org.openhab.io.homekit.internal.HomekitSettings;
26 import org.openhab.io.homekit.internal.HomekitTaggedItem;
28 import io.github.hapjava.accessories.LightbulbAccessory;
29 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
30 import io.github.hapjava.services.impl.LightbulbService;
33 * Implements Lightbulb using an Item that provides an On/Off state
35 * @author Andy Lintner - Initial contribution
37 class HomekitLightbulbImpl extends AbstractHomekitAccessoryImpl implements LightbulbAccessory {
39 public HomekitLightbulbImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
40 HomekitAccessoryUpdater updater, HomekitSettings settings) {
41 super(taggedItem, mandatoryCharacteristics, updater, settings);
45 public void init() throws HomekitException {
47 addService(new LightbulbService(this));
51 public CompletableFuture<Boolean> getLightbulbPowerState() {
52 OnOffType state = getStateAs(HomekitCharacteristicType.ON_STATE, OnOffType.class);
53 return CompletableFuture.completedFuture(state == OnOffType.ON);
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);
66 return CompletableFuture.completedFuture(null);
70 public void subscribeLightbulbPowerState(HomekitCharacteristicChangeCallback callback) {
71 subscribe(HomekitCharacteristicType.ON_STATE, callback);
75 public void unsubscribeLightbulbPowerState() {
76 unsubscribe(HomekitCharacteristicType.ON_STATE);