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.HomekitException;
24 import org.openhab.io.homekit.internal.HomekitSettings;
25 import org.openhab.io.homekit.internal.HomekitTaggedItem;
27 import io.github.hapjava.accessories.IrrigationSystemAccessory;
28 import io.github.hapjava.characteristics.Characteristic;
29 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
30 import io.github.hapjava.characteristics.impl.common.ActiveEnum;
31 import io.github.hapjava.characteristics.impl.common.InUseEnum;
32 import io.github.hapjava.characteristics.impl.common.ProgramModeEnum;
33 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceCharacteristic;
34 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceEnum;
35 import io.github.hapjava.services.impl.IrrigationSystemService;
36 import io.github.hapjava.services.impl.ServiceLabelService;
39 * Implements an Irrigation System accessory.
41 * To be a complete accessory, the user must configure individual valves linked
42 * to this primary service. This class also adds the ServiceLabelService
45 * @author Cody Cutrer - Initial contribution
48 public class HomekitIrrigationSystemImpl extends AbstractHomekitAccessoryImpl implements IrrigationSystemAccessory {
49 private Map<InUseEnum, String> inUseMapping;
50 private Map<ProgramModeEnum, String> programModeMap;
51 private static final String SERVICE_LABEL = "ServiceLabel";
53 public HomekitIrrigationSystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
54 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater, HomekitSettings settings)
55 throws IncompleteAccessoryException {
56 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
57 inUseMapping = createMapping(HomekitCharacteristicType.INUSE_STATUS, InUseEnum.class);
58 programModeMap = HomekitCharacteristicFactory
59 .createMapping(getCharacteristic(HomekitCharacteristicType.PROGRAM_MODE).get(), ProgramModeEnum.class);
60 addService(new IrrigationSystemService(this));
64 public void init() throws HomekitException {
67 String serviceLabelNamespaceConfig = getAccessoryConfiguration(SERVICE_LABEL, "ARABIC_NUMERALS");
68 ServiceLabelNamespaceEnum serviceLabelEnum;
71 serviceLabelEnum = ServiceLabelNamespaceEnum.valueOf(serviceLabelNamespaceConfig.toUpperCase());
72 } catch (IllegalArgumentException e) {
73 serviceLabelEnum = ServiceLabelNamespaceEnum.ARABIC_NUMERALS;
75 final var finalEnum = serviceLabelEnum;
76 var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class).orElseGet(
77 () -> new ServiceLabelNamespaceCharacteristic(() -> CompletableFuture.completedFuture(finalEnum)));
78 addService(new ServiceLabelService(serviceLabelNamespace));
82 public CompletableFuture<ActiveEnum> getActive() {
83 OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
84 return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
88 public CompletableFuture<Void> setActive(ActiveEnum value) {
89 getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
90 tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
92 return CompletableFuture.completedFuture(null);
96 public CompletableFuture<InUseEnum> getInUse() {
97 return CompletableFuture.completedFuture(
98 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
102 public CompletableFuture<ProgramModeEnum> getProgramMode() {
103 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
104 programModeMap, ProgramModeEnum.NO_SCHEDULED));
108 public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
109 subscribe(HomekitCharacteristicType.ACTIVE, callback);
113 public void unsubscribeActive() {
114 unsubscribe(HomekitCharacteristicType.ACTIVE);
118 public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
119 subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
123 public void unsubscribeInUse() {
124 unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
128 public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
129 subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
133 public void unsubscribeProgramMode() {
134 unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);