]> git.basschouten.com Git - openhab-addons.git/blob
c1acd4a206f4a2ad780aef290536d8388d49b300
[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.neohub.internal;
14
15 import static org.openhab.binding.neohub.internal.NeoHubBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.thing.Thing;
20 import org.openhab.core.types.Command;
21
22 /**
23  * The {@link NeoPlugHandler} is the OpenHAB Handler for NeoPlug devices Note:
24  * inherits almost all the functionality of a {@link NeoBaseHandler}
25  * 
26  * @author Andrew Fiddian-Green - Initial contribution
27  * 
28  */
29 @NonNullByDefault
30 public class NeoPlugHandler extends NeoBaseHandler {
31
32     public NeoPlugHandler(Thing thing) {
33         super(thing);
34     }
35
36     // =========== methods of NeoBaseHandler that are overridden ================
37
38     @Override
39     protected String toNeoHubBuildCommandString(String channelId, Command command) {
40         NeoBaseConfiguration config = this.config;
41         if (config != null) {
42             if (command instanceof OnOffType onOffCommand && channelId.equals(CHAN_PLUG_OUTPUT_STATE)) {
43                 return String.format(CMD_CODE_TIMER, onOffCommand.toString(), config.deviceNameInHub);
44             }
45             if (command instanceof OnOffType onOffCommand && channelId.equals(CHAN_PLUG_AUTO_MODE)) {
46                 return String.format(CMD_CODE_MANUAL, invert(onOffCommand).toString(), config.deviceNameInHub);
47             }
48         }
49         return "";
50     }
51
52     @Override
53     protected void toNeoHubSendCommandSet(String channelId, Command command) {
54         // if this is a manual command, switch to manual mode first..
55         if (channelId.equals(CHAN_PLUG_OUTPUT_STATE) && command instanceof OnOffType) {
56             toNeoHubSendCommand(CHAN_PLUG_AUTO_MODE, OnOffType.from(false));
57         }
58         // send the actual command to the hub
59         toNeoHubSendCommand(channelId, command);
60     }
61
62     @Override
63     protected void toOpenHabSendChannelValues(NeoHubAbstractDeviceData.AbstractRecord deviceRecord) {
64         boolean offline = deviceRecord.offline();
65         toOpenHabSendValueDebounced(CHAN_PLUG_AUTO_MODE, OnOffType.from(!deviceRecord.stateManual()), offline);
66         toOpenHabSendValueDebounced(CHAN_PLUG_OUTPUT_STATE, OnOffType.from(deviceRecord.isTimerOn()), offline);
67     }
68 }