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 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;
30 * Wraps either a SwitchItem or a ContactItem, interpreting the open / closed states accordingly.
32 * @author Tim Harper - Initial contribution
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);
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
48 BooleanItemReader(Item item, OnOffType trueOnOffValue, OpenClosedType trueOpenClosedValue) {
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());
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");
69 logger.debug("Unexpected item state, returning false. Item {}, State {}", item.getName(), state);
74 private OnOffType getOffValue(OnOffType onValue) {
75 return onValue == OnOffType.ON ? OnOffType.OFF : OnOffType.ON;
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));
84 logger.debug("Cannot set value {} for item {}. Only Switch and Group items are supported.", value, item);