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.binding.touchwand.internal;
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
17 import java.util.ArrayList;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.touchwand.internal.dto.TouchWandBSensorUnitData;
21 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.binding.builder.ThingBuilder;
27 import org.openhab.core.types.Command;
30 * The {@link TouchWandBSensorHandler} is responsible for handling command for Binary Sensor unit
32 * @author Roie Geron - Initial contribution
35 public class TouchWandBSensorHandler extends TouchWandBaseUnitHandler {
37 private boolean isFirstUpdateTouchWandUnitState = true;
39 public TouchWandBSensorHandler(Thing thing) {
44 void updateTouchWandUnitState(TouchWandUnitData unitData) {
45 if (unitData instanceof TouchWandBSensorUnitData) {
46 if (isFirstUpdateTouchWandUnitState) {
47 removeUnsupportedChannels((TouchWandBSensorUnitData) unitData);
48 isFirstUpdateTouchWandUnitState = false;
50 String sensorSubType = ((TouchWandBSensorUnitData) unitData).getIdData().getSubType();
51 switch (sensorSubType) {
52 case BSENSOR_SUBTYPE_DOORWINDOW:
53 updateChannelDoorWindow((TouchWandBSensorUnitData) unitData);
55 case BSENSOR_SUBTYPE_MOTION:
56 updateChannelMotion((TouchWandBSensorUnitData) unitData);
58 case BSENSOR_SUBTYPE_SMOKE:
59 updateChannelSmoke((TouchWandBSensorUnitData) unitData);
64 logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance");
69 void touchWandUnitHandleCommand(Command command) {
72 void updateChannelDoorWindow(TouchWandBSensorUnitData unitData) {
73 OpenClosedType myOpenClose;
74 String isOpen = unitData.getCurrStatus();
75 logger.debug("recieved status {} from door unit {} ", isOpen, unitData.getName());
76 if (isOpen.equals(BSENSOR_STATUS_OPEN)) {
77 myOpenClose = OpenClosedType.OPEN;
78 } else if (isOpen.equals(BSENSOR_STATUS_CLOSE)) {
79 myOpenClose = OpenClosedType.CLOSED;
81 logger.debug("TouchWandBSensorUnitData illegal update value {}", isOpen);
84 updateState(CHANNEL_DOORWINDOW, myOpenClose);
87 void updateChannelMotion(TouchWandBSensorUnitData unitData) {
88 String motion = unitData.getCurrStatus();
89 logger.debug("recieved status {} from motion unit {} ", motion, unitData.getName());
91 if (motion.equals(BSENSOR_STATUS_OPEN)) {
92 status = OnOffType.ON;
93 } else if (motion.equals(BSENSOR_STATUS_CLOSE)) {
94 status = OnOffType.OFF;
96 logger.debug("TouchWandBSensorUnitData illegal update value {}", motion);
99 updateState(CHANNEL_MOTION, status);
102 void updateChannelSmoke(TouchWandBSensorUnitData unitData) {
103 String hasSmoke = unitData.getCurrStatus();
105 if (hasSmoke.equals(BSENSOR_STATUS_OPEN)) {
106 status = OnOffType.ON;
107 } else if (hasSmoke.equals(BSENSOR_STATUS_CLOSE)) {
108 status = OnOffType.OFF;
110 logger.debug("TouchWandBSensorUnitData illegal update value {}", hasSmoke);
113 updateState(CHANNEL_SMOKE, status);
116 void removeUnsupportedChannels(TouchWandBSensorUnitData unitData) {
117 ArrayList<Channel> toBeRemovedChannels = new ArrayList<>(thing.getChannels());
118 String sensorSubType = unitData.getIdData().getSubType();
119 switch (sensorSubType) {
120 case BSENSOR_SUBTYPE_DOORWINDOW:
121 toBeRemovedChannels.remove(thing.getChannel(CHANNEL_DOORWINDOW));
123 case BSENSOR_SUBTYPE_MOTION:
124 toBeRemovedChannels.remove(thing.getChannel(CHANNEL_MOTION));
126 case BSENSOR_SUBTYPE_SMOKE:
127 Channel channel = thing.getChannel(CHANNEL_SMOKE);
128 toBeRemovedChannels.remove(channel);
132 ThingBuilder thingBuilder = editThing();
133 thingBuilder.withoutChannels(toBeRemovedChannels);
134 updateThing(thingBuilder.build());