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 static org.openhab.io.homekit.internal.HomekitCharacteristicType.ON_STATE;
17 import java.util.List;
18 import java.util.concurrent.CompletableFuture;
20 import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
21 import org.openhab.io.homekit.internal.HomekitSettings;
22 import org.openhab.io.homekit.internal.HomekitTaggedItem;
24 import io.github.hapjava.accessories.SwitchAccessory;
25 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
26 import io.github.hapjava.services.impl.SwitchService;
29 * Implements Switch using an Item that provides an On/Off state.
31 * @author Andy Lintner - Initial contribution
33 public class HomekitSwitchImpl extends AbstractHomekitAccessoryImpl implements SwitchAccessory {
34 private final BooleanItemReader onReader;
36 public HomekitSwitchImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
37 HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
38 super(taggedItem, mandatoryCharacteristics, updater, settings);
39 onReader = createBooleanReader(ON_STATE);
40 getServices().add(new SwitchService(this));
44 public CompletableFuture<Boolean> getSwitchState() {
45 return CompletableFuture.completedFuture(onReader.getValue());
49 public CompletableFuture<Void> setSwitchState(boolean state) {
50 onReader.setValue(state);
51 return CompletableFuture.completedFuture(null);
55 public void subscribeSwitchState(HomekitCharacteristicChangeCallback callback) {
56 subscribe(ON_STATE, callback);
60 public void unsubscribeSwitchState() {
61 unsubscribe(ON_STATE);