]> git.basschouten.com Git - openhab-addons.git/blob
5199a701b14ef23609e9978bb480189d548b8beb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.enocean.internal.eep.A5_38;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.CHANNEL_DIMMER;
16 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.ZERO;
17
18 import java.util.function.Function;
19
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;
34
35 /**
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:
38  * <ul>
39  * <li>Dimming value 0-100%: standard 0-255, Eltako 0-100</li>
40  * <li>Store value: standard DB0.1, Eltako DB0.2</li>
41  * </ul>
42  *
43  * @author Daniel Weber - Initial contribution
44  */
45 @NonNullByDefault
46 public class A5_38_08_Dimming extends _4BSMessage {
47
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;
52
53     public A5_38_08_Dimming() {
54         super();
55     }
56
57     public A5_38_08_Dimming(ERP1Message packet) {
58         super(packet);
59     }
60
61     @Override
62     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command outputCommand,
63             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
64         switch (channelId) {
65             case CHANNEL_DIMMER:
66                 byte dimmValue;
67
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;
76                 } else {
77                     throw new IllegalArgumentException(outputCommand.toFullString() + " is no valid dimming command.");
78                 }
79                 if (config != null) {
80                     EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
81
82                     byte storeByte = ZERO; // "Store final value" (standard) vs. "block value" (Eltako)
83
84                     if (!c.eltakoDimmer) {
85                         dimmValue *= 2.55; // 0-100% = 0-255
86
87                         if (c.storeValue) {
88                             storeByte = 0x02; // set DB0.1
89                         }
90                     } else {
91                         if (c.storeValue) {
92                             storeByte = 0x04; // set DB0.2
93                         }
94                     }
95
96                     byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
97                     byte switchingCommand = (dimmValue == ZERO) ? SWITCH_OFF : SWITCH_ON;
98
99                     setData(COMMAND_ID, dimmValue, rampingTime, (byte) (TEACHIN_BIT | storeByte | switchingCommand));
100                 } else {
101                     logger.error("Cannot handle command {}, when configuration is null", outputCommand.toFullString());
102                 }
103
104                 break;
105         }
106     }
107
108     @Override
109     public State convertToStateImpl(String channelId, String channelTypeId,
110             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
111         switch (channelId) {
112             case CHANNEL_DIMMER:
113                 if (!getBit(getDB0(), 0)) {
114                     // Switching Command is OFF (DB0.0==0), return 0%
115                     return new PercentType(0);
116                 } else {
117                     // DB2 contains the Dimming value (absolute[0...255] or relative/Eltako [0...100])
118                     int dimmValue = getDB2Value();
119
120                     EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
121
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%]
125                         dimmValue /= 2.55;
126                     }
127
128                     return new PercentType(dimmValue);
129                 }
130         }
131
132         return UnDefType.UNDEF;
133     }
134 }