custom_components/ble_monitor/__init__.py
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
    """Set up BLE Monitor from a config entry."""
    _LOGGER.debug("Initializing BLE Monitor entry (config entry): %s", config_entry)

    # Prevent unload to be triggered each time we update the config entry
    global UPDATE_UNLISTENER
    if UPDATE_UNLISTENER:
        UPDATE_UNLISTENER()

    if not config_entry.unique_id:
        hass.config_entries.async_update_entry(config_entry, unique_id=config_entry.title)

    _LOGGER.debug("async_setup_entry: domain %s", CONFIG_YAML)

    config = {}
    hci_list = []
    bt_mac_list = []

    if not CONFIG_YAML:
        # Configuration in UI
        for key, value in config_entry.data.items():
            config[key] = value

        for key, value in config_entry.options.items():
            config[key] = value

        config[CONFIG_IS_FLOW] = True
        if CONF_DEVICES not in config:
            config[CONF_DEVICES] = []
        else:
            # device configuration is taken from yaml, but yaml config already removed
            # save unique IDs (only once)
            if "ids_from_name" in config:
                devlist = config[CONF_DEVICES]
                for dev_idx, dev_conf in enumerate(devlist):
                    if CONF_NAME in dev_conf:
                        devlist[dev_idx][CONF_UNIQUE_ID] = dev_conf[CONF_NAME]
                del config["ids_from_name"]

        if not config[CONF_BT_INTERFACE]:
            default_hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(DEFAULT_BT_INTERFACE)]
            hci_list.append(int(default_hci))
            bt_mac_list.append(str(DEFAULT_BT_INTERFACE))
        else:
            bt_interface_list = config[CONF_BT_INTERFACE]
            for bt_mac in bt_interface_list:
                hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(bt_mac)]
                hci_list.append(int(hci))
                bt_mac_list.append(str(bt_mac))
    else:
        # Configuration in YAML
        for key, value in CONFIG_YAML.items():
            config[key] = value
        _LOGGER.warning("Available Bluetooth interfaces for BLE monitor: %s", BT_MAC_INTERFACES)

        if config[CONF_HCI_INTERFACE]:
            # Configuration of BT interface with hci number
            for hci in CONFIG_YAML[CONF_HCI_INTERFACE]:
                try:
                    hci_list.append(int(hci))
                    bt_mac = BT_INTERFACES.get(hci)
                    if bt_mac:
                        bt_mac_list.append(str(bt_mac))
                    else:
                        _LOGGER.error("Bluetooth interface hci%i is not available", hci)
                except ValueError:
                    _LOGGER.error("Bluetooth interface hci%i is not available", hci)
        else:
            # Configuration of BT interface with mac address
            CONF_BT_INTERFACES = [x.upper() for x in CONFIG_YAML[CONF_BT_INTERFACE]]
            for bt_mac in CONF_BT_INTERFACES:
                try:
                    hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(bt_mac)]
                    hci_list.append(int(hci))
                    bt_mac_list.append(str(bt_mac))
                except ValueError:
                    _LOGGER.error("Bluetooth interface with MAC address %s is not available", bt_mac)

    if not hci_list:
        # Fall back in case no hci interfaces are added
        default_hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(DEFAULT_BT_INTERFACE)]
        hci_list.append(int(default_hci))
        bt_mac_list.append(str(DEFAULT_BT_INTERFACE))
        _LOGGER.warning("No configured Bluetooth interfaces was found, using default interface instead")

    config[CONF_HCI_INTERFACE] = hci_list
    config[CONF_BT_INTERFACE] = bt_mac_list

    hass.config_entries.async_update_entry(config_entry, data={}, options=config)

    _LOGGER.debug("async_setup_entry: %s", config)

    UPDATE_UNLISTENER = config_entry.add_update_listener(_async_update_listener)

    _LOGGER.debug("HCI interface is %s", config[CONF_HCI_INTERFACE])

    blemonitor = BLEmonitor(config)
    hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, blemonitor.shutdown_handler)
    blemonitor.start()

    hass.data[DOMAIN] = {}
    hass.data[DOMAIN]["blemonitor"] = blemonitor
    hass.data[DOMAIN]["config_entry_id"] = config_entry.entry_id

    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(config_entry, component)
        )

    return True
