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.enocean.internal.eep.F6_02;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchConfigBase.Channel;
22 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchListenerConfig;
23 import org.openhab.binding.enocean.internal.eep.Base._RPSMessage;
24 import org.openhab.binding.enocean.internal.messages.ERP1Message;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.thing.CommonTriggerEvents;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
34 * @author Daniel Weber - Initial contribution
37 public class F6_02_01 extends F6_02 {
43 public F6_02_01(ERP1Message packet) {
48 protected @Nullable String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
49 Configuration config) {
51 if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
52 return getRockerSwitchAction(config);
54 byte dir1 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
55 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? AI : BI;
57 return getChannelEvent(dir1, dir2);
59 } else if (t21 && !nu) {
60 if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
61 return CommonTriggerEvents.RELEASED;
63 if (lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
64 return CommonTriggerEvents.DIR1_RELEASED;
65 } else if (lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
66 return CommonTriggerEvents.DIR2_RELEASED;
75 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
76 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
77 if (command instanceof StringType) {
78 String s = ((StringType) command).toString();
80 if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
81 setStatus(_RPSMessage.T21_FLAG);
86 byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
87 byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
89 if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
90 setStatus((byte) (_RPSMessage.T21_FLAG | _RPSMessage.NU_FLAG));
91 setData((byte) ((dir1 << 5) | PRESSED));
92 } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
93 setStatus((byte) (_RPSMessage.T21_FLAG | _RPSMessage.NU_FLAG));
94 setData((byte) ((dir2 << 5) | PRESSED));
100 protected State convertToStateImpl(String channelId, String channelTypeId,
101 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
102 // this method is used by the classic device listener channels to convert a rocker switch message into an
103 // appropriate item update
104 State currentState = getCurrentStateFunc.apply(channelId);
105 if (t21 && nu && currentState != null) {
106 EnOceanChannelRockerSwitchListenerConfig c = config.as(EnOceanChannelRockerSwitchListenerConfig.class);
107 byte dir1 = c.getChannel() == Channel.ChannelA ? A0 : B0;
108 byte dir2 = c.getChannel() == Channel.ChannelA ? AI : BI;
110 return getState(dir1, dir2, c.handleSecondAction, c.getSwitchMode(), channelTypeId, currentState);
113 return UnDefType.UNDEF;
117 public boolean isValidForTeachIn() {
119 // just treat press as teach in => DB0.4 has to be set
120 if (!getBit(bytes[0], 4)) {
123 // DB0.7 is never set for rocker switch message
124 if (getBit(bytes[0], 7)) {