]> git.basschouten.com Git - openhab-addons.git/blob
a4ae7a124cb46ec0b6655da156432bfe815b74fa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.items.GroupItem;
17 import org.openhab.core.items.Item;
18 import org.openhab.core.library.items.ContactItem;
19 import org.openhab.core.library.items.StringItem;
20 import org.openhab.core.library.items.SwitchItem;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.OpenClosedType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.io.homekit.internal.HomekitOHItemProxy;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Wraps either a SwitchItem or a ContactItem, interpreting the open / closed states accordingly.
31  *
32  * @author Tim Harper - Initial contribution
33  *
34  */
35 @NonNullByDefault
36 public class BooleanItemReader {
37     private final Item item;
38     private final OnOffType trueOnOffValue;
39     private final OpenClosedType trueOpenClosedValue;
40     private final Logger logger = LoggerFactory.getLogger(BooleanItemReader.class);
41
42     /**
43      *
44      * @param item The item to read
45      * @param trueOnOffValue If OnOffType, then consider true if this value
46      * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
47      */
48     BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) {
49         this.item = item;
50         this.trueOnOffValue = trueOnOffValue;
51         this.trueOpenClosedValue = trueOpenClosedValue;
52         final Item baseItem = HomekitOHItemProxy.getBaseItem(item);
53         if (!(baseItem instanceof SwitchItem) && !(baseItem instanceof ContactItem)
54                 && !(baseItem instanceof StringItem)) {
55             logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem or StringItem", item.getName(),
56                     item.getClass().getName());
57         }
58     }
59
60     boolean getValue() {
61         final State state = item.getState();
62         if (state instanceof OnOffType) {
63             return state.equals(trueOnOffValue);
64         } else if (state instanceof OpenClosedType) {
65             return state.equals(trueOpenClosedValue);
66         } else if (state instanceof StringType) {
67             return state.toString().equalsIgnoreCase("Open") || state.toString().equalsIgnoreCase("Opened");
68         } else {
69             logger.debug("Unexpected item state,  returning false. Item {}, State {}", item.getName(), state);
70             return false;
71         }
72     }
73
74     private OnOffType getOffValue(OnOffType onValue) {
75         return onValue == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
76     }
77
78     void setValue(Boolean value) {
79         if (item instanceof SwitchItem) {
80             ((SwitchItem) item).send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
81         } else if (item instanceof GroupItem) {
82             ((GroupItem) item).send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
83         } else {
84             logger.debug("Cannot set value {} for item {}. Only Switch and Group items are supported.", value, item);
85         }
86     }
87 }