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.HomekitCharacteristicChangeCallback;
29 import io.github.hapjava.characteristics.impl.common.ActiveEnum;
30 import io.github.hapjava.characteristics.impl.common.InUseEnum;
31 import io.github.hapjava.characteristics.impl.common.ProgramModeEnum;
32 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceCharacteristic;
33 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceEnum;
34 import io.github.hapjava.services.impl.IrrigationSystemService;
35 import io.github.hapjava.services.impl.ServiceLabelService;
38 * Implements an Irrigation System accessory.
40 * To be a complete accessory, the user must configure individual valves linked
41 * to this primary service. This class also adds the ServiceLabelService
44 * @author Cody Cutrer - Initial contribution
47 public class HomekitIrrigationSystemImpl extends AbstractHomekitAccessoryImpl implements IrrigationSystemAccessory {
48 private Map<InUseEnum, String> inUseMapping;
49 private Map<ProgramModeEnum, String> programModeMap;
50 private static final String SERVICE_LABEL = "ServiceLabel";
52 public HomekitIrrigationSystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
53 HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
54 super(taggedItem, mandatoryCharacteristics, updater, settings);
55 inUseMapping = createMapping(HomekitCharacteristicType.INUSE_STATUS, InUseEnum.class);
56 programModeMap = HomekitCharacteristicFactory
57 .createMapping(getCharacteristic(HomekitCharacteristicType.PROGRAM_MODE).get(), ProgramModeEnum.class);
58 getServices().add(new IrrigationSystemService(this));
62 public void init() throws HomekitException {
65 String serviceLabelNamespaceConfig = getAccessoryConfiguration(SERVICE_LABEL, "ARABIC_NUMERALS");
66 ServiceLabelNamespaceEnum serviceLabelEnum;
69 serviceLabelEnum = ServiceLabelNamespaceEnum.valueOf(serviceLabelNamespaceConfig.toUpperCase());
70 } catch (IllegalArgumentException e) {
71 serviceLabelEnum = ServiceLabelNamespaceEnum.ARABIC_NUMERALS;
73 final var finalEnum = serviceLabelEnum;
74 var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class).orElseGet(
75 () -> new ServiceLabelNamespaceCharacteristic(() -> CompletableFuture.completedFuture(finalEnum)));
76 getServices().add(new ServiceLabelService(serviceLabelNamespace));
80 public CompletableFuture<ActiveEnum> getActive() {
81 OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
82 return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
86 public CompletableFuture<Void> setActive(ActiveEnum value) {
87 getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
88 tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
90 return CompletableFuture.completedFuture(null);
94 public CompletableFuture<InUseEnum> getInUse() {
95 return CompletableFuture.completedFuture(
96 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
100 public CompletableFuture<ProgramModeEnum> getProgramMode() {
101 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
102 programModeMap, ProgramModeEnum.NO_SCHEDULED));
106 public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
107 subscribe(HomekitCharacteristicType.ACTIVE, callback);
111 public void unsubscribeActive() {
112 unsubscribe(HomekitCharacteristicType.ACTIVE);
116 public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
117 subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
121 public void unsubscribeInUse() {
122 unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
126 public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
127 subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
131 public void unsubscribeProgramMode() {
132 unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);