Set up BLE Monitor from a config entry.
Set up BLE Monitor from a config entry.
Set up BLE Monitor from a config entry .
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
    _LOGGER.debug("Initializing BLE Monitor entry (config entry): %s", config_entry)
    global UPDATE_UNLISTENER
    if UPDATE_UNLISTENER:
        UPDATE_UNLISTENER()
    if not config_entry.unique_id:
        hass.config_entries.async_update_entry(config_entry, unique_id=config_entry.title)
    _LOGGER.debug("async_setup_entry: domain %s", CONFIG_YAML)
    config = {}
    hci_list = []
    bt_mac_list = []
    if not CONFIG_YAML:
        for key, value in config_entry.data.items():
            config[key] = value
        for key, value in config_entry.options.items():
            config[key] = value
        config[CONFIG_IS_FLOW] = True
        if CONF_DEVICES not in config:
            config[CONF_DEVICES] = []
        else:
            if "ids_from_name" in config:
                devlist = config[CONF_DEVICES]
                for dev_idx, dev_conf in enumerate(devlist):
                    if CONF_NAME in dev_conf:
                        devlist[dev_idx][CONF_UNIQUE_ID] = dev_conf[CONF_NAME]
                del config["ids_from_name"]
        if not config[CONF_BT_INTERFACE]:
            default_hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(DEFAULT_BT_INTERFACE)]
            hci_list.append(int(default_hci))
            bt_mac_list.append(str(DEFAULT_BT_INTERFACE))
        else:
            bt_interface_list = config[CONF_BT_INTERFACE]
            for bt_mac in bt_interface_list:
                hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(bt_mac)]
                hci_list.append(int(hci))
                bt_mac_list.append(str(bt_mac))
    else:
        for key, value in CONFIG_YAML.items():
            config[key] = value
        _LOGGER.warning("Available Bluetooth interfaces for BLE monitor: %s", BT_MAC_INTERFACES)
        if config[CONF_HCI_INTERFACE]:
            for hci in CONFIG_YAML[CONF_HCI_INTERFACE]:
                try:
                    hci_list.append(int(hci))
                    bt_mac = BT_INTERFACES.get(hci)
                    if bt_mac:
                        bt_mac_list.append(str(bt_mac))
                    else:
                        _LOGGER.error("Bluetooth interface hci%i is not available", hci)
                except ValueError:
                    _LOGGER.error("Bluetooth interface hci%i is not available", hci)
        else:
            CONF_BT_INTERFACES = [x.upper() for x in CONFIG_YAML[CONF_BT_INTERFACE]]
            for bt_mac in CONF_BT_INTERFACES:
                try:
                    hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(bt_mac)]
                    hci_list.append(int(hci))
                    bt_mac_list.append(str(bt_mac))
                except ValueError:
                    _LOGGER.error("Bluetooth interface with MAC address %s is not available", bt_mac)
    if not hci_list:
        default_hci = list(BT_INTERFACES.keys())[list(BT_INTERFACES.values()).index(DEFAULT_BT_INTERFACE)]
        hci_list.append(int(default_hci))
        bt_mac_list.append(str(DEFAULT_BT_INTERFACE))
        _LOGGER.warning("No configured Bluetooth interfaces was found, using default interface instead")
    config[CONF_HCI_INTERFACE] = hci_list
    config[CONF_BT_INTERFACE] = bt_mac_list
    hass.config_entries.async_update_entry(config_entry, data={}, options=config)
    _LOGGER.debug("async_setup_entry: %s", config)
    UPDATE_UNLISTENER = config_entry.add_update_listener(_async_update_listener)
    _LOGGER.debug("HCI interface is %s", config[CONF_HCI_INTERFACE])
    blemonitor = BLEmonitor(config)
    hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, blemonitor.shutdown_handler)
    blemonitor.start()
    hass.data[DOMAIN] = {}
    hass.data[DOMAIN]["blemonitor"] = blemonitor
    hass.data[DOMAIN]["config_entry_id"] = config_entry.entry_id
    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(config_entry, component)
        )
    return True
