2 * Copyright (c) 2010-2024 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.broadlink.internal.handler;
15 import static org.junit.Assert.fail;
16 import static org.junit.jupiter.api.Assertions.assertEquals;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.broadlink.internal.BroadlinkBindingConstants.*;
20 import java.io.IOException;
21 import java.util.List;
23 import javax.measure.quantity.Power;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.ArgumentMatchers;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.openhab.binding.broadlink.internal.BroadlinkBindingConstants;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.QuantityType;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.types.State;
39 * Tests the Socket Model 3S (SP3S) handler.
41 * @author John Marshall - Initial contribution
44 public class BroadlinkSocketModel3SHandlerTest extends AbstractBroadlinkThingHandlerTest {
46 private final BroadlinkSocketModel3SHandler model3s;
48 public BroadlinkSocketModel3SHandlerTest() {
50 configureUnderlyingThing(BroadlinkBindingConstants.THING_TYPE_SP3S, "sp3s-test");
51 model3s = new BroadlinkSocketModel3SHandler(thing);
56 public void setUp() throws Exception {
57 MockitoAnnotations.openMocks(this).close();
61 public void deriveSp3sPowerConsumptionTooShort() {
62 byte[] payload = { 0x00, 0x00, 0x00, 0x00, 0x33 };
63 double result = model3s.deriveSP3sPowerConsumption(payload);
64 assertEquals(0D, result, 0.1D);
68 public void deriveSp3sPowerConsumptionCorrectSmallValue() {
69 byte[] payload = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x02, 0x00 };
70 double result = model3s.deriveSP3sPowerConsumption(payload);
71 assertEquals(2.19D, result, 0.1D);
75 public void deriveSp3sPowerConsumptionCorrectMediumValue() {
76 byte[] payload = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x75, 0x00 };
77 double result = model3s.deriveSP3sPowerConsumption(payload);
78 assertEquals(75.33D, result, 0.1D);
82 public void deriveSp3sPowerConsumptionCorrectLargeValue() {
83 byte[] payload = { 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x99, (byte) 0x88, 0x07 };
84 double result = model3s.deriveSP3sPowerConsumption(payload);
85 assertEquals(788.99D, result, 0.1D);
89 public void setsThePowerChannelAndConsumptionAfterGettingStatusOnSP3S() {
90 // Power bytes are 5, 6, 7 (little-endian) in BCD
91 // So here it's 0x38291 => 38291, divided by 100 ==> 382.91W
92 byte[] payload = { 0x08, 0x00, 0x11, 0x22, 0x01, (byte) 0x91, (byte) 0x82, 0x3, 0x16, 0x27, 0x28, 0x01, 0x02,
94 byte[] responseMessage = generateReceivedBroadlinkMessage(payload);
95 Mockito.when(mockSocket.sendAndReceive(ArgumentMatchers.any(byte[].class), ArgumentMatchers.anyString()))
96 .thenReturn(responseMessage);
97 setMocksForTesting(model3s);
102 model3s.getStatusFromDevice();
103 } catch (IOException e) {
104 fail("Unexpected exception: " + e.getClass().getCanonicalName());
107 ArgumentCaptor<ChannelUID> channelCaptor = ArgumentCaptor.forClass(ChannelUID.class);
108 ArgumentCaptor<State> stateCaptor = ArgumentCaptor.forClass(State.class);
109 verify(mockCallback, Mockito.times(2)).stateUpdated(channelCaptor.capture(), stateCaptor.capture());
111 List<ChannelUID> channels = channelCaptor.getAllValues();
112 List<State> states = stateCaptor.getAllValues();
114 ChannelUID expectedPowerChannel = new ChannelUID(thing.getUID(), COMMAND_POWER_ON);
115 assertEquals(expectedPowerChannel, channels.get(0));
117 assertEquals(OnOffType.ON, states.get(0));
119 ChannelUID expectedConsumptionChannel = new ChannelUID(thing.getUID(), POWER_CONSUMPTION_CHANNEL);
120 assertEquals(expectedConsumptionChannel, channels.get(1));
122 QuantityType<Power> expectedPower = new QuantityType<>(382.91,
123 BroadlinkBindingConstants.BROADLINK_POWER_CONSUMPTION_UNIT);
124 assertEquals(expectedPower, states.get(1));