]> git.basschouten.com Git - openhab-addons.git/blob
e505e49883e010eac76bb59722978616863f6266
[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.HomekitSettings;
24 import org.openhab.io.homekit.internal.HomekitTaggedItem;
25
26 import io.github.hapjava.accessories.IrrigationSystemAccessory;
27 import io.github.hapjava.characteristics.HomekitCharacteristicChangeCallback;
28 import io.github.hapjava.characteristics.impl.common.ActiveEnum;
29 import io.github.hapjava.characteristics.impl.common.InUseEnum;
30 import io.github.hapjava.characteristics.impl.common.ProgramModeEnum;
31 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceCharacteristic;
32 import io.github.hapjava.characteristics.impl.common.ServiceLabelNamespaceEnum;
33 import io.github.hapjava.services.impl.IrrigationSystemService;
34 import io.github.hapjava.services.impl.ServiceLabelService;
35
36 /**
37  * Implements an Irrigation System accessory.
38  * 
39  * To be a complete accessory, the user must configure individual valves linked
40  * to this primary service. This class also adds the ServiceLabelService
41  * automatically.
42  *
43  * @author Cody Cutrer - Initial contribution
44  */
45 @NonNullByDefault({})
46 public class HomekitIrrigationSystemImpl extends AbstractHomekitAccessoryImpl implements IrrigationSystemAccessory {
47     private Map<InUseEnum, String> inUseMapping;
48     private Map<ProgramModeEnum, String> programModeMap;
49     private static final String SERVICE_LABEL = "ServiceLabel";
50
51     public HomekitIrrigationSystemImpl(HomekitTaggedItem taggedItem, List<HomekitTaggedItem> mandatoryCharacteristics,
52             HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
53         super(taggedItem, mandatoryCharacteristics, updater, settings);
54         inUseMapping = createMapping(HomekitCharacteristicType.INUSE_STATUS, InUseEnum.class);
55         programModeMap = HomekitCharacteristicFactory
56                 .createMapping(getCharacteristic(HomekitCharacteristicType.PROGRAM_MODE).get(), ProgramModeEnum.class);
57         getServices().add(new IrrigationSystemService(this));
58     }
59
60     @Override
61     public void init() {
62         String serviceLabelNamespaceConfig = getAccessoryConfiguration(SERVICE_LABEL, "ARABIC_NUMERALS");
63         ServiceLabelNamespaceEnum serviceLabelEnum;
64
65         try {
66             serviceLabelEnum = ServiceLabelNamespaceEnum.valueOf(serviceLabelNamespaceConfig.toUpperCase());
67         } catch (IllegalArgumentException e) {
68             serviceLabelEnum = ServiceLabelNamespaceEnum.ARABIC_NUMERALS;
69         }
70         final var finalEnum = serviceLabelEnum;
71         var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class).orElseGet(
72                 () -> new ServiceLabelNamespaceCharacteristic(() -> CompletableFuture.completedFuture(finalEnum)));
73         getServices().add(new ServiceLabelService(serviceLabelNamespace));
74     }
75
76     @Override
77     public CompletableFuture<ActiveEnum> getActive() {
78         OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
79         return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
80     }
81
82     @Override
83     public CompletableFuture<Void> setActive(ActiveEnum value) {
84         getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
85             tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
86         });
87         return CompletableFuture.completedFuture(null);
88     }
89
90     @Override
91     public CompletableFuture<InUseEnum> getInUse() {
92         return CompletableFuture.completedFuture(
93                 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
94     }
95
96     @Override
97     public CompletableFuture<ProgramModeEnum> getProgramMode() {
98         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
99                 programModeMap, ProgramModeEnum.NO_SCHEDULED));
100     }
101
102     @Override
103     public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
104         subscribe(HomekitCharacteristicType.ACTIVE, callback);
105     }
106
107     @Override
108     public void unsubscribeActive() {
109         unsubscribe(HomekitCharacteristicType.ACTIVE);
110     }
111
112     @Override
113     public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
114         subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
115     }
116
117     @Override
118     public void unsubscribeInUse() {
119         unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
120     }
121
122     @Override
123     public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
124         subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
125     }
126
127     @Override
128     public void unsubscribeProgramMode() {
129         unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);
130     }
131 }