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;
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.List;
22 import java.util.stream.Collectors;
24 import org.openhab.binding.loxone.internal.types.LxUuid;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.StateDescriptionFragmentBuilder;
31 import org.openhab.core.types.StateOption;
34 * A radio-button type of control on Loxone Miniserver.
36 * @author Pawel Pieczul - initial contribution
39 class LxControlRadio extends LxControl {
41 static class Factory extends LxControlInstance {
43 LxControl create(LxUuid uuid) {
44 return new LxControlRadio(uuid);
54 * Number of outputs a radio controller may have
56 private static final int MAX_RADIO_OUTPUTS = 16;
59 * Radio-button has one state that is a number representing current active output
61 private static final String STATE_ACTIVE_OUTPUT = "activeoutput";
64 * Command string used to set radio button to all outputs off
66 private static final String CMD_RESET = "reset";
68 private Map<String, String> outputsMap;
71 public void initialize(LxControlConfig config) {
72 super.initialize(config);
73 // add both channel and state description (all needed configuration is available)
74 ChannelUID cid = addChannel("Number", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_RADIO_BUTTON),
75 defaultChannelLabel, "Radio button", tags, this::handleCommands, this::getChannelState);
77 if (details != null) {
78 List<StateOption> outputs = new ArrayList<>();
79 if (details.outputs != null) {
80 outputsMap = details.outputs;
81 outputs = details.outputs.entrySet().stream().map(e -> new StateOption(e.getKey(), e.getValue()))
82 .collect(Collectors.toList());
84 if (details.allOff != null && !details.allOff.isEmpty()) {
85 outputs.add(new StateOption("0", details.allOff));
86 outputsMap.put("0", details.allOff);
88 addChannelStateDescriptionFragment(cid,
89 StateDescriptionFragmentBuilder.create().withMinimum(BigDecimal.ZERO)
90 .withMaximum(new BigDecimal(MAX_RADIO_OUTPUTS)).withStep(BigDecimal.ONE).withReadOnly(false)
91 .withOptions(outputs).build());
95 private LxControlRadio(LxUuid uuid) {
99 private void handleCommands(Command command) throws IOException {
100 if (((command instanceof OnOffType onOffCommand && onOffCommand == OnOffType.OFF)
101 || DecimalType.ZERO.equals(command)) && outputsMap.containsKey("0")) {
102 sendAction(CMD_RESET);
103 } else if (command instanceof DecimalType output) {
104 if (outputsMap.containsKey(output.toString())) {
105 sendAction(String.valueOf(output.intValue()));
110 private DecimalType getChannelState() {
111 Double output = getStateDoubleValue(STATE_ACTIVE_OUTPUT);
112 if (output != null && output % 1 == 0 && outputsMap.containsKey(String.valueOf(output.intValue()))) {
113 return new DecimalType(output);