2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.loxone.internal.controls;
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
17 import java.io.IOException;
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;
29 * A switch type of control on Loxone Miniserver.
31 * According to Loxone API documentation, a switch control is:
33 * <li>a virtual input of switch type
34 * <li>a push button function block
37 * @author Pawel Pieczul - initial contribution
40 class LxControlSwitch extends LxControl {
42 static class Factory extends LxControlInstance {
44 LxControl create(LxUuid uuid) {
45 return new LxControlSwitch(uuid);
55 * Switch has one state that can be on/off
57 private static final String STATE_ACTIVE = "active";
60 * Command string used to set control's state to ON
62 private static final String CMD_ON = "On";
64 * Command string used to set control's state to OFF
66 private static final String CMD_OFF = "Off";
68 LxControlSwitch(LxUuid uuid) {
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);
79 tags.addAll(LxTags.SWITCHABLE);
81 addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH), defaultChannelLabel,
82 "Switch", tags, this::handleSwitchCommands, this::getSwitchState);
85 void handleSwitchCommands(Command command) throws IOException {
86 if (command instanceof OnOffType onOffCommand) {
87 if (onOffCommand == OnOffType.ON) {
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
102 * @throws IOException when something went wrong with communication
104 void on() throws IOException {
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.
115 * @throws IOException when something went wrong with communication
117 void off() throws IOException {
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.
126 * @return ON/OFF or null if undefined
128 State getSwitchState() {
129 return convertSwitchState(getStateDoubleValue(STATE_ACTIVE));
133 * Convert double value of switch into ON/OFF state value
135 * @param value state value as double
136 * @return state value as ON/OFF
138 static State convertSwitchState(Double value) {
142 } else if (value == 0.0) {
143 return OnOffType.OFF;
145 return UnDefType.UNDEF;