]> git.basschouten.com Git - openhab-addons.git/blob
5b75f2de67543ad12df9103224834e288f891d5c
[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
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));
60     }
61
62     @Override
63     public void init() throws HomekitException {
64         super.init();
65
66         var serviceLabelNamespace = getCharacteristic(ServiceLabelNamespaceCharacteristic.class)
67                 .orElseGet(() -> new ServiceLabelNamespaceCharacteristic(
68                         () -> CompletableFuture.completedFuture(ServiceLabelNamespaceEnum.ARABIC_NUMERALS)));
69         addService(new ServiceLabelService(serviceLabelNamespace));
70     }
71
72     @Override
73     public CompletableFuture<ActiveEnum> getActive() {
74         OnOffType state = getStateAs(HomekitCharacteristicType.ACTIVE, OnOffType.class);
75         return CompletableFuture.completedFuture(state == OnOffType.ON ? ActiveEnum.ACTIVE : ActiveEnum.INACTIVE);
76     }
77
78     @Override
79     public CompletableFuture<Void> setActive(ActiveEnum value) {
80         getCharacteristic(HomekitCharacteristicType.ACTIVE).ifPresent(tItem -> {
81             tItem.send(OnOffType.from(value == ActiveEnum.ACTIVE));
82         });
83         return CompletableFuture.completedFuture(null);
84     }
85
86     @Override
87     public CompletableFuture<InUseEnum> getInUse() {
88         return CompletableFuture.completedFuture(
89                 getKeyFromMapping(HomekitCharacteristicType.INUSE_STATUS, inUseMapping, InUseEnum.NOT_IN_USE));
90     }
91
92     @Override
93     public CompletableFuture<ProgramModeEnum> getProgramMode() {
94         return CompletableFuture.completedFuture(getKeyFromMapping(HomekitCharacteristicType.PROGRAM_MODE,
95                 programModeMap, ProgramModeEnum.NO_SCHEDULED));
96     }
97
98     @Override
99     public void subscribeActive(HomekitCharacteristicChangeCallback callback) {
100         subscribe(HomekitCharacteristicType.ACTIVE, callback);
101     }
102
103     @Override
104     public void unsubscribeActive() {
105         unsubscribe(HomekitCharacteristicType.ACTIVE);
106     }
107
108     @Override
109     public void subscribeInUse(HomekitCharacteristicChangeCallback callback) {
110         subscribe(HomekitCharacteristicType.INUSE_STATUS, callback);
111     }
112
113     @Override
114     public void unsubscribeInUse() {
115         unsubscribe(HomekitCharacteristicType.INUSE_STATUS);
116     }
117
118     @Override
119     public void subscribeProgramMode(HomekitCharacteristicChangeCallback callback) {
120         subscribe(HomekitCharacteristicType.PROGRAM_MODE, callback);
121     }
122
123     @Override
124     public void unsubscribeProgramMode() {
125         unsubscribe(HomekitCharacteristicType.PROGRAM_MODE);
126     }
127 }