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