async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Broadlink device sensors.""" host = config[CONF_HOST] mac_addr = config[CONF_MAC] model = config[CONF_TYPE] name = config[CONF_NAME] timeout = config[CONF_TIMEOUT] update_interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL) if model in RM4_TYPES: api = blk.rm4((host, DEFAULT_PORT), mac_addr, None) check_sensors = api.check_sensors else: api = blk.a1((host, DEFAULT_PORT), mac_addr, None) check_sensors = api.check_sensors_raw api.timeout = timeout device = BroadlinkDevice(hass, api) connected = await device.async_connect() if not connected: raise PlatformNotReady broadlink_data = BroadlinkData(device, check_sensors, update_interval) sensors = [ BroadlinkSensor(name, broadlink_data, variable) for variable in config[CONF_MONITORED_CONDITIONS] ] async_add_entities(sensors, True)
def run(self, host, mac, file=None): host = (host, 80) mac = bytearray.fromhex(mac.replace(':', ' ')) device = broadlink.rm4(host=host, mac=mac, devtype=0x51da) device.auth() if self.radioRf.isChecked(): self.record_rf(device, file) elif self.radioIr.isChecked(): self.record(device, file)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Broadlink remote.""" host = config[CONF_HOST] mac_addr = config[CONF_MAC] model = config[CONF_TYPE] timeout = config[CONF_TIMEOUT] name = config[CONF_NAME] unique_id = f"remote_{hexlify(mac_addr).decode('utf-8')}" if unique_id in hass.data.setdefault(DOMAIN, {}).setdefault(COMPONENT, []): _LOGGER.error("Duplicate: %s", unique_id) return hass.data[DOMAIN][COMPONENT].append(unique_id) if model in RM_TYPES: api = blk.rm((host, DEFAULT_PORT), mac_addr, None) else: api = blk.rm4((host, DEFAULT_PORT), mac_addr, None) api.timeout = timeout device = BroadlinkDevice(hass, api) code_storage = Store(hass, CODE_STORAGE_VERSION, f"broadlink_{unique_id}_codes") flag_storage = Store(hass, FLAG_STORAGE_VERSION, f"broadlink_{unique_id}_flags") remote = BroadlinkRemote(name, unique_id, device, code_storage, flag_storage) connected, loaded = await asyncio.gather(device.async_connect(), remote.async_load_storage_files()) if not connected: hass.data[DOMAIN][COMPONENT].remove(unique_id) raise PlatformNotReady if not loaded: _LOGGER.error("Failed to set up %s", unique_id) hass.data[DOMAIN][COMPONENT].remove(unique_id) return async_add_entities([remote], False)
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Broadlink device sensors.""" host = config[CONF_HOST] mac_addr = config[CONF_MAC] model = config[CONF_TYPE] name = config[CONF_NAME] timeout = config[CONF_TIMEOUT] update_interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL) if model in RM4_TYPES: api = blk.rm4((host, DEFAULT_PORT), mac_addr, None) check_sensors = api.check_sensors else: api = blk.a1((host, DEFAULT_PORT), mac_addr, None) check_sensors = api.check_sensors_raw api.timeout = timeout broadlink_data = BroadlinkData(api, check_sensors, update_interval) dev = [] for variable in config[CONF_MONITORED_CONDITIONS]: dev.append(BroadlinkSensor(name, broadlink_data, variable)) add_entities(dev, True)
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Broadlink switches.""" devices = config.get(CONF_SWITCHES) slots = config.get("slots", {}) host = config.get(CONF_HOST) mac_addr = config.get(CONF_MAC) friendly_name = config.get(CONF_FRIENDLY_NAME) model = config[CONF_TYPE] retry_times = config.get(CONF_RETRY) def generate_rm_switches(switches, broadlink_device): """Generate RM switches.""" return [ BroadlinkRMSwitch( object_id, config.get(CONF_FRIENDLY_NAME, object_id), broadlink_device, config.get(CONF_COMMAND_ON), config.get(CONF_COMMAND_OFF), retry_times, ) for object_id, config in switches.items() ] def get_mp1_slot_name(switch_friendly_name, slot): """Get slot name.""" if not slots[f"slot_{slot}"]: return f"{switch_friendly_name} slot {slot}" return slots[f"slot_{slot}"] if model in RM_TYPES: broadlink_device = blk.rm((host, DEFAULT_PORT), mac_addr, None) hass.add_job(async_setup_service, hass, host, broadlink_device) switches = generate_rm_switches(devices, broadlink_device) elif model in RM4_TYPES: broadlink_device = blk.rm4((host, DEFAULT_PORT), mac_addr, None) hass.add_job(async_setup_service, hass, host, broadlink_device) switches = generate_rm_switches(devices, broadlink_device) elif model in SP1_TYPES: broadlink_device = blk.sp1((host, DEFAULT_PORT), mac_addr, None) switches = [ BroadlinkSP1Switch(friendly_name, broadlink_device, retry_times) ] elif model in SP2_TYPES: broadlink_device = blk.sp2((host, DEFAULT_PORT), mac_addr, None) switches = [ BroadlinkSP2Switch(friendly_name, broadlink_device, retry_times) ] elif model in MP1_TYPES: switches = [] broadlink_device = blk.mp1((host, DEFAULT_PORT), mac_addr, None) parent_device = BroadlinkMP1Switch(broadlink_device, retry_times) for i in range(1, 5): slot = BroadlinkMP1Slot( get_mp1_slot_name(friendly_name, i), broadlink_device, i, parent_device, retry_times, ) switches.append(slot) broadlink_device.timeout = config.get(CONF_TIMEOUT) try: broadlink_device.auth() except OSError: _LOGGER.error("Failed to connect to device") add_entities(switches)
import broadlink IP = 'xxx.xxx.xxx.xxx' MAC = 'xx:xx:xx:xx:xx:xx' remote = broadlink.rm4((IP, 80), MAC, 0x27c2) projectorPower_bin = b'&\x00X\x00\x00\x01&\x93\x15\x11\x13\x12\x14\x11\x14\x12\x14\x11\x14\x11\x14\x11\x14\x12\x136\x146\x155\x145\x146\x146\x145\x155\x15\x10\x14\x12\x13\x12\x14\x11\x14\x11\x14\x12\x14\x11\x14\x11\x146\x146\x136\x155\x146\x136\x146\x155\x14\x00\x05!\x00\x01*I\x14\x00\x0c[\x00\x01*I\x13\x00\r\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' projectorPower_hex = '2600580000012693151113121411141214111411141114121336143615351435143614361435153515101412131214111411141214111411143614361336153514361336143615351400052100012a4914000c5b00012a4913000d050000000000000000000000000000' remote.auth() remote.send_data(projectorPower_bin)
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)
======= mac='_'.join(format(s, '02x') for s in device.mac), mac_nic='_'.join(format(s, '02x') for s in device.mac[3::])) >>>>>>> upstream2/master 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)
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)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Broadlink switches.""" host = config[CONF_HOST] mac_addr = config[CONF_MAC] friendly_name = config[CONF_FRIENDLY_NAME] model = config[CONF_TYPE] timeout = config[CONF_TIMEOUT] slots = config[CONF_SLOTS] devices = config[CONF_SWITCHES] def generate_rm_switches(switches, broadlink_device): """Generate RM switches.""" return [ BroadlinkRMSwitch( object_id, config.get(CONF_FRIENDLY_NAME, object_id), broadlink_device, config.get(CONF_COMMAND_ON), config.get(CONF_COMMAND_OFF), ) for object_id, config in switches.items() ] def get_mp1_slot_name(switch_friendly_name, slot): """Get slot name.""" if not slots[f"slot_{slot}"]: return f"{switch_friendly_name} slot {slot}" return slots[f"slot_{slot}"] if model in RM_TYPES: api = blk.rm((host, DEFAULT_PORT), mac_addr, None) broadlink_device = BroadlinkDevice(hass, api) switches = generate_rm_switches(devices, broadlink_device) elif model in RM4_TYPES: api = blk.rm4((host, DEFAULT_PORT), mac_addr, None) broadlink_device = BroadlinkDevice(hass, api) switches = generate_rm_switches(devices, broadlink_device) elif model in SP1_TYPES: api = blk.sp1((host, DEFAULT_PORT), mac_addr, None) broadlink_device = BroadlinkDevice(hass, api) switches = [BroadlinkSP1Switch(friendly_name, broadlink_device)] elif model in SP2_TYPES: api = blk.sp2((host, DEFAULT_PORT), mac_addr, None) broadlink_device = BroadlinkDevice(hass, api) switches = [BroadlinkSP2Switch(friendly_name, broadlink_device)] elif model in MP1_TYPES: api = blk.mp1((host, DEFAULT_PORT), mac_addr, None) broadlink_device = BroadlinkDevice(hass, api) parent_device = BroadlinkMP1Switch(broadlink_device) switches = [ BroadlinkMP1Slot( get_mp1_slot_name(friendly_name, i), broadlink_device, i, parent_device, ) for i in range(1, 5) ] api.timeout = timeout connected = await broadlink_device.async_connect() if not connected: raise PlatformNotReady if model in RM_TYPES or model in RM4_TYPES: hass.async_create_task( async_setup_service(hass, host, broadlink_device)) async_add_entities(switches)