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.A5_38;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.CHANNEL_DIMMER;
16 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.ZERO;
18 import java.util.function.Function;
20 import org.openhab.binding.enocean.internal.config.EnOceanChannelDimmerConfig;
21 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.UpDownType;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
34 * This class tries to combine the classic EEP A5-38-08 CMD 0x02 dimming with the Eltako interpretation of this EEP.
35 * It is doing it by channel config parameter "eltakoDimmer". The differences are:
36 * <li>Dimming value 0-100%: standard 0-255, Eltako 0-100</li>
37 * <li>Store value: standard DB0.1, Eltako DB0.2</li>
39 * @author Daniel Weber - Initial contribution
41 public class A5_38_08_Dimming extends _4BSMessage {
43 static final byte CommandId = 0x02;
44 static final byte SwitchOff = 0x00;
45 static final byte SwitchOn = 0x01;
46 static final byte Switch100Percent = 0x64;
48 public A5_38_08_Dimming() {
52 public A5_38_08_Dimming(ERP1Message packet) {
57 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command outputCommand,
58 Function<String, State> getCurrentStateFunc, Configuration config) {
63 if (outputCommand instanceof DecimalType) {
64 dimmValue = ((DecimalType) outputCommand).byteValue();
65 } else if (outputCommand instanceof OnOffType) {
66 dimmValue = ((OnOffType) outputCommand == OnOffType.ON) ? Switch100Percent : ZERO;
67 } else if (outputCommand instanceof IncreaseDecreaseType) {
68 dimmValue = ((IncreaseDecreaseType) outputCommand == IncreaseDecreaseType.INCREASE)
71 } else if (outputCommand instanceof UpDownType) {
72 dimmValue = ((UpDownType) outputCommand == UpDownType.UP) ? Switch100Percent : ZERO;
74 throw new IllegalArgumentException(outputCommand.toFullString() + " is no valid dimming command.");
77 EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
79 byte storeByte = ZERO; // "Store final value" (standard) vs. "block value" (Eltako)
81 if (!c.eltakoDimmer) {
82 dimmValue *= 2.55; // 0-100% = 0-255
85 storeByte = 0x02; // set DB0.1
89 storeByte = 0x04; // set DB0.2
93 byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
94 byte switchingCommand = (dimmValue == ZERO) ? SwitchOff : SwitchOn;
96 setData(CommandId, dimmValue, rampingTime, (byte) (TeachInBit | storeByte | switchingCommand));
103 public State convertToStateImpl(String channelId, String channelTypeId, Function<String, State> getCurrentStateFunc,
104 Configuration config) {
107 if (!getBit(getDB_0(), 0)) {
108 // Switching Command is OFF (DB0.0==0), return 0%
109 return new PercentType(0);
111 // DB2 contains the Dimming value (absolute[0...255] or relative/Eltako [0...100])
112 int dimmValue = getDB_2Value();
114 EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
116 // if Standard dimmer and Dimming Range is absolute (DB0.2==0),
117 if (!c.eltakoDimmer && !getBit(getDB_0(), 2)) {
118 // map range [0...255] to [0%...100%]
122 return new PercentType(dimmValue);
126 return UnDefType.UNDEF;