]> git.basschouten.com Git - openhab-addons.git/blob
04effbdd933d8a18eba34f7809ad8458b93da2a3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.tplinksmarthome.internal.device;
14
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.tplinksmarthome.internal.Commands;
19 import org.openhab.binding.tplinksmarthome.internal.model.Realtime;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.unit.Units;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
25
26 /**
27  * TP-Link Smart Home Switch device that also can measure energy usage.
28  *
29  * @author Hilbrand Bouwkamp - Initial contribution
30  */
31 @NonNullByDefault
32 public class EnergySwitchDevice extends SwitchDevice {
33
34     @Override
35     public String getUpdateCommand() {
36         return Commands.getRealtimeAndSysinfo();
37     }
38
39     @Override
40     public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
41         final State state;
42         final String matchChannelId = channelUid.isInGroup() ? channelUid.getIdWithoutGroup() : channelUid.getId();
43
44         if (CHANNELS_ENERGY.contains(matchChannelId)) {
45             state = updateEnergyChannel(matchChannelId, deviceState.getRealtime());
46         } else {
47             state = super.updateChannel(channelUid, deviceState);
48         }
49         return state;
50     }
51
52     /**
53      * Gets the state for an energy channel.
54      *
55      * @param channelId Id of the energy channel to get the state
56      * @param realtime data object containing the data from the device
57      * @return state object for the given channel
58      */
59     protected State updateEnergyChannel(String channelId, Realtime realtime) {
60         final State value;
61
62         switch (channelId) {
63             case CHANNEL_ENERGY_CURRENT:
64                 value = new QuantityType<>(realtime.getCurrent(), Units.AMPERE);
65                 break;
66             case CHANNEL_ENERGY_TOTAL:
67                 value = new QuantityType<>(realtime.getTotal(), Units.KILOWATT_HOUR);
68                 break;
69             case CHANNEL_ENERGY_VOLTAGE:
70                 value = new QuantityType<>(realtime.getVoltage(), Units.VOLT);
71                 break;
72             case CHANNEL_ENERGY_POWER:
73                 value = new QuantityType<>(realtime.getPower(), Units.WATT);
74                 break;
75             default:
76                 value = UnDefType.UNDEF;
77                 break;
78         }
79         return value;
80     }
81 }