]> git.basschouten.com Git - openhab-addons.git/blob
44450af8ea31898c553448c646e4ee32d8cf2850
[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.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;
37
38 /**
39  * Implements an Irrigation System accessory.
40  * 
41  * To be a complete accessory, the user must configure individual valves linked
42  * to this primary service. This class also adds the ServiceLabelService
43  * automatically.
44  *
45  * @author Cody Cutrer - Initial contribution
46  */
47 @NonNullByDefault({})
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";
52
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));
61     }
62
63     @Override
64     public void init() throws HomekitException {
65         super.init();
66
67         String serviceLabelNamespaceConfig = getAccessoryConfiguration(SERVICE_LABEL, "ARABIC_NUMERALS");
68         ServiceLabelNamespaceEnum serviceLabelEnum;
69
70         try {
71             serviceLabelEnum = ServiceLabelNamespaceEnum.valueOf(serviceLabelNamespaceConfig.toUpperCase());
72         } catch (IllegalArgumentException e) {
73             serviceLabelEnum = ServiceLabelNamespaceEnum.ARABIC_NUMERALS;
74         }
75         final var finalEnum = serviceLabelEnum;
76         var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class).orElseGet(
77                 () -> new ServiceLabelNamespaceCharacteristic(() -> CompletableFuture.completedFuture(finalEnum)));
78         addService(new ServiceLabelService(serviceLabelNamespace));
79     }
80
81     @Override
82     public CompletableFuture<ActiveEnum> getActive() {
83         OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
84         return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
85     }
86
87     @Override
88     public CompletableFuture<Void> setActive(ActiveEnum value) {
89         getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
90             tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
91         });
92         return CompletableFuture.completedFuture(null);
93     }
94
95     @Override
96     public CompletableFuture<InUseEnum> getInUse() {
97         return CompletableFuture.completedFuture(
98                 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
99     }
100
101     @Override
102     public CompletableFuture<ProgramModeEnum> getProgramMode() {
103         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
104                 programModeMap, ProgramModeEnum.NO_SCHEDULED));
105     }
106
107     @Override
108     public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
109         subscribe(HomekitCharacteristicType.ACTIVE, callback);
110     }
111
112     @Override
113     public void unsubscribeActive() {
114         unsubscribe(HomekitCharacteristicType.ACTIVE);
115     }
116
117     @Override
118     public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
119         subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
120     }
121
122     @Override
123     public void unsubscribeInUse() {
124         unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
125     }
126
127     @Override
128     public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
129         subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
130     }
131
132     @Override
133     public void unsubscribeProgramMode() {
134         unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);
135     }
136 }