2 * Copyright (c) 2010-2023 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.PercentType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.types.State;
32 import org.openhab.io.homekit.internal.HomekitOHItemProxy;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Wraps either a SwitchItem or a ContactItem, interpreting the open / closed states accordingly.
39 * @author Tim Harper - Initial contribution
43 public class BooleanItemReader {
44 private final Item item;
45 private final OnOffType trueOnOffValue;
46 private final OpenClosedType trueOpenClosedValue;
47 private final Logger logger = LoggerFactory.getLogger(BooleanItemReader.class);
48 private final @Nullable BigDecimal trueThreshold;
49 private final boolean invertThreshold;
53 * @param item The item to read
54 * @param trueOnOffValue If OnOffType, then consider true if this value
55 * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
57 BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) {
58 this(item, trueOnOffValue, trueOpenClosedValue, null, false);
63 * @param item The item to read
64 * @param trueOnOffValue If OnOffType, then consider true if this value
65 * @param trueOpenClosedValue if OpenClosedType, then consider true if this value
66 * @param trueThreshold If the state is numeric, and this param is given, return true if the value is above this
68 * @param invertThreshold Invert threshold to be true if below, not above
70 BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue,
71 @Nullable BigDecimal trueThreshold, boolean invertThreshold) {
73 this.trueOnOffValue = trueOnOffValue;
74 this.trueOpenClosedValue = trueOpenClosedValue;
75 final Item baseItem = HomekitOHItemProxy.getBaseItem(item);
76 this.trueThreshold = trueThreshold;
77 this.invertThreshold = invertThreshold;
78 if (!(baseItem instanceof SwitchItem || baseItem instanceof ContactItem || baseItem instanceof StringItem
79 || (trueThreshold != null && baseItem instanceof NumberItem))) {
80 if (trueThreshold != null) {
81 logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem, NumberItem or StringItem",
82 item.getName(), item.getClass().getSimpleName());
84 logger.warn("Item {} is a {} instead of the expected SwitchItem, ContactItem or StringItem",
85 item.getName(), item.getClass().getSimpleName());
91 State state = item.getState();
92 final BigDecimal localTrueThresheold = trueThreshold;
93 if (state instanceof PercentType) {
94 state = state.as(OnOffType.class);
96 if (state instanceof OnOffType) {
97 return state.equals(trueOnOffValue);
98 } else if (state instanceof OpenClosedType) {
99 return state.equals(trueOpenClosedValue);
100 } else if (state instanceof StringType) {
101 return "Open".equalsIgnoreCase(state.toString()) || "Opened".equalsIgnoreCase(state.toString());
102 } else if (localTrueThresheold != null) {
103 if (state instanceof DecimalType stateAsDecimalType) {
104 final boolean result = stateAsDecimalType.toBigDecimal().compareTo(localTrueThresheold) > 0;
105 return result ^ invertThreshold;
106 } else if (state instanceof QuantityType stateAsQuantityType) {
107 final boolean result = stateAsQuantityType.toBigDecimal().compareTo(localTrueThresheold) > 0;
108 return result ^ invertThreshold;
111 logger.debug("Unexpected item state, returning false. Item {}, State {} ({})", item.getName(), state,
112 state.getClass().getSimpleName());
116 private OnOffType getOffValue(OnOffType onValue) {
117 return OnOffType.from(onValue != OnOffType.ON);
120 void setValue(Boolean value) {
121 if (item instanceof SwitchItem switchItem) {
122 switchItem.send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
123 } else if (item instanceof GroupItem groupItem) {
124 groupItem.send(value ? trueOnOffValue : getOffValue(trueOnOffValue));
126 logger.debug("Cannot set value {} for item {}. Only Switch and Group items are supported.", value, item);