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.draytonwiser.internal.handler;
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
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;
34 * The {@link SmartPlugHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Andrew Schofield - Initial contribution
38 * @author Hilbrand Bouwkamp - Simplified handler to handle null data
41 public class SmartPlugHandler extends DraytonWiserThingHandler<SmartPlugData> {
43 private String serialNumber = "";
45 public SmartPlugHandler(final Thing thing) {
50 public void initialize() {
52 serialNumber = getConfig().get("serialNumber").toString();
56 protected void handleCommand(final String channelId, final Command command) throws DraytonWiserApiException {
57 if (command instanceof OnOffType) {
59 case CHANNEL_DEVICE_LOCKED:
60 setDeviceLocked(OnOffType.ON.equals(command));
62 case CHANNEL_SMARTPLUG_OUTPUT_STATE:
63 setOutputState(OnOffType.ON.equals(command));
65 case CHANNEL_SMARTPLUG_AWAY_ACTION:
66 setAwayAction(OnOffType.ON.equals(command));
68 case CHANNEL_MANUAL_MODE_STATE:
69 setManualMode(OnOffType.ON.equals(command));
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);
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());
94 return smartPlug == null || device == null ? null : new SmartPlugData(smartPlug, device);
97 private State getAwayAction() {
98 return OnOffType.from("off".equalsIgnoreCase(getData().smartPlug.getAwayAction()));
101 private State getOutputState() {
102 final String outputState = getData().smartPlug.getOutputState();
103 return outputState == null ? UnDefType.UNDEF : OnOffType.from(outputState);
106 private State getSignalRSSI() {
107 final Integer rssi = getData().device.getRssi();
108 return rssi == null ? UnDefType.UNDEF : new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS);
111 private State getSignalLQI() {
112 final Integer lqi = getData().device.getLqi();
113 return lqi == null ? UnDefType.UNDEF : new DecimalType(lqi);
116 private State getZigbeeConnected() {
117 return OnOffType.from(getData().device.getLqi() != null);
120 private State getDeviceLocked() {
121 return getData().device.getDeviceLockEnabled() == null ? UnDefType.UNDEF
122 : OnOffType.from(getData().device.getDeviceLockEnabled());
125 private void setDeviceLocked(final Boolean state) throws DraytonWiserApiException {
126 getApi().setDeviceLocked(getData().device.getId(), state);
129 private State getManualModeState() {
130 return OnOffType.from("MANUAL".equalsIgnoreCase(getData().smartPlug.getMode()));
133 private void setManualMode(final Boolean manualMode) throws DraytonWiserApiException {
134 getApi().setSmartPlugManualMode(getData().smartPlug.getId(), manualMode);
137 private void setOutputState(final Boolean outputState) throws DraytonWiserApiException {
138 getApi().setSmartPlugOutputState(getData().smartPlug.getId(), outputState);
141 private void setAwayAction(final Boolean awayAction) throws DraytonWiserApiException {
142 getApi().setSmartPlugAwayAction(getData().smartPlug.getId(), awayAction);
145 private State getInstantaneousDemand() {
146 final Integer demand = getData().smartPlug.getInstantaneousDemand();
147 return demand == null ? UnDefType.UNDEF : new QuantityType<>(demand, Units.WATT);
150 private State getCurrentSummationDelivered() {
151 final Integer delivered = getData().smartPlug.getCurrentSummationDelivered();
152 return delivered == null ? UnDefType.UNDEF : new QuantityType<>(delivered, Units.WATT_HOUR);
155 static class SmartPlugData {
156 public final SmartPlugDTO smartPlug;
157 public final DeviceDTO device;
159 public SmartPlugData(final SmartPlugDTO smartPlug, final DeviceDTO device) {
160 this.smartPlug = smartPlug;
161 this.device = device;