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':