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.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.enocean.internal.config.EnOceanChannelDimmerConfig;
23 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
24 import org.openhab.binding.enocean.internal.messages.ERP1Message;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.UpDownType;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
36 * This class tries to combine the classic EEP A5-38-08 CMD 0x02 dimming with the Eltako interpretation of this EEP.
37 * It is doing it by channel config parameter "eltakoDimmer". The differences are:
39 * <li>Dimming value 0-100%: standard 0-255, Eltako 0-100</li>
40 * <li>Store value: standard DB0.1, Eltako DB0.2</li>
43 * @author Daniel Weber - Initial contribution
46 public class A5_38_08_Dimming extends _4BSMessage {
48 static final byte COMMAND_ID = 0x02;
49 static final byte SWITCH_OFF = 0x00;
50 static final byte SWITCH_ON = 0x01;
51 static final byte SWITCH_100_PERCENT = 0x64;
53 public A5_38_08_Dimming() {
57 public A5_38_08_Dimming(ERP1Message packet) {
62 protected void convertFromCommandImpl(String channelId, String channelTypeId, Command outputCommand,
63 Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
68 if (outputCommand instanceof DecimalType decimalCommand) {
69 dimmValue = decimalCommand.byteValue();
70 } else if (outputCommand instanceof OnOffType onOffCommand) {
71 dimmValue = (onOffCommand == OnOffType.ON) ? SWITCH_100_PERCENT : ZERO;
72 } else if (outputCommand instanceof IncreaseDecreaseType increaseDecreaseCommand) {
73 dimmValue = (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) ? SWITCH_100_PERCENT : ZERO;
74 } else if (outputCommand instanceof UpDownType upDownCommand) {
75 dimmValue = (upDownCommand == UpDownType.UP) ? SWITCH_100_PERCENT : ZERO;
77 throw new IllegalArgumentException(outputCommand.toFullString() + " is no valid dimming command.");
80 EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
82 byte storeByte = ZERO; // "Store final value" (standard) vs. "block value" (Eltako)
84 if (!c.eltakoDimmer) {
85 dimmValue *= 2.55; // 0-100% = 0-255
88 storeByte = 0x02; // set DB0.1
92 storeByte = 0x04; // set DB0.2
96 byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
97 byte switchingCommand = (dimmValue == ZERO) ? SWITCH_OFF : SWITCH_ON;
99 setData(COMMAND_ID, dimmValue, rampingTime, (byte) (TEACHIN_BIT | storeByte | switchingCommand));
101 logger.error("Cannot handle command {}, when configuration is null", outputCommand.toFullString());
109 public State convertToStateImpl(String channelId, String channelTypeId,
110 Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
113 if (!getBit(getDB0(), 0)) {
114 // Switching Command is OFF (DB0.0==0), return 0%
115 return new PercentType(0);
117 // DB2 contains the Dimming value (absolute[0...255] or relative/Eltako [0...100])
118 int dimmValue = getDB2Value();
120 EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
122 // if Standard dimmer and Dimming Range is absolute (DB0.2==0),
123 if (!c.eltakoDimmer && !getBit(getDB0(), 2)) {
124 // map range [0...255] to [0%...100%]
128 return new PercentType(dimmValue);
132 return UnDefType.UNDEF;