]> git.basschouten.com Git - openhab-addons.git/blob
fc7ea58fae78e92618d7395ed45f7d9b87e018fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.draytonwiser.internal.handler;
14
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApiException;
20 import org.openhab.binding.draytonwiser.internal.handler.SmartPlugHandler.SmartPlugData;
21 import org.openhab.binding.draytonwiser.internal.model.DeviceDTO;
22 import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
23 import org.openhab.binding.draytonwiser.internal.model.SmartPlugDTO;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30
31 /**
32  * The {@link SmartPlugHandler} is responsible for handling commands, which are
33  * sent to one of the channels.
34  *
35  * @author Andrew Schofield - Initial contribution
36  * @author Hilbrand Bouwkamp - Simplified handler to handle null data
37  */
38 @NonNullByDefault
39 public class SmartPlugHandler extends DraytonWiserThingHandler<SmartPlugData> {
40
41     private String serialNumber = "";
42
43     public SmartPlugHandler(final Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void initialize() {
49         super.initialize();
50         serialNumber = getConfig().get("serialNumber").toString();
51     }
52
53     @Override
54     protected void handleCommand(final String channelId, final Command command) throws DraytonWiserApiException {
55         if (command instanceof OnOffType) {
56             switch (channelId) {
57                 case CHANNEL_DEVICE_LOCKED:
58                     setDeviceLocked(OnOffType.ON.equals(command));
59                     break;
60                 case CHANNEL_SMARTPLUG_OUTPUT_STATE:
61                     setOutputState(OnOffType.ON.equals(command));
62                     break;
63                 case CHANNEL_SMARTPLUG_AWAY_ACTION:
64                     setAwayAction(OnOffType.ON.equals(command));
65                     break;
66                 case CHANNEL_MANUAL_MODE_STATE:
67                     setManualMode(OnOffType.ON.equals(command));
68                     break;
69             }
70         }
71     }
72
73     @Override
74     protected void refresh() {
75         updateState(CHANNEL_SMARTPLUG_OUTPUT_STATE, this::getOutputState);
76         updateState(CHANNEL_SMARTPLUG_AWAY_ACTION, this::getAwayAction);
77         updateState(CHANNEL_CURRENT_SIGNAL_RSSI, this::getSignalRSSI);
78         updateState(CHANNEL_CURRENT_SIGNAL_LQI, this::getSignalLQI);
79         updateState(CHANNEL_ZIGBEE_CONNECTED, this::getZigbeeConnected);
80         updateState(CHANNEL_DEVICE_LOCKED, this::getDeviceLocked);
81         updateState(CHANNEL_MANUAL_MODE_STATE, this::getManualModeState);
82     }
83
84     @Override
85     protected @Nullable SmartPlugData collectData(final DraytonWiserDTO domainDTOProxy) {
86         final SmartPlugDTO smartPlug = domainDTOProxy.getSmartPlug(serialNumber);
87         final DeviceDTO device = smartPlug == null ? null
88                 : domainDTOProxy.getExtendedDeviceProperties(smartPlug.getId());
89
90         return smartPlug == null || device == null ? null : new SmartPlugData(smartPlug, device);
91     }
92
93     private State getAwayAction() {
94         return OnOffType.from("off".equalsIgnoreCase(getData().smartPlug.getAwayAction()));
95     }
96
97     private State getOutputState() {
98         final String outputState = getData().smartPlug.getOutputState();
99         return outputState == null ? UnDefType.UNDEF : OnOffType.from(outputState);
100     }
101
102     private State getSignalRSSI() {
103         return new DecimalType(getData().device.getRssi());
104     }
105
106     private State getSignalLQI() {
107         return new DecimalType(getData().device.getLqi());
108     }
109
110     private State getZigbeeConnected() {
111         return getData().device.getLqi() == null ? OnOffType.OFF : OnOffType.ON;
112     }
113
114     private State getDeviceLocked() {
115         return getData().device.getDeviceLockEnabled() == null ? UnDefType.UNDEF
116                 : OnOffType.from(getData().device.getDeviceLockEnabled());
117     }
118
119     private void setDeviceLocked(final Boolean state) throws DraytonWiserApiException {
120         getApi().setDeviceLocked(getData().device.getId(), state);
121     }
122
123     private State getManualModeState() {
124         return OnOffType.from("MANUAL".equalsIgnoreCase(getData().smartPlug.getMode()));
125     }
126
127     private void setManualMode(final Boolean manualMode) throws DraytonWiserApiException {
128         getApi().setSmartPlugManualMode(getData().smartPlug.getId(), manualMode);
129     }
130
131     private void setOutputState(final Boolean outputState) throws DraytonWiserApiException {
132         getApi().setSmartPlugOutputState(getData().smartPlug.getId(), outputState);
133     }
134
135     private void setAwayAction(final Boolean awayAction) throws DraytonWiserApiException {
136         getApi().setSmartPlugAwayAction(getData().smartPlug.getId(), awayAction);
137     }
138
139     static class SmartPlugData {
140         public final SmartPlugDTO smartPlug;
141         public final DeviceDTO device;
142
143         public SmartPlugData(final SmartPlugDTO smartPlug, final DeviceDTO device) {
144             this.smartPlug = smartPlug;
145             this.device = device;
146         }
147     }
148 }