2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal.accessories;
15 import java.math.BigDecimal;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.items.GroupItem;
20 import org.openhab.core.items.Item;
21 import org.openhab.core.library.items.ContactItem;
22 import org.openhab.core.library.items.NumberItem;
23 import org.openhab.core.library.items.StringItem;
24 import org.openhab.core.library.items.SwitchItem;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.types.State;
31 import org.openhab.io.homekit.internal.HomekitOHItemProxy;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Wraps either a SwitchItem or a ContactItem, interpreting the open / closed states accordingly.
38 * @author Tim Harper - Initial contribution
42 public class BooleanItemReader {
43 private final Item item;
44 private final OnOffType trueOnOffValue;
45 private final OpenClosedType trueOpenClosedValue;
46 private final Logger logger = LoggerFactory.getLogger(BooleanItemReader.class);
47 private final @Nullable BigDecimal trueThreshold;
48 private final boolean invertThreshold;
52 * @param item The item to read
53 * @param trueOnOffValue If OnOffType, then consider true if this value
54 * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
56 BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) {
57 this(item, trueOnOffValue, trueOpenClosedValue, null, false);
62 * @param item The item to read
63 * @param trueOnOffValue If OnOffType, then consider true if this value
64 * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
65 * @param trueThreshold If the state is numeric, and this param is given, return true if the value is above this
67 * @param invertThreshold Invert threshold to be true if below, not above
69 BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue,
70 @Nullable BigDecimal trueThreshold, boolean invertThreshold) {
72 this.trueOnOffValue = trueOnOffValue;
73 this.trueOpenClosedValue = trueOpenClosedValue;
74 final Item baseItem = HomekitOHItemProxy.getBaseItem(item);
75 this.trueThreshold = trueThreshold;
76 this.invertThreshold = invertThreshold;
77 if (!(baseItem instanceof SwitchItem || baseItem instanceof ContactItem || baseItem instanceof StringItem
78 || (trueThreshold != null && baseItem instanceof NumberItem))) {
79 if (trueThreshold != null) {
80 logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem, NumberItem or StringItem",
81 item.getName(), item.getClass().getName());
83 logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem or StringItem",
84 item.getName(), item.getClass().getName());
90 final State state = item.getState();
91 final BigDecimal localTrueThresheold = trueThreshold;
92 if (state instanceof OnOffType) {
93 return state.equals(trueOnOffValue);
94 } else if (state instanceof OpenClosedType) {
95 return state.equals(trueOpenClosedValue);
96 } else if (state instanceof StringType) {
97 return state.toString().equalsIgnoreCase("Open") || state.toString().equalsIgnoreCase("Opened");
98 } else if (localTrueThresheold != null) {
99 if (state instanceof DecimalType) {
100 final boolean result = ((DecimalType) state).toBigDecimal().compareTo(localTrueThresheold) > 0;
101 return result ^ invertThreshold;
102 } else if (state instanceof QuantityType) {
103 final boolean result = ((QuantityType<?>) state).toBigDecimal().compareTo(localTrueThresheold) > 0;
104 return result ^ invertThreshold;
107 logger.debug("Unexpected item state, returning false. Item {}, State {}", item.getName(), state);
111 private OnOffType getOffValue(OnOffType onValue) {
112 return onValue == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
115 void setValue(Boolean value) {
116 if (item instanceof SwitchItem) {
117 ((SwitchItem) item).send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
118 } else if (item instanceof GroupItem) {
119 ((GroupItem) item).send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
121 logger.debug("Cannot set value {} for item {}. Only Switch and Group items are supported.", value, item);