forked from davorf/BlackBeanControl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlackBeanControl.py
More file actions
209 lines (173 loc) · 7.47 KB
/
BlackBeanControl.py
File metadata and controls
209 lines (173 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!python2
import broadlink, configparser
import sys, getopt
import time, binascii
import netaddr
import Settings
from os import path
from Crypto.Cipher import AES
SettingsFile = configparser.ConfigParser()
SettingsFile.optionxform = str
SettingsFile.read(Settings.BlackBeanControlSettings)
SentCommand = ''
ReKeyCommand = False
DeviceName=''
DeviceIPAddress = ''
DevicePort = ''
DeviceMACAddres = ''
DeviceTimeout = ''
AlternativeIPAddress = ''
AlternativePort = ''
AlternativeMACAddress = ''
AlternativeTimeout = ''
try:
Options, args = getopt.getopt(sys.argv[1:], 'c:d:r:i:p:m:t:h', ['command=','device=','rekey=','ipaddress=','port=','macaddress=','timeout=','help'])
except getopt.GetoptError:
print('BlackBeanControl.py -c <Command name> [-d <Device name>] [-i <IP Address>] [-p <Port>] [-m <MAC Address>] [-t <Timeout>] [-r <Re-Key Command>]')
sys.exit(2)
for Option, Argument in Options:
if Option in ('-h', '--help'):
print('BlackBeanControl.py -c <Command name> [-d <Device name>] [-i <IP Address>] [-p <Port>] [-m <MAC Address>] [-t <Timeout> [-r <Re-Key Command>]')
sys.exit()
elif Option in ('-c', '--command'):
SentCommand = Argument
elif Option in ('-d', '--device'):
DeviceName = Argument
elif Option in ('-r', '--rekey'):
ReKeyCommand = True
SentCommand = Argument
elif Option in ('-i', '--ipaddress'):
AlternativeIPAddress = Argument
elif Option in ('-p', '--port'):
AlternativePort = Argument
elif Option in ('-m', '--macaddress'):
AlternativeMACAddress = Argument
elif Option in ('-t', '--timeout'):
AlternativeTimeout = Argument
if SentCommand.strip() == '':
print('Command name parameter is mandatory')
sys.exit(2)
if (DeviceName.strip() != '') and ((AlternativeIPAddress.strip() != '') or (AlternativePort.strip() != '') or (AlternativeMACAddress.strip() != '') or (AlternativeTimeout != '')):
print('Device name parameter can not be used in conjunction with IP Address/Port/MAC Address/Timeout parameters')
sys.exit(2)
if (((AlternativeIPAddress.strip() != '') or (AlternativePort.strip() != '') or (AlternativeMACAddress.strip() != '') or (AlternativeTimeout.strip() != '')) and ((AlternativeIPAddress.strip() == '') or (AlternativePort.strip() == '') or (AlternativeMACAddress.strip() == '') or (AlternativeTimeout.strip() == ''))):
print('IP Address, Port, MAC Address and Timeout parameters can not be used separately')
sys.exit(2)
if DeviceName.strip() != '':
if SettingsFile.has_section(DeviceName.strip()):
if SettingsFile.has_option(DeviceName.strip(), 'IPAddress'):
DeviceIPAddress = SettingsFile.get(DeviceName.strip(), 'IPAddress')
else:
DeviceIPAddress = ''
if SettingsFile.has_option(DeviceName.strip(), 'Port'):
DevicePort = SettingsFile.get(DeviceName.strip(), 'Port')
else:
DevicePort = ''
if SettingsFile.has_option(DeviceName.strip(), 'MACAddress'):
DeviceMACAddress = SettingsFile.get(DeviceName.strip(), 'MACAddress')
else:
DeviceMACAddress = ''
if SettingsFile.has_option(DeviceName.strip(), 'Timeout'):
DeviceTimeout = SettingsFile.get(DeviceName.strip(), 'Timeout')
else:
DeviceTimeout = ''
else:
print('Device does not exist in BlackBeanControl.ini')
sys.exit(2)
if (DeviceName.strip() != '') and (DeviceIPAddress.strip() == ''):
print('IP address must exist in BlackBeanControl.ini for the selected device')
sys.exit(2)
if (DeviceName.strip() != '') and (DevicePort.strip() == ''):
print('Port must exist in BlackBeanControl.ini for the selected device')
sys.exit(2)
if (DeviceName.strip() != '') and (DeviceMACAddress.strip() == ''):
print('MAC address must exist in BlackBeanControl.ini for the selected device')
sys.exit(2)
if (DeviceName.strip() != '') and (DeviceTimeout.strip() == ''):
print('Timeout must exist in BlackBeanControl.ini for the selected device')
sys.exit(2)
if DeviceName.strip() != '':
RealIPAddress = DeviceIPAddress.strip()
elif AlternativeIPAddress.strip() != '':
RealIPAddress = AlternativeIPAddress.strip()
else:
RealIPAddress = Settings.IPAddress
if RealIPAddress.strip() == '':
print('IP address must exist in BlackBeanControl.ini or it should be entered as a command line parameter')
sys.exit(2)
if DeviceName.strip() != '':
RealPort = DevicePort.strip()
elif AlternativePort.strip() != '':
RealPort = AlternativePort.strip()
else:
RealPort = Settings.Port
if RealPort.strip() == '':
print('Port must exist in BlackBeanControl.ini or it should be entered as a command line parameter')
sys.exit(2)
else:
RealPort = int(RealPort.strip())
if DeviceName.strip() != '':
RealMACAddress = DeviceMACAddress.strip()
elif AlternativeMACAddress.strip() != '':
RealMACAddress = AlternativeMACAddress.strip()
else:
RealMACAddress = Settings.MACAddress
if RealMACAddress.strip() == '':
print('MAC address must exist in BlackBeanControl.ini or it should be entered as a command line parameter')
sys.exit(2)
else:
RealMACAddress = netaddr.EUI(RealMACAddress)
if DeviceName.strip() != '':
RealTimeout = DeviceTimeout.strip()
elif AlternativeTimeout.strip() != '':
RealTimeout = AlternativeTimeout.strip()
else:
RealTimeout = Settings.Timeout
if RealTimeout.strip() == '':
print('Timeout must exist in BlackBeanControl.ini or it should be entered as a command line parameter')
sys.exit(2)
else:
RealTimeout = int(RealTimeout.strip())
RM3Device = broadlink.rm((RealIPAddress, RealPort), RealMACAddress)
RM3Device.auth()
if ReKeyCommand:
if SettingsFile.has_option('Commands', SentCommand):
CommandFromSettings = SettingsFile.get('Commands', SentCommand)
if CommandFromSettings[0:4] != '2600':
RM3Key = RM3Device.key
RM3IV = RM3Device.iv
DecodedCommand = binascii.unhexlify(CommandFromSettings)
AESEncryption = AES.new(str(RM3Key), AES.MODE_CBC, str(RM3IV))
EncodedCommand = AESEncryption.encrypt(str(DecodedCommand))
FinalCommand = EncodedCommand[0x04:]
EncodedCommand = FinalCommand.encode('hex')
BlackBeanControlIniFile = open(path.join(Settings.ApplicationDir, 'BlackBeanControl.ini'), 'w')
SettingsFile.set('Commands', SentCommand, EncodedCommand)
SettingsFile.write(BlackBeanControlIniFile)
BlackBeanControlIniFile.close()
sys.exit()
else:
print("Command appears to already be re-keyed.")
sys.exit(2)
else:
print("Command not found in ini file for re-keying.")
sys.exit(2)
if SettingsFile.has_option('Commands', SentCommand):
CommandFromSettings = SettingsFile.get('Commands', SentCommand)
else:
CommandFromSettings = ''
if CommandFromSettings.strip() != '':
DecodedCommand = CommandFromSettings.decode('hex')
RM3Device.send_data(DecodedCommand)
else:
RM3Device.enter_learning()
time.sleep(RealTimeout)
LearnedCommand = RM3Device.check_data()
if LearnedCommand is None:
print('Command not received')
sys.exit()
EncodedCommand = LearnedCommand.encode('hex')
BlackBeanControlIniFile = open(path.join(Settings.ApplicationDir, 'BlackBeanControl.ini'), 'w')
SettingsFile.set('Commands', SentCommand, EncodedCommand)
SettingsFile.write(BlackBeanControlIniFile)
BlackBeanControlIniFile.close()