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.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchConfigBase.Channel;
20 import org.openhab.binding.enocean.internal.config.EnOceanChannelRockerSwitchListenerConfig;
21 import org.openhab.binding.enocean.internal.eep.Base._RPSMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.CommonTriggerEvents;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
32 * @author Daniel Weber - Initial contribution
34 public class F6_02_01 extends F6_02 {
40 public F6_02_01(ERP1Message packet) {
45 protected String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
46 Configuration config) {
49 if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
50 return getRockerSwitchAction(config);
52 byte dir1 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
53 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? AI : BI;
55 return getChannelEvent(dir1, dir2);
57 } else if (t21 && !nu) {
58 if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
59 return CommonTriggerEvents.RELEASED;
61 if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
62 return CommonTriggerEvents.DIR1_RELEASED;
63 } else if (lastEvent != null && lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
64 return CommonTriggerEvents.DIR2_RELEASED;
73 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
74 Function<String, State> getCurrentStateFunc, Configuration config) {
75 if (command instanceof StringType) {
76 String s = ((StringType) command).toString();
78 if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
79 setStatus(_RPSMessage.T21Flag);
84 byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
85 byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
87 if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
88 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
89 setData((byte) ((dir1 << 5) | PRESSED));
90 } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
91 setStatus((byte) (_RPSMessage.T21Flag | _RPSMessage.NUFlag));
92 setData((byte) ((dir2 << 5) | PRESSED));
98 protected State convertToStateImpl(String channelId, String channelTypeId,
99 Function<String, State> getCurrentStateFunc, Configuration config) {
100 // this method is used by the classic device listener channels to convert a rocker switch message into an
101 // appropriate item update
102 State currentState = getCurrentStateFunc.apply(channelId);
104 EnOceanChannelRockerSwitchListenerConfig c = config.as(EnOceanChannelRockerSwitchListenerConfig.class);
105 byte dir1 = c.getChannel() == Channel.ChannelA ? A0 : B0;
106 byte dir2 = c.getChannel() == Channel.ChannelA ? AI : BI;
108 return getState(dir1, dir2, c.handleSecondAction, c.getSwitchMode(), channelTypeId, currentState);
111 return UnDefType.UNDEF;
115 public boolean isValidForTeachIn() {
117 // just treat press as teach in => DB0.4 has to be set
118 if (!getBit(bytes[0], 4)) {
121 // DB0.7 is never set for rocker switch message
122 if (getBit(bytes[0], 7)) {