if device_type == 'rm': device = broadlink.rm(host=host, mac=mac, devtype=0x2712) elif device_type == 'rm4': device = broadlink.rm4(host=host, mac=mac, devtype=0x51da) 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) def configure_device(device, mqtt_prefix): device.auth() <<<<<<< HEAD logging.debug('Connected to \'%s\' Broadlink device at \'%s\' (MAC %s) and started listening for commands at MQTT topic having prefix \'%s\' ' % (device.type, device.host[0], ':'.join(format(s, '02x') for s in device.mac[::-1]), mqtt_prefix)) ======= logging.debug('Connected to \'%s\' Broadlink device at \'%s\' (MAC %s) and started listening to MQTT commands at \'%s#\' ' % (device.type, device.host[0], ':'.join(format(s, '02x') for s in device.mac), mqtt_prefix))
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') 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), mac_nic='_'.join(format(s, '02x') for s in device.mac[3::])) 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 == 'rm4': device = broadlink.rm4(host=host, mac=mac, devtype=0x51da) 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)
def get_device(cf, devices_dictionary={}): 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') 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) elif device_type == 'dict': global device_rescan_required device_rescan_required = False devices_list = json.loads(cf.get('devices_dict', '[]')) mqtt_multiple_prefix_format = cf.get('mqtt_multiple_subprefix_format', None) for device in devices_list: if pingOk(sHost=device['host'], count=3): mac = bytearray.fromhex(device['mac'].replace(':', ' '))[::-1] host = (device['host'], 80) init_func = getattr(broadlink, device['class']) deviceObj = init_func(host=host, mac=mac, devtype=int(device['devtype'], 0)) mqtt_subprefix = mqtt_multiple_prefix_format.format( type=deviceObj.type, host=deviceObj.host[0], mac='_'.join( format(s, '02x') for s in deviceObj.mac[::-1]), mac_nic='_'.join( format(s, '02x') for s in deviceObj.mac[2::-1])) if mqtt_subprefix not in devices_dictionary: device_configured = configure_device( deviceObj, topic_prefix + mqtt_subprefix) devices_dictionary[mqtt_subprefix] = device_configured print("Type: %s, host: %s, MQTT subprefix: %s" % (deviceObj.type, deviceObj.host, mqtt_subprefix)) if len(devices_list) != len(devices_dictionary): device_rescan_required = True logging.warning( 'Less devices are found than expected. Rescan is required.') return devices_dictionary 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 == 'rm4': device = broadlink.rm4(host=host, mac=mac, devtype=0x51da) 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) elif device_type == 'SmartBulb': device = broadlink.lb1(host=host, mac=mac, devtype=0x60c8) else: logging.error('Incorrect device configured: ' + device_type) sys.exit(2) return configure_device(device, topic_prefix)