]> git.basschouten.com Git - openhab-addons.git/blob
2d5771c21af587836588c6e045ca89a7e7e6e7c5
[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 decimalCommand) {
67                     dimmValue = decimalCommand.byteValue();
68                 } else if (outputCommand instanceof OnOffType onOffCommand) {
69                     dimmValue = (onOffCommand == OnOffType.ON) ? SWITCH_100_PERCENT : ZERO;
70                 } else if (outputCommand instanceof IncreaseDecreaseType increaseDecreaseCommand) {
71                     dimmValue = (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) ? SWITCH_100_PERCENT : ZERO;
72                 } else if (outputCommand instanceof UpDownType upDownCommand) {
73                     dimmValue = (upDownCommand == UpDownType.UP) ? SWITCH_100_PERCENT : ZERO;
74                 } else {
75                     throw new IllegalArgumentException(outputCommand.toFullString() + " is no valid dimming command.");
76                 }
77                 if (config != null) {
78                     EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
79
80                     byte storeByte = ZERO; // "Store final value" (standard) vs. "block value" (Eltako)
81
82                     if (!c.eltakoDimmer) {
83                         dimmValue *= 2.55; // 0-100% = 0-255
84
85                         if (c.storeValue) {
86                             storeByte = 0x02; // set DB0.1
87                         }
88                     } else {
89                         if (c.storeValue) {
90                             storeByte = 0x04; // set DB0.2
91                         }
92                     }
93
94                     byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
95                     byte switchingCommand = (dimmValue == ZERO) ? SWITCH_OFF : SWITCH_ON;
96
97                     setData(COMMAND_ID, dimmValue, rampingTime, (byte) (TEACHIN_BIT | storeByte | switchingCommand));
98                 } else {
99                     logger.error("Cannot handle command {}, when configuration is null", outputCommand.toFullString());
100                 }
101
102                 break;
103         }
104     }
105
106     @Override
107     public State convertToStateImpl(String channelId, String channelTypeId,
108             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
109         switch (channelId) {
110             case CHANNEL_DIMMER:
111                 if (!getBit(getDB0(), 0)) {
112                     // Switching Command is OFF (DB0.0==0), return 0%
113                     return new PercentType(0);
114                 } else {
115                     // DB2 contains the Dimming value (absolute[0...255] or relative/Eltako [0...100])
116                     int dimmValue = getDB2Value();
117
118                     EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
119
120                     // if Standard dimmer and Dimming Range is absolute (DB0.2==0),
121                     if (!c.eltakoDimmer && !getBit(getDB0(), 2)) {
122                         // map range [0...255] to [0%...100%]
123                         dimmValue /= 2.55;
124                     }
125
126                     return new PercentType(dimmValue);
127                 }
128         }
129
130         return UnDefType.UNDEF;
131     }
132 }