3
3
import static org .junit .jupiter .api .Assertions .*;
4
4
5
5
import com .bigboxer23 .switch_bot .data .Device ;
6
+ import com .bigboxer23 .utils .command .Command ;
7
+ import com .bigboxer23 .utils .properties .PropertyUtils ;
6
8
import java .io .IOException ;
7
9
import java .util .List ;
8
10
import org .junit .jupiter .api .Test ;
9
11
10
12
/** Need to define environment variables for SwitchBotToken/SwitchBotSecret to run tests */
11
13
public class SwitchBotApiTest {
12
- private final String token = System . getenv ( "SwitchBotToken " );
14
+ private static final String token = PropertyUtils . getProperty ( "switchbot_token " );
13
15
14
- private final String secret = System .getenv ("SwitchBotSecret" );
16
+ private static final String secret = PropertyUtils .getProperty ("switchbot_secret" );
17
+
18
+ private static final SwitchBotApi instance = SwitchBotApi .getInstance (token , secret );
15
19
16
20
@ Test
17
21
public void testGetDevices () throws IOException {
18
- SwitchBotApi instance = SwitchBotApi .getInstance (token , secret );
19
22
List <Device > devices = instance .getDeviceApi ().getDevices ();
20
23
assertFalse (devices .isEmpty ());
21
24
assertNotNull (devices .get (0 ).getDeviceId ());
22
25
}
23
26
24
27
@ Test
25
28
public void testDeviceStatus () throws IOException {
26
- SwitchBotApi instance = SwitchBotApi .getInstance (token , secret );
27
-
28
29
try {
29
30
instance .getDeviceApi ().getDeviceStatus ("123" );
30
31
fail ();
@@ -45,7 +46,7 @@ public void testDeviceStatus() throws IOException {
45
46
}
46
47
case IDeviceTypes .CURTAIN -> {
47
48
assertTrue (status .getSlidePosition () >= 0 );
48
- assertTrue (status .getMoving () >= 0 );
49
+ assertTrue (status .isMoving () );
49
50
assertTrue (status .getBattery () >= 0 );
50
51
}
51
52
case IDeviceTypes .PLUG_MINI -> {
@@ -60,15 +61,59 @@ public void testDeviceStatus() throws IOException {
60
61
}
61
62
62
63
@ Test
63
- public void testDeviceCommands () throws IOException {
64
- SwitchBotApi instance = SwitchBotApi .getInstance (token , secret );
64
+ public void testCurtainDeviceCommands () throws IOException , InterruptedException {
65
65
Device curtain = instance .getDeviceApi ().getDevices ().stream ()
66
66
.filter (device -> IDeviceTypes .CURTAIN .equals (device .getDeviceType ()))
67
67
.filter (Device ::isMaster )
68
68
.findAny ()
69
69
.orElse (null );
70
70
assertNotNull (curtain );
71
71
assertNotNull (curtain .getDeviceId ());
72
- instance .getDeviceApi ().sendDeviceControlCommands (curtain .getDeviceId (), IDeviceCommands .CLOSE_CURTAIN );
72
+ System .out .println ("slide position "
73
+ + instance .getDeviceApi ().getDeviceStatus (curtain .getDeviceId ()).getSlidePosition ());
74
+
75
+ instance .getDeviceApi ().sendDeviceControlCommands (curtain .getDeviceId (), IDeviceCommands .CURTAIN_CLOSE );
76
+ pollForStatus (() -> {
77
+ int slidePosition = instance .getDeviceApi ()
78
+ .getDeviceStatus (curtain .getDeviceId ())
79
+ .getSlidePosition ();
80
+ System .out .println ("slide position " + slidePosition );
81
+ return slidePosition >= 90 ;
82
+ });
83
+ instance .getDeviceApi ().sendDeviceControlCommands (curtain .getDeviceId (), IDeviceCommands .CURTAIN_OPEN );
84
+ pollForStatus (() -> {
85
+ int slidePosition = instance .getDeviceApi ()
86
+ .getDeviceStatus (curtain .getDeviceId ())
87
+ .getSlidePosition ();
88
+ System .out .println ("slide position " + slidePosition );
89
+ return slidePosition == 0 ;
90
+ });
91
+ }
92
+
93
+ @ Test
94
+ public void testPlugDeviceCommands () throws IOException , InterruptedException {
95
+ Device plug = instance .getDeviceApi ().getDevices ().stream ()
96
+ .filter (device -> IDeviceTypes .PLUG_MINI .equals (device .getDeviceType ()))
97
+ .findAny ()
98
+ .orElse (null );
99
+ assertNotNull (plug );
100
+
101
+ instance .getDeviceApi ().sendDeviceControlCommands (plug .getDeviceId (), IDeviceCommands .PLUG_MINI_OFF );
102
+ pollForStatus (() ->
103
+ !instance .getDeviceApi ().getDeviceStatus (plug .getDeviceId ()).isPowerOn ());
104
+
105
+ instance .getDeviceApi ().sendDeviceControlCommands (plug .getDeviceId (), IDeviceCommands .PLUG_MINI_ON );
106
+ pollForStatus (() ->
107
+ instance .getDeviceApi ().getDeviceStatus (plug .getDeviceId ()).isPowerOn ());
108
+ }
109
+
110
+ private void pollForStatus (Command <Boolean > command ) throws IOException , InterruptedException {
111
+ boolean result = command .execute ();
112
+ for (int ai = 0 ; ai < 10 && !result ; ai ++) {
113
+ System .out .println ("sleeping " + ai );
114
+ Thread .sleep (2000 );
115
+ result = command .execute ();
116
+ }
117
+ assertTrue (result );
73
118
}
74
119
}
0 commit comments