]> git.basschouten.com Git - openhab-addons.git/blob
4a333bb965cee022f4c97c1df286ceafb6dda4cb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.loxone.internal.controls;
14
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
16
17 import java.math.BigDecimal;
18
19 import org.openhab.binding.loxone.internal.types.LxUuid;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.type.ChannelTypeUID;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.StateDescriptionFragmentBuilder;
26
27 /**
28  * A timed switch type of control on Loxone Miniserver.
29  * <p>
30  * According to Loxone API documentation, a switch control is:
31  * <ul>
32  * <li>a virtual input of switch type
33  * <li>a push button function block
34  * </ul>
35  *
36  * @author Stephan Brunner - initial contribution
37  *
38  */
39 class LxControlTimedSwitch extends LxControlPushbutton {
40
41     static class Factory extends LxControlInstance {
42         @Override
43         LxControl create(LxUuid uuid) {
44             return new LxControlTimedSwitch(uuid);
45         }
46
47         @Override
48         String getType() {
49             return "timedswitch";
50         }
51     }
52
53     /**
54      * deactivationDelay - countdown until the output is deactivated.
55      * 0 = the output is turned off
56      * -1 = the output is permanently on
57      * otherwise it will count down from deactivationDelayTotal
58      */
59     private static final String STATE_DEACTIVATION_DELAY = "deactivationdelay";
60
61     private LxControlTimedSwitch(LxUuid uuid) {
62         super(uuid);
63     }
64
65     @Override
66     public void initialize(LxControlConfig config) {
67         super.initialize(config);
68         ChannelUID id = addChannel("Number", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_RO_NUMBER),
69                 defaultChannelLabel + " / Deactivation Delay", "Deactivation Delay", null, null,
70                 this::getDeactivationState);
71         addChannelStateDescriptionFragment(id,
72                 StateDescriptionFragmentBuilder.create().withMinimum(new BigDecimal(-1)).withReadOnly(true).build());
73     }
74
75     private State getDeactivationState() {
76         Double deactivationValue = getStateDoubleValue(STATE_DEACTIVATION_DELAY);
77         if (deactivationValue != null) {
78             if (deactivationValue.equals(-1.0)) {
79                 // we don't show the special value of -1 to the user, this means switch is on and delay is zero
80                 deactivationValue = 0.0;
81             }
82             return new DecimalType(deactivationValue);
83         }
84         return null;
85     }
86
87     @Override
88     OnOffType getSwitchState() {
89         /**
90          * 0 = the output is turned off
91          * -1 = the output is permanently on
92          * otherwise it will count down from deactivationDelayTotal
93          **/
94         Double value = getStateDoubleValue(STATE_DEACTIVATION_DELAY);
95         if (value != null) {
96             if (value == -1.0 || value > 0.0) { // mapping
97                 return OnOffType.ON;
98             } else if (value == 0) {
99                 return OnOffType.OFF;
100             }
101         }
102         return null;
103     }
104 }