]> git.basschouten.com Git - openhab-addons.git/blob
4dbb97ac80cae66e56fed00b95ab26977a473ce0
[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.loxone.internal.controls;
14
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
16
17 import java.io.IOException;
18
19 import org.openhab.binding.loxone.internal.types.LxCategory;
20 import org.openhab.binding.loxone.internal.types.LxTags;
21 import org.openhab.binding.loxone.internal.types.LxUuid;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.type.ChannelTypeUID;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27
28 /**
29  * A switch type of control on Loxone Miniserver.
30  * <p>
31  * According to Loxone API documentation, a switch control is:
32  * <ul>
33  * <li>a virtual input of switch type
34  * <li>a push button function block
35  * </ul>
36  *
37  * @author Pawel Pieczul - initial contribution
38  *
39  */
40 class LxControlSwitch extends LxControl {
41
42     static class Factory extends LxControlInstance {
43         @Override
44         LxControl create(LxUuid uuid) {
45             return new LxControlSwitch(uuid);
46         }
47
48         @Override
49         String getType() {
50             return "switch";
51         }
52     }
53
54     /**
55      * Switch has one state that can be on/off
56      */
57     private static final String STATE_ACTIVE = "active";
58
59     /**
60      * Command string used to set control's state to ON
61      */
62     private static final String CMD_ON = "On";
63     /**
64      * Command string used to set control's state to OFF
65      */
66     private static final String CMD_OFF = "Off";
67
68     LxControlSwitch(LxUuid uuid) {
69         super(uuid);
70     }
71
72     @Override
73     public void initialize(LxControlConfig config) {
74         super.initialize(config);
75         LxCategory category = getCategory();
76         if (category != null && category.getType() == LxCategory.CategoryType.LIGHTS) {
77             tags.addAll(LxTags.LIGHTING);
78         } else {
79             tags.addAll(LxTags.SWITCHABLE);
80         }
81         addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH), defaultChannelLabel,
82                 "Switch", tags, this::handleSwitchCommands, this::getSwitchState);
83     }
84
85     void handleSwitchCommands(Command command) throws IOException {
86         if (command instanceof OnOffType onOffCommand) {
87             if (onOffCommand == OnOffType.ON) {
88                 on();
89             } else {
90                 off();
91             }
92         }
93     }
94
95     /**
96      * Set switch to ON.
97      * <p>
98      * Sends a command to operate the switch.
99      * This method is separated, so {@link LxControlMood} can inherit from this class, but handle 'on' commands
100      * differently.
101      *
102      * @throws IOException when something went wrong with communication
103      */
104     void on() throws IOException {
105         sendAction(CMD_ON);
106     }
107
108     /**
109      * Set switch to OFF.
110      * <p>
111      * Sends a command to operate the switch.
112      * This method is separated, so {@link LxControlMood} and {@link LxControlPushbutton} can inherit from this class,
113      * but handle 'off' commands differently.
114      *
115      * @throws IOException when something went wrong with communication
116      */
117     void off() throws IOException {
118         sendAction(CMD_OFF);
119     }
120
121     /**
122      * Get current value of the switch'es state.
123      * This method is separated, so it can be overridden by {@link LxControlTimedSwitch}, which inherits from the switch
124      * class, but has a different way of handling states.
125      *
126      * @return ON/OFF or null if undefined
127      */
128     State getSwitchState() {
129         return convertSwitchState(getStateDoubleValue(STATE_ACTIVE));
130     }
131
132     /**
133      * Convert double value of switch into ON/OFF state value
134      *
135      * @param value state value as double
136      * @return state value as ON/OFF
137      */
138     static State convertSwitchState(Double value) {
139         if (value != null) {
140             if (value == 1.0) {
141                 return OnOffType.ON;
142             } else if (value == 0.0) {
143                 return OnOffType.OFF;
144             } else {
145                 return UnDefType.UNDEF;
146             }
147         }
148         return null;
149     }
150 }