]> git.basschouten.com Git - openhab-addons.git/blob
058291541fa5b98b52c67fdf5c9133cadd32b5a3
[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.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;
32
33 /**
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>
38  *
39  * @author Daniel Weber - Initial contribution
40  */
41 public class A5_38_08_Dimming extends _4BSMessage {
42
43     static final byte CommandId = 0x02;
44     static final byte SwitchOff = 0x00;
45     static final byte SwitchOn = 0x01;
46     static final byte Switch100Percent = 0x64;
47
48     public A5_38_08_Dimming() {
49         super();
50     }
51
52     public A5_38_08_Dimming(ERP1Message packet) {
53         super(packet);
54     }
55
56     @Override
57     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command outputCommand,
58             Function<String, State> getCurrentStateFunc, Configuration config) {
59         switch (channelId) {
60             case CHANNEL_DIMMER:
61                 byte dimmValue;
62
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)
69                             ? Switch100Percent
70                             : ZERO;
71                 } else if (outputCommand instanceof UpDownType) {
72                     dimmValue = ((UpDownType) outputCommand == UpDownType.UP) ? Switch100Percent : ZERO;
73                 } else {
74                     throw new IllegalArgumentException(outputCommand.toFullString() + " is no valid dimming command.");
75                 }
76
77                 EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
78
79                 byte storeByte = ZERO; // "Store final value" (standard) vs. "block value" (Eltako)
80
81                 if (!c.eltakoDimmer) {
82                     dimmValue *= 2.55; // 0-100% = 0-255
83
84                     if (c.storeValue) {
85                         storeByte = 0x02; // set DB0.1
86                     }
87                 } else {
88                     if (c.storeValue) {
89                         storeByte = 0x04; // set DB0.2
90                     }
91                 }
92
93                 byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
94                 byte switchingCommand = (dimmValue == ZERO) ? SwitchOff : SwitchOn;
95
96                 setData(CommandId, dimmValue, rampingTime, (byte) (TeachInBit | storeByte | switchingCommand));
97
98                 break;
99         }
100     }
101
102     @Override
103     public State convertToStateImpl(String channelId, String channelTypeId, Function<String, State> getCurrentStateFunc,
104             Configuration config) {
105         switch (channelId) {
106             case CHANNEL_DIMMER:
107                 if (!getBit(getDB_0(), 0)) {
108                     // Switching Command is OFF (DB0.0==0), return 0%
109                     return new PercentType(0);
110                 } else {
111                     // DB2 contains the Dimming value (absolute[0...255] or relative/Eltako [0...100])
112                     int dimmValue = getDB_2Value();
113
114                     EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
115
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%]
119                         dimmValue /= 2.55;
120                     }
121
122                     return new PercentType(dimmValue);
123                 }
124         }
125
126         return UnDefType.UNDEF;
127     }
128 }