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.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.*;
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.params.ParameterizedTest;
24 import org.junit.jupiter.params.provider.MethodSource;
25 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
31 * Test class for {@link EnergySwitchDevice} class.
33 * @author Hilbrand Bouwkamp - Initial contribution
36 public class EnergySwitchDeviceTest {
38 private static final List<Object[]> TESTS = Arrays
39 .asList(new Object[][] { { "plug_get_realtime_response", }, { "plug_get_realtime_response_v2", } });
41 private final EnergySwitchDevice device = new EnergySwitchDevice();
43 public static List<Object[]> data() {
49 public void testUpdateChannelEnergyCurrent(String name) throws IOException {
50 DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
51 assertEquals(new QuantityType<>(1 + " A"), device.updateChannel(CHANNEL_UID_ENERGY_CURRENT, deviceState),
52 "Energy current should have valid state value");
57 public void testUpdateChannelEnergyTotal(String name) throws IOException {
58 DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
59 assertEquals(new QuantityType<>(10 + " kWh"), device.updateChannel(CHANNEL_UID_ENERGY_TOTAL, deviceState),
60 "Energy total should have valid state value");
65 public void testUpdateChannelEnergyVoltage(String name) throws IOException {
66 DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
67 State state = device.updateChannel(CHANNEL_UID_ENERGY_VOLTAGE, deviceState);
68 assertEquals(230, ((QuantityType<?>) state).intValue(), "Energy voltage should have valid state value");
69 assertEquals("230 V", state.format("%.0f %unit%"), "Channel patten to format voltage correctly");
74 public void testUpdateChanneEnergyPower(String name) throws IOException {
75 DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
76 assertEquals(new QuantityType<>(20 + " W"), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
77 "Energy power should have valid state value");
82 public void testUpdateChannelOther(String name) throws IOException {
83 DeviceState deviceState = new DeviceState(ModelTestUtil.readJson(name));
84 assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
85 "Unknown channel should return UNDEF");