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.tplinksmarthome.internal.device;
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
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;
27 * TP-Link Smart Home Switch device that also can measure energy usage.
29 * @author Hilbrand Bouwkamp - Initial contribution
32 public class EnergySwitchDevice extends SwitchDevice {
35 public String getUpdateCommand() {
36 return Commands.getRealtimeAndSysinfo();
40 public State updateChannel(ChannelUID channelUid, DeviceState deviceState) {
42 final String matchChannelId = channelUid.isInGroup() ? channelUid.getIdWithoutGroup() : channelUid.getId();
44 if (CHANNELS_ENERGY.contains(matchChannelId)) {
45 state = updateEnergyChannel(matchChannelId, deviceState.getRealtime());
47 state = super.updateChannel(channelUid, deviceState);
53 * Gets the state for an energy channel.
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
59 protected State updateEnergyChannel(String channelId, Realtime realtime) {
63 case CHANNEL_ENERGY_CURRENT:
64 value = new QuantityType<>(realtime.getCurrent(), Units.AMPERE);
66 case CHANNEL_ENERGY_TOTAL:
67 value = new QuantityType<>(realtime.getTotal(), Units.KILOWATT_HOUR);
69 case CHANNEL_ENERGY_VOLTAGE:
70 value = new QuantityType<>(realtime.getVoltage(), Units.VOLT);
72 case CHANNEL_ENERGY_POWER:
73 value = new QuantityType<>(realtime.getPower(), Units.WATT);
76 value = UnDefType.UNDEF;