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.satel.internal.handler;
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.satel.internal.types.StateType;
19 import org.openhab.binding.satel.internal.types.TroubleState;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.binding.builder.ChannelBuilder;
23 import org.openhab.core.thing.binding.builder.ThingBuilder;
26 * The {@link WirelessChannelsHandler} is base thing handler class for things that can be wireless devices.
27 * It adds support for two additional channels:
29 * <li><i>device_lobatt</i> - low battery indication</li>
30 * <li><i>device_nocomm</i> - communication problems indication</li>
32 * adding them if the device is configured as a wireless device.
34 * @author Krzysztof Goworek - Initial contribution
37 public abstract class WirelessChannelsHandler extends SatelStateThingHandler {
39 public WirelessChannelsHandler(Thing thing) {
44 public void initialize() {
47 withBridgeHandlerPresent(bridgeHandler -> {
48 // add/remove channels depending on whether or not the device is wireless
49 final int wirelessDeviceId = getThingConfig().getId() - bridgeHandler.getIntegraType().getOnMainboard();
50 ThingBuilder thingBuilder = editThing();
51 if (isWirelessDevice() && wirelessDeviceId > 0) {
52 // if a wireless device, remove channels for wireless devices
53 if (getChannel(TroubleState.DEVICE_LOBATT) == null) {
54 thingBuilder.withChannel(ChannelBuilder.create(getChannelUID(TroubleState.DEVICE_LOBATT), "Switch")
55 .withType(CHANNEL_TYPE_LOBATT).build());
57 if (getChannel(TroubleState.DEVICE_NOCOMM) == null) {
58 thingBuilder.withChannel(ChannelBuilder.create(getChannelUID(TroubleState.DEVICE_NOCOMM), "Switch")
59 .withType(CHANNEL_TYPE_NOCOMM).build());
62 // if not a wireless device, remove channels for wireless devices
63 thingBuilder.withoutChannel(getChannelUID(TroubleState.DEVICE_LOBATT))
64 .withoutChannel(getChannelUID(TroubleState.DEVICE_NOCOMM));
66 updateThing(thingBuilder.build());
71 * Defines the thing as a wireless or wired device.
73 * @return <code>true</code> if the thing is, or is configured as a wireless device
75 protected boolean isWirelessDevice() {
76 return getThingConfig().isWireless();
80 protected int getStateBitNbr(StateType stateType) {
81 int bitNbr = getThingConfig().getId() - 1;
82 if (stateType instanceof TroubleState) {
83 // for wireless devices we need to correct bit number
84 switch ((TroubleState) stateType) {
93 bitNbr -= getBridgeHandler().getIntegraType().getOnMainboard();
96 // other states are either not supported or don't need correction
104 protected StateType getStateType(String channelId) {
105 String stateName = channelId.toUpperCase();
106 if (TroubleState.DEVICE_LOBATT.name().equals(stateName)
107 || TroubleState.DEVICE_NOCOMM.name().equals(stateName)) {
108 if (getThingConfig().getId() - getBridgeHandler().getIntegraType().getOnMainboard() > 120) {
109 // last 120 ACU-100 devices in INTEGRA 256 PLUS
112 return TroubleState.valueOf(stateName);
114 return StateType.NONE;
119 * Returns channel UID for given state type.
121 * @param stateType state type to get channel UID for
122 * @return channel UID object
124 private ChannelUID getChannelUID(StateType stateType) {
125 String channelId = stateType.toString().toLowerCase();
126 return new ChannelUID(getThing().getUID(), channelId);