]> git.basschouten.com Git - openhab-addons.git/blob
efc31cf0e4dbff10f80a38829899bea510fd1593
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.homekit.internal.accessories;
14
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.CompletableFuture;
18
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;
26
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;
36
37 /**
38  * Implements an Irrigation System accessory.
39  * 
40  * To be a complete accessory, the user must configure individual valves linked
41  * to this primary service. This class also adds the ServiceLabelService
42  * automatically.
43  *
44  * @author Cody Cutrer - Initial contribution
45  */
46 @NonNullByDefault({})
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";
51
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));
59     }
60
61     @Override
62     public void init() throws HomekitException {
63         super.init();
64
65         String serviceLabelNamespaceConfig = getAccessoryConfiguration(SERVICE_LABEL, "ARABIC_NUMERALS");
66         ServiceLabelNamespaceEnum serviceLabelEnum;
67
68         try {
69             serviceLabelEnum = ServiceLabelNamespaceEnum.valueOf(serviceLabelNamespaceConfig.toUpperCase());
70         } catch (IllegalArgumentException e) {
71             serviceLabelEnum = ServiceLabelNamespaceEnum.ARABIC_NUMERALS;
72         }
73         final var finalEnum = serviceLabelEnum;
74         var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class).orElseGet(
75                 () -> new ServiceLabelNamespaceCharacteristic(() -> CompletableFuture.completedFuture(finalEnum)));
76         getServices().add(new ServiceLabelService(serviceLabelNamespace));
77     }
78
79     @Override
80     public CompletableFuture<ActiveEnum> getActive() {
81         OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
82         return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
83     }
84
85     @Override
86     public CompletableFuture<Void> setActive(ActiveEnum value) {
87         getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
88             tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
89         });
90         return CompletableFuture.completedFuture(null);
91     }
92
93     @Override
94     public CompletableFuture<InUseEnum> getInUse() {
95         return CompletableFuture.completedFuture(
96                 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
97     }
98
99     @Override
100     public CompletableFuture<ProgramModeEnum> getProgramMode() {
101         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
102                 programModeMap, ProgramModeEnum.NO_SCHEDULED));
103     }
104
105     @Override
106     public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
107         subscribe(HomekitCharacteristicType.ACTIVE, callback);
108     }
109
110     @Override
111     public void unsubscribeActive() {
112         unsubscribe(HomekitCharacteristicType.ACTIVE);
113     }
114
115     @Override
116     public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
117         subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
118     }
119
120     @Override
121     public void unsubscribeInUse() {
122         unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
123     }
124
125     @Override
126     public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
127         subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
128     }
129
130     @Override
131     public void unsubscribeProgramMode() {
132         unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);
133     }
134 }