1
+ """
2
+ Unit tests for pyRCSwitch
3
+ Python module to wrap the RCSwitch Common Library
4
+
5
+ See: https://github.com/latchdevel/pyRCSwitch
6
+
7
+ Copyright (c) 2024 Jorge Rivera. All right reserved.
8
+ License GNU Lesser General Public License v3.0.
9
+ """
10
+
11
+ import unittest
12
+
13
+ from pyRCSwitch import RCSwitch
14
+
15
+ testRCSwitchPiCodes = [
16
+ {
17
+ "picode" : {'conrad_rsl_switch' : {'id' : 1 , 'unit' : 2 , 'state' : 'on' }},
18
+ "command" : "On" , # unused
19
+ "protocol" : 2 ,
20
+ "length" : 650 ,
21
+ "value" : 2384640512 ,
22
+ "bits" : 32 ,
23
+ "pulses" : [1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 6500 ]
24
+ },
25
+ {
26
+ "picode" : {'conrad_rsl_switch' : {'id' : 1 , 'unit' : 2 , 'state' : 'off' }},
27
+ "command" : "Off" , # unused
28
+ "protocol" : 2 ,
29
+ "length" : 650 ,
30
+ "value" : 2166536704 ,
31
+ "bits" : 32 ,
32
+ "pulses" : [1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 650 , 1300 , 1300 , 650 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 1300 , 650 , 6500 ]
33
+ },
34
+ ]
35
+
36
+ testRCSwitchCodes = [
37
+ {
38
+ "sliding" : ["11111" , "00010" ],
39
+ "command" : "On" ,
40
+ "protocol" : 1 ,
41
+ "length" : 350 ,
42
+ "value" : 5393 ,
43
+ "bits" : 24 ,
44
+ "binary" : "000000000001010100010001" ,
45
+ "tri-state" : "00000FFF0F0F" ,
46
+ "pulses" : [350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 10850 ]
47
+ },
48
+ {
49
+ "sliding" : ["11111" , "00010" ],
50
+ "command" : "Off" ,
51
+ "protocol" : 1 ,
52
+ "length" : 350 ,
53
+ "value" : 5396 ,
54
+ "bits" : 24 ,
55
+ "binary" : "000000000001010100010100" ,
56
+ "tri-state" : "00000FFF0FF0" ,
57
+ "pulses" : [350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 10850 ]
58
+ },
59
+ ]
60
+
61
+ testRCSwitchTypeD = [
62
+ {
63
+ "family-device" : ['d' , 2 ], # {'protocols': [{'arctech_screen_old': {'id': 26, 'unit': 0, 'state': 'up'}}, {'arctech_switch_old': {'id': 26, 'unit': 0, 'state': 'on'}}]}
64
+ "command" : "On" ,
65
+ "protocol" : 1 ,
66
+ "length" : 360 ,
67
+ "value" : 4433 ,
68
+ "bits" : 24 ,
69
+ "pulses" : [360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 1080 , 360 , 360 , 11160 ]
70
+ },
71
+ {
72
+ "family-device" : ['d' , 2 ], # {'protocols': [{'arctech_screen_old': {'id': 26, 'unit': 0, 'state': 'down'}}, {'arctech_switch_old': {'id': 26, 'unit': 0, 'state': 'off'}}]}
73
+ "command" : "Off" ,
74
+ "protocol" : 1 ,
75
+ "length" : 360 ,
76
+ "value" : 4436 ,
77
+ "bits" : 24 ,
78
+ "pulses" : [360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 360 , 1080 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 1080 , 360 , 360 , 1080 , 360 , 1080 , 360 , 11160 ]
79
+ },
80
+ ]
81
+
82
+ testRCSwitchTypeCIntertechno = [
83
+ {
84
+ "family-group-device" : ['d' , 3 , 2 ],
85
+ "command" : "On" ,
86
+ "protocol" : 1 ,
87
+ "length" : 350 ,
88
+ "value" : 5259541 ,
89
+ "bits" : 24 ,
90
+ "pulses" : [350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 10850 ]
91
+ },
92
+ {
93
+ "family-group-device" : ['d' , 3 , 2 ],
94
+ "command" : "Off" ,
95
+ "protocol" : 1 ,
96
+ "length" : 350 ,
97
+ "value" : 5259540 ,
98
+ "bits" : 24 ,
99
+ "pulses" : [350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 1050 , 350 , 350 , 1050 , 350 , 1050 , 350 , 10850 ]
100
+ },
101
+ ]
102
+
103
+ class tests_pyRCSwitch (unittest .TestCase ):
104
+
105
+ myRCSwitch = RCSwitch ()
106
+
107
+ def test_moduleVersion (self ):
108
+ import pyRCSwitch as _pyRCSwitch
109
+ result = _pyRCSwitch .__version__
110
+ self .assertIsInstance (result ,str )
111
+
112
+ def test_RCSwitchObjectInstance (self ):
113
+ result = self .myRCSwitch
114
+ self .assertIsInstance (result , RCSwitch )
115
+
116
+ def test_RCSwitchSendSliding (self ):
117
+ for code in testRCSwitchCodes :
118
+ if code ["command" ] == "On" :
119
+ command = self .myRCSwitch .switchOn
120
+ else :
121
+ command = self .myRCSwitch .switchOff
122
+ with self .subTest ("test_RCSwitch.switch{}({})" .format (code ["command" ],code ["sliding" ])):
123
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
124
+ self .assertEqual (command (code ["sliding" ][0 ],code ["sliding" ][1 ]),code ["pulses" ])
125
+
126
+ def test_RCSwitchSendTypeD (self ):
127
+ for code in testRCSwitchTypeD :
128
+ if code ["command" ] == "On" :
129
+ command = self .myRCSwitch .switchOn
130
+ else :
131
+ command = self .myRCSwitch .switchOff
132
+ with self .subTest ("test_RCSwitch.switch{}({})" .format (code ["command" ],code ["family-device" ])):
133
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
134
+ self .assertEqual (command (code ["family-device" ][0 ],code ["family-device" ][1 ]),code ["pulses" ])
135
+
136
+ def test_RCSwitchSendTypeCIntertechno (self ):
137
+ for code in testRCSwitchTypeCIntertechno :
138
+ if code ["command" ] == "On" :
139
+ command = self .myRCSwitch .switchOn
140
+ else :
141
+ command = self .myRCSwitch .switchOff
142
+ with self .subTest ("test_RCSwitch.switch{}({})" .format (code ["command" ],code ["family-group-device" ])):
143
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
144
+ self .assertEqual (command (code ["family-group-device" ][0 ],code ["family-group-device" ][1 ],code ["family-group-device" ][2 ]),code ["pulses" ])
145
+
146
+ def test_RCSwitchSendCodeValue (self ):
147
+ for code in testRCSwitchCodes :
148
+ with self .subTest ("test_RCSwitch.send({},{})" .format (code ["value" ],code ["bits" ])):
149
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
150
+ self .assertEqual (self .myRCSwitch .send (code ["value" ],code ["bits" ]),code ["pulses" ])
151
+
152
+ def test_RCSwitchSendPiCodeValue (self ):
153
+ for code in testRCSwitchPiCodes :
154
+ with self .subTest ("test_RCSwitch.send({},{})" .format (code ["value" ],code ["bits" ])):
155
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
156
+ self .assertEqual (self .myRCSwitch .send (code ["value" ],code ["bits" ]),code ["pulses" ])
157
+
158
+ def test_RCSwitchSendCodeBinary (self ):
159
+ for code in testRCSwitchCodes :
160
+ with self .subTest ("test_RCSwitch.send(\" {}\" )" .format (code ["binary" ])):
161
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
162
+ self .assertEqual (self .myRCSwitch .send (code ["binary" ]),code ["pulses" ])
163
+
164
+ def test_RCSwitchSendCodeTriState (self ):
165
+ for code in testRCSwitchCodes :
166
+ with self .subTest ("test_RCSwitch.sendTriState(\" {}\" )" .format (code ["tri-state" ])):
167
+ self .myRCSwitch .setProtocol (code ["protocol" ],code ["length" ])
168
+ self .assertEqual (self .myRCSwitch .sendTriState (code ["tri-state" ]),code ["pulses" ])
169
+
170
+ def test_RCSwitchDecode (self ):
171
+ for code in testRCSwitchPiCodes + testRCSwitchCodes + testRCSwitchTypeCIntertechno + testRCSwitchTypeD :
172
+ with self .subTest ("test_RCSwitch.decode([pulse_list])" ):
173
+ self .assertTrue (self .myRCSwitch .decodePulseTrain (code ["pulses" ]))
174
+ self .assertEqual (self .myRCSwitch .getReceivedProtocol (),code ["protocol" ])
175
+ self .assertEqual (self .myRCSwitch .getReceivedDelay (),code ["length" ])
176
+ self .assertEqual (self .myRCSwitch .getReceivedBitlength (),code ["bits" ])
177
+ self .assertEqual (self .myRCSwitch .getReceivedValue (),code ["value" ])
178
+ self .assertEqual (self .myRCSwitch .getReceivedRawdata (),code ["pulses" ])
179
+
180
+ if __name__ == '__main__' :
181
+ unittest .main ()
0 commit comments