Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
DiscoverTimeout = 5
Autodetect = True
settingsFile.remove_option('General','Autodetect')
# Device list
DeviceByName = {}
if not settings.DevList:
Autodetect = True
if Autodetect == True:
print ("Beginning device auto-detection ... ")
# Try to support multi-homed broadcast better
try:
devices = broadlink.discover(DiscoverTimeout,listen_address,broadcast_address)
except:
devices = broadlink.discover(DiscoverTimeout,listen_address)
backupSettings()
try:
broadlinkControlIniFile = open(path.join(settings.applicationDir, 'settings.ini'), 'w')
for device in devices:
try:
device.hostname = socket.gethostbyaddr(device.host[0])[0]
if "." in device.hostname:
device.hostname = device.hostname.split('.')[0]
except:
device.hostname = "Broadlink" + device.type.upper()
if device.hostname in DeviceByName:
device.hostname = "%s-%s" % (device.hostname, str(device.host).split('.')[3])
DeviceByName[device.hostname] = device
if not settingsFile.has_section(device.hostname):
settingsFile.add_section(device.hostname)
def get_device(cf):
device_type = cf.get('device_type', 'lookup')
if device_type == 'lookup':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink device found')
sys.exit(2)
if len(devices) > 1:
logging.error('More than one Broadlink device found (' +
', '.join([d.type + '/' + d.host[0] + '/' + ':'.join(format(s, '02x') for s in d.mac[::-1]) for d in devices]) +
')')
sys.exit(2)
return configure_device(devices[0], topic_prefix)
elif device_type == 'multiple_lookup':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink devices found')
def get_device(cf):
device_type = cf.get('device_type', 'lookup')
if device_type == 'lookup':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink device found')
sys.exit(2)
if len(devices) > 1:
logging.error('More than one Broadlink device found (' +
', '.join([d.type + '/' + d.host[0] + '/' + ':'.join(format(s, '02x') for s in d.mac[::-1]) for d in devices]) +
')')
sys.exit(2)
return configure_device(devices[0], topic_prefix)
elif device_type == 'multiple_lookup':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
DiscoverTimeout = int(settingsFile.get('General', 'Autodetect').strip())
except:
DiscoverTimeout = 5
Autodetect = True
settingsFile.remove_option('General','Autodetect')
# Device list
DeviceByName = {}
if not settings.DevList:
Autodetect = True
if Autodetect == True:
print ("Beginning device auto-detection ... ")
# Try to support multi-homed broadcast better
try:
devices = broadlink.discover(DiscoverTimeout,listen_address,broadcast_address)
except:
devices = broadlink.discover(DiscoverTimeout,listen_address)
backupSettings()
try:
broadlinkControlIniFile = open(path.join(settings.applicationDir, 'settings.ini'), 'w')
for device in devices:
try:
device.hostname = socket.gethostbyaddr(device.host[0])[0]
if "." in device.hostname:
device.hostname = device.hostname.split('.')[0]
except:
device.hostname = "Broadlink" + device.type.upper()
if device.hostname in DeviceByName:
device.hostname = "%s-%s" % (device.hostname, str(device.host).split('.')[3])
DeviceByName[device.hostname] = device
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink device found')
sys.exit(2)
if len(devices) > 1:
logging.error('More than one Broadlink device found (' +
', '.join([d.type + '/' + d.host[0] + '/' + ':'.join(format(s, '02x') for s in d.mac[::-1]) for d in devices]) +
')')
sys.exit(2)
return configure_device(devices[0], topic_prefix)
elif device_type == 'multiple_lookup':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink devices found')
sys.exit(2)
mqtt_multiple_prefix_format = cf.get('mqtt_multiple_subprefix_format', None)
devices_dict = {}
for device in devices:
mqtt_subprefix = mqtt_multiple_prefix_format.format(
type=device.type,
host=device.host[0],
mac='_'.join(format(s, '02x') for s in device.mac[::-1]),
mac_nic='_'.join(format(s, '02x') for s in device.mac[2::-1]))
device = configure_device(device, topic_prefix + mqtt_subprefix)
devices_dict[mqtt_subprefix] = device
return devices_dict
elif device_type == 'test':
return configure_device(TestDevice(cf), topic_prefix)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink device found')
sys.exit(2)
if len(devices) > 1:
logging.error('More than one Broadlink device found (' +
', '.join([d.type + '/' + d.host[0] + '/' + ':'.join(format(s, '02x') for s in d.mac[::-1]) for d in devices]) +
')')
sys.exit(2)
return configure_device(devices[0], topic_prefix)
elif device_type == 'multiple_lookup':
local_address = cf.get('local_address', None)
lookup_timeout = cf.get('lookup_timeout', 20)
devices = broadlink.discover(timeout=lookup_timeout) if local_address is None else \
broadlink.discover(timeout=lookup_timeout, local_ip_address=local_address)
if len(devices) == 0:
logging.error('No Broadlink devices found')
sys.exit(2)
mqtt_multiple_prefix_format = cf.get('mqtt_multiple_subprefix_format', None)
devices_dict = {}
for device in devices:
mqtt_subprefix = mqtt_multiple_prefix_format.format(
type=device.type,
host=device.host[0],
mac='_'.join(format(s, '02x') for s in device.mac[::-1]),
mac_nic='_'.join(format(s, '02x') for s in device.mac[2::-1]))
device = configure_device(device, topic_prefix + mqtt_subprefix)
devices_dict[mqtt_subprefix] = device
return devices_dict
elif device_type == 'test':
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')
for device in devices:
mqtt_subprefix = mqtt_multiple_prefix_format.format(
type=device.type,
host=device.host[0],
mac='_'.join(format(s, '02x') for s in device.mac[::-1]),
mac_nic='_'.join(format(s, '02x') for s in device.mac[2::-1]))
device = configure_device(device, topic_prefix + mqtt_subprefix)
devices_dict[mqtt_subprefix] = device
return devices_dict
elif device_type == 'test':
return configure_device(TestDevice(cf), topic_prefix)
else:
host = (cf.get('device_host'), 80)
mac = bytearray.fromhex(cf.get('device_mac').replace(':', ' '))
if device_type == 'rm':
device = broadlink.rm(host=host, mac=mac, devtype=0x2712)
elif device_type == 'sp1':
device = broadlink.sp1(host=host, mac=mac, devtype=0)
elif device_type == 'sp2':
device = broadlink.sp2(host=host, mac=mac, devtype=0x2711)
elif device_type == 'a1':
device = broadlink.a1(host=host, mac=mac, devtype=0x2714)
elif device_type == 'mp1':
device = broadlink.mp1(host=host, mac=mac, devtype=0x4EB5)
elif device_type == 'dooya':
device = broadlink.dooya(host=host, mac=mac, devtype=0x4E4D)
elif device_type == 'bg1':
device = broadlink.bg1(host=host, mac=mac, devtype=0x51E3)
else:
logging.error('Incorrect device configured: ' + device_type)
sys.exit(2)
return configure_device(device, topic_prefix)
return devices_dict
elif device_type == 'test':
return configure_device(TestDevice(cf), topic_prefix)
else:
host = (cf.get('device_host'), 80)
mac = bytearray.fromhex(cf.get('device_mac').replace(':', ' '))
if device_type == 'rm':
device = broadlink.rm(host=host, mac=mac, devtype=0x2712)
elif device_type == 'sp1':
device = broadlink.sp1(host=host, mac=mac, devtype=0)
elif device_type == 'sp2':
device = broadlink.sp2(host=host, mac=mac, devtype=0x2711)
elif device_type == 'a1':
device = broadlink.a1(host=host, mac=mac, devtype=0x2714)
elif device_type == 'mp1':
device = broadlink.mp1(host=host, mac=mac, devtype=0x4EB5)
elif device_type == 'dooya':
device = broadlink.dooya(host=host, mac=mac, devtype=0x4E4D)
elif device_type == 'bg1':
device = broadlink.bg1(host=host, mac=mac, devtype=0x51E3)
else:
logging.error('Incorrect device configured: ' + device_type)
sys.exit(2)
return configure_device(device, topic_prefix)
device = configure_device(device, topic_prefix + mqtt_subprefix)
devices_dict[mqtt_subprefix] = device
return devices_dict
elif device_type == 'test':
return configure_device(TestDevice(cf), topic_prefix)
else:
host = (cf.get('device_host'), 80)
mac = bytearray.fromhex(cf.get('device_mac').replace(':', ' '))
if device_type == 'rm':
device = broadlink.rm(host=host, mac=mac, devtype=0x2712)
elif device_type == 'sp1':
device = broadlink.sp1(host=host, mac=mac, devtype=0)
elif device_type == 'sp2':
device = broadlink.sp2(host=host, mac=mac, devtype=0x2711)
elif device_type == 'a1':
device = broadlink.a1(host=host, mac=mac, devtype=0x2714)
elif device_type == 'mp1':
device = broadlink.mp1(host=host, mac=mac, devtype=0x4EB5)
elif device_type == 'dooya':
device = broadlink.dooya(host=host, mac=mac, devtype=0x4E4D)
elif device_type == 'bg1':
device = broadlink.bg1(host=host, mac=mac, devtype=0x51E3)
else:
logging.error('Incorrect device configured: ' + device_type)
sys.exit(2)
return configure_device(device, topic_prefix)