]> git.basschouten.com Git - openhab-addons.git/blob
49cca1ee52dbb9409b31d06f55303b13f0645da9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.argoclima.internal.device.api.protocol.elements;
14
15 import java.security.InvalidParameterException;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.argoclima.internal.device.api.protocol.ArgoDeviceStatus;
20 import org.openhab.binding.argoclima.internal.device.api.protocol.IArgoSettingProvider;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.types.Command;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
25
26 /**
27  * The API element representing ON/OFF knob
28  *
29  * @author Mateusz Bronk - Initial contribution
30  */
31 @NonNullByDefault
32 public class OnOffParam extends ArgoApiElementBase {
33     private Optional<Boolean> currentValue = Optional.empty();
34     private static final String VALUE_ON = "1";
35     private static final String VALUE_OFF = "0";
36
37     /**
38      * C-tor
39      *
40      * @param settingsProvider the settings provider (getting device state as well as schedule configuration)
41      */
42     public OnOffParam(IArgoSettingProvider settingsProvider) {
43         super(settingsProvider);
44     }
45
46     private static State valueToState(Optional<Boolean> value) {
47         return value.<State> map(v -> OnOffType.from(v)).orElse(UnDefType.UNDEF);
48     }
49
50     @Override
51     protected void updateFromApiResponseInternal(String responseValue) {
52         if (OnOffParam.VALUE_ON.equals(responseValue)) {
53             this.currentValue = Optional.of(true);
54         } else if (OnOffParam.VALUE_OFF.equals(responseValue)) {
55             this.currentValue = Optional.of(false);
56         } else if (ArgoDeviceStatus.NO_VALUE.equals(responseValue)) {
57             this.currentValue = Optional.empty();
58         } else {
59             throw new InvalidParameterException(String.format("Invalid value of parameter: {}", responseValue));
60         }
61     }
62
63     @Override
64     public State toState() {
65         return valueToState(currentValue);
66     }
67
68     @Override
69     public String toString() {
70         if (currentValue.isEmpty()) {
71             return "???";
72         }
73         return currentValue.get() ? "ON" : "OFF";
74     }
75
76     @Override
77     protected HandleCommandResult handleCommandInternalEx(Command command) {
78         if (command instanceof OnOffType onOffTypeCommand) {
79             if (OnOffType.ON.equals(onOffTypeCommand)) {
80                 var targetValue = Optional.of(true);
81                 currentValue = targetValue;
82                 return HandleCommandResult.accepted(VALUE_ON, valueToState(targetValue));
83             } else if (OnOffType.OFF.equals(onOffTypeCommand)) {
84                 var targetValue = Optional.of(false);
85                 currentValue = targetValue;
86                 return HandleCommandResult.accepted(VALUE_OFF, valueToState(targetValue));
87             }
88         }
89         return HandleCommandResult.rejected();
90     }
91 }