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;
17 import java.util.concurrent.CompletableFuture;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
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.HomekitSettings;
24 import org.openhab.io.homekit.internal.HomekitTaggedItem;
26 import io.github.hapjava.accessories.IrrigationSystemAccessory;
27 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.characteristics.impl.common.ActiveEnum;
29 import io.github.hapjava.characteristics.impl.common.InUseEnum;
30 import io.github.hapjava.characteristics.impl.common.ProgramModeEnum;
31 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceCharacteristic;
32 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceEnum;
33 import io.github.hapjava.services.impl.IrrigationSystemService;
34 import io.github.hapjava.services.impl.ServiceLabelService;
37 * Implements an Irrigation System accessory.
39 * To be a complete accessory, the user must configure individual valves linked
40 * to this primary service. This class also adds the ServiceLabelService
43 * @author Cody Cutrer - Initial contribution
46 public class HomekitIrrigationSystemImpl extends AbstractHomekitAccessoryImpl implements IrrigationSystemAccessory {
47 private Map<InUseEnum, String> inUseMapping;
48 private Map<ProgramModeEnum, String> programModeMap;
49 private static final String SERVICE_LABEL = "ServiceLabel";
51 public HomekitIrrigationSystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
52 HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
53 super(taggedItem, mandatoryCharacteristics, updater, settings);
54 inUseMapping = createMapping(HomekitCharacteristicType.INUSE_STATUS, InUseEnum.class);
55 programModeMap = HomekitCharacteristicFactory
56 .createMapping(getCharacteristic(HomekitCharacteristicType.PROGRAM_MODE).get(), ProgramModeEnum.class);
57 getServices().add(new IrrigationSystemService(this));
62 String serviceLabelNamespaceConfig = getAccessoryConfiguration(SERVICE_LABEL, "ARABIC_NUMERALS");
63 ServiceLabelNamespaceEnum serviceLabelEnum;
66 serviceLabelEnum = ServiceLabelNamespaceEnum.valueOf(serviceLabelNamespaceConfig.toUpperCase());
67 } catch (IllegalArgumentException e) {
68 serviceLabelEnum = ServiceLabelNamespaceEnum.ARABIC_NUMERALS;
70 final var finalEnum = serviceLabelEnum;
71 var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class).orElseGet(
72 () -> new ServiceLabelNamespaceCharacteristic(() -> CompletableFuture.completedFuture(finalEnum)));
73 getServices().add(new ServiceLabelService(serviceLabelNamespace));
77 public CompletableFuture<ActiveEnum> getActive() {
78 OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
79 return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
83 public CompletableFuture<Void> setActive(ActiveEnum value) {
84 getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
85 tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
87 return CompletableFuture.completedFuture(null);
91 public CompletableFuture<InUseEnum> getInUse() {
92 return CompletableFuture.completedFuture(
93 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
97 public CompletableFuture<ProgramModeEnum> getProgramMode() {
98 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
99 programModeMap, ProgramModeEnum.NO_SCHEDULED));
103 public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
104 subscribe(HomekitCharacteristicType.ACTIVE, callback);
108 public void unsubscribeActive() {
109 unsubscribe(HomekitCharacteristicType.ACTIVE);
113 public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
114 subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
118 public void unsubscribeInUse() {
119 unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
123 public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
124 subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
128 public void unsubscribeProgramMode() {
129 unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);