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_02 extends F6_02 {
43 public F6_02_02(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) ? AI : BI;
55 byte dir2 = channelId.equals(CHANNEL_ROCKERSWITCH_CHANNELA) ? A0 : B0;
56 return getChannelEvent(dir1, dir2);
58 } else if (t21 && !nu) {
59 if (CHANNEL_ROCKERSWITCH_ACTION.equals(channelTypeId)) {
60 return CommonTriggerEvents.RELEASED;
62 if (lastEvent.equals(CommonTriggerEvents.DIR1_PRESSED)) {
63 return CommonTriggerEvents.DIR1_RELEASED;
64 } else if (lastEvent.equals(CommonTriggerEvents.DIR2_PRESSED)) {
65 return CommonTriggerEvents.DIR2_RELEASED;
74 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
75 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
76 if (command instanceof StringType) {
77 String s = ((StringType) command).toString();
79 if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
80 setStatus(_RPSMessage.T21_FLAG);
85 byte dir1 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? BI : AI;
86 byte dir2 = channelTypeId.equalsIgnoreCase(CHANNEL_VIRTUALROCKERSWITCHB) ? B0 : A0;
88 if (s.equals(CommonTriggerEvents.DIR1_PRESSED)) {
89 setStatus((byte) (_RPSMessage.T21_FLAG | _RPSMessage.NU_FLAG));
90 setData((byte) ((dir1 << 5) | PRESSED));
91 } else if (s.equals(CommonTriggerEvents.DIR2_PRESSED)) {
92 setStatus((byte) (_RPSMessage.T21_FLAG | _RPSMessage.NU_FLAG));
93 setData((byte) ((dir2 << 5) | PRESSED));
99 protected State convertToStateImpl(String channelId, String channelTypeId,
100 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
101 // this method is used by the classic device listener channels to convert a rocker switch message into an
102 // appropriate item update
103 State currentState = getCurrentStateFunc.apply(channelId);
104 if (t21 && nu && currentState != null) {
105 EnOceanChannelRockerSwitchListenerConfig c = config.as(EnOceanChannelRockerSwitchListenerConfig.class);
106 byte dir1 = c.getChannel() == Channel.ChannelA ? AI : BI;
107 byte dir2 = c.getChannel() == Channel.ChannelA ? A0 : B0;
109 return getState(dir1, dir2, c.handleSecondAction, c.getSwitchMode(), channelTypeId, currentState);
112 return UnDefType.UNDEF;
116 public boolean isValidForTeachIn() {
117 return false; // Never treat a message as F6-02-02, let user decide which orientation of rocker switch is used