async def async_setup_entry ( hass : HomeAssistant , config_entry : ConfigEntry ) : _LOGGER . debug ( "Initializing BLE Monitor entry (config entry): %s" , config_entry ) global UPDATE_UNLISTENER if UPDATE_UNLISTENER : UPDATE_UNLISTENER ( ) if not config_entry . unique_id : hass . config_entries . async_update_entry ( config_entry , unique_id = config_entry . title ) _LOGGER . debug ( "async_setup_entry: domain %s" , CONFIG_YAML ) config = { } hci_list = [ ] bt_mac_list = [ ] if not CONFIG_YAML : for key , value in config_entry . data . items ( ) : config [ key ] = value for key , value in config_entry . options . items ( ) : config [ key ] = value config [ CONFIG_IS_FLOW ] = True if CONF_DEVICES not in config : config [ CONF_DEVICES ] = [ ] else : if "ids_from_name" in config : devlist = config [ CONF_DEVICES ] for dev_idx , dev_conf in enumerate ( devlist ) : if CONF_NAME in dev_conf : devlist [ dev_idx ] [ CONF_UNIQUE_ID ] = dev_conf [ CONF_NAME ] del config [ "ids_from_name" ] if not config [ CONF_BT_INTERFACE ] : default_hci = list ( BT_INTERFACES . keys ( ) ) [ list ( BT_INTERFACES . values ( ) ) . index ( DEFAULT_BT_INTERFACE ) ] hci_list . append ( int ( default_hci ) ) bt_mac_list . append ( str ( DEFAULT_BT_INTERFACE ) ) else : bt_interface_list = config [ CONF_BT_INTERFACE ] for bt_mac in bt_interface_list : hci = list ( BT_INTERFACES . keys ( ) ) [ list ( BT_INTERFACES . values ( ) ) . index ( bt_mac ) ] hci_list . append ( int ( hci ) ) bt_mac_list . append ( str ( bt_mac ) ) else : for key , value in CONFIG_YAML . items ( ) : config [ key ] = value _LOGGER . warning ( "Available Bluetooth interfaces for BLE monitor: %s" , BT_MAC_INTERFACES ) if config [ CONF_HCI_INTERFACE ] : for hci in CONFIG_YAML [ CONF_HCI_INTERFACE ] : try : hci_list . append ( int ( hci ) ) bt_mac = BT_INTERFACES . get ( hci ) if bt_mac : bt_mac_list . append ( str ( bt_mac ) ) else : _LOGGER . error ( "Bluetooth interface hci%i is not available" , hci ) except ValueError : _LOGGER . error ( "Bluetooth interface hci%i is not available" , hci ) else : CONF_BT_INTERFACES = [ x . upper ( ) for x in CONFIG_YAML [ CONF_BT_INTERFACE ] ] for bt_mac in CONF_BT_INTERFACES : try : hci = list ( BT_INTERFACES . keys ( ) ) [ list ( BT_INTERFACES . values ( ) ) . index ( bt_mac ) ] hci_list . append ( int ( hci ) ) bt_mac_list . append ( str ( bt_mac ) ) except ValueError : _LOGGER . error ( "Bluetooth interface with MAC address %s is not available" , bt_mac ) if not hci_list : default_hci = list ( BT_INTERFACES . keys ( ) ) [ list ( BT_INTERFACES . values ( ) ) . index ( DEFAULT_BT_INTERFACE ) ] hci_list . append ( int ( default_hci ) ) bt_mac_list . append ( str ( DEFAULT_BT_INTERFACE ) ) _LOGGER . warning ( "No configured Bluetooth interfaces was found, using default interface instead" ) config [ CONF_HCI_INTERFACE ] = hci_list config [ CONF_BT_INTERFACE ] = bt_mac_list hass . config_entries . async_update_entry ( config_entry , data = { } , options = config ) _LOGGER . debug ( "async_setup_entry: %s" , config ) UPDATE_UNLISTENER = config_entry . add_update_listener ( _async_update_listener ) _LOGGER . debug ( "HCI interface is %s" , config [ CONF_HCI_INTERFACE ] ) blemonitor = BLEmonitor ( config ) hass . bus . async_listen ( EVENT_HOMEASSISTANT_STOP , blemonitor . shutdown_handler ) blemonitor . start ( ) hass . data [ DOMAIN ] = { } hass . data [ DOMAIN ] [ "blemonitor" ] = blemonitor hass . data [ DOMAIN ] [ "config_entry_id" ] = config_entry . entry_id for component in PLATFORMS : hass . async_create_task ( hass . config_entries . async_forward_entry_setup ( config_entry , component ) ) return True
Set up BLE Monitor from a config entry.
Set up BLE Monitor from a config entry .
"""Set up BLE Monitor from a config entry.""" # Prevent unload to be triggered each time we update the config entry # Configuration in UI # device configuration is taken from yaml, but yaml config already removed # save unique IDs (only once) # Configuration in YAML # Configuration of BT interface with hci number # Configuration of BT interface with mac address # Fall back in case no hci interfaces are added
hass
HomeAssistant
config_entry
ConfigEntry