]> git.basschouten.com Git - openhab-addons.git/blob
148b5fc70389b65396a235a89f805e874d6c69d2
[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  * <li>Dimming value 0-100%: standard 0-255, Eltako 0-100</li>
39  * <li>Store value: standard DB0.1, Eltako DB0.2</li>
40  *
41  * @author Daniel Weber - Initial contribution
42  */
43 @NonNullByDefault
44 public class A5_38_08_Dimming extends _4BSMessage {
45
46     static final byte COMMAND_ID = 0x02;
47     static final byte SWITCH_OFF = 0x00;
48     static final byte SWITCH_ON = 0x01;
49     static final byte SWITCH_100_PERCENT = 0x64;
50
51     public A5_38_08_Dimming() {
52         super();
53     }
54
55     public A5_38_08_Dimming(ERP1Message packet) {
56         super(packet);
57     }
58
59     @Override
60     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command outputCommand,
61             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
62         switch (channelId) {
63             case CHANNEL_DIMMER:
64                 byte dimmValue;
65
66                 if (outputCommand instanceof DecimalType) {
67                     dimmValue = ((DecimalType) outputCommand).byteValue();
68                 } else if (outputCommand instanceof OnOffType) {
69                     dimmValue = ((OnOffType) outputCommand == OnOffType.ON) ? SWITCH_100_PERCENT : ZERO;
70                 } else if (outputCommand instanceof IncreaseDecreaseType) {
71                     dimmValue = ((IncreaseDecreaseType) outputCommand == IncreaseDecreaseType.INCREASE)
72                             ? SWITCH_100_PERCENT
73                             : ZERO;
74                 } else if (outputCommand instanceof UpDownType) {
75                     dimmValue = ((UpDownType) outputCommand == 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 }