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;
52 public HomekitIrrigationSystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
53 List<Characteristic> mandatoryRawCharacteristics, HomekitAccessoryUpdater updater, HomekitSettings settings)
54 throws IncompleteAccessoryException {
55 super(taggedItem, mandatoryCharacteristics, mandatoryRawCharacteristics, updater, settings);
56 inUseMapping = createMapping(HomekitCharacteristicType.INUSE_STATUS, InUseEnum.class);
57 programModeMap = HomekitCharacteristicFactory
58 .createMapping(getCharacteristic(HomekitCharacteristicType.PROGRAM_MODE).get(), ProgramModeEnum.class);
59 addService(new IrrigationSystemService(this));
63 public void init() throws HomekitException {
66 var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class)
67 .orElseGet(() -> new ServiceLabelNamespaceCharacteristic(
68 () -> CompletableFuture.completedFuture(ServiceLabelNamespaceEnum.ARABIC_NUMERALS)));
69 addService(new ServiceLabelService(serviceLabelNamespace));
73 public CompletableFuture<ActiveEnum> getActive() {
74 OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
75 return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
79 public CompletableFuture<Void> setActive(ActiveEnum value) {
80 getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
81 tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
83 return CompletableFuture.completedFuture(null);
87 public CompletableFuture<InUseEnum> getInUse() {
88 return CompletableFuture.completedFuture(
89 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
93 public CompletableFuture<ProgramModeEnum> getProgramMode() {
94 return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
95 programModeMap, ProgramModeEnum.NO_SCHEDULED));
99 public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
100 subscribe(HomekitCharacteristicType.ACTIVE, callback);
104 public void unsubscribeActive() {
105 unsubscribe(HomekitCharacteristicType.ACTIVE);
109 public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
110 subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
114 public void unsubscribeInUse() {
115 unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
119 public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
120 subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
124 public void unsubscribeProgramMode() {
125 unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);