Advanced config_flow for custom integration

I’m working on a custom integration where I need multiple “accounts” and multiple “devices” within each account.

Example:

  • Account A (userA/pwdA)
    – Device 1 (name1/sn1)
    – Device 2 (name2/sn2)
  • Account B (userB/pwdB)
    – Device 3 (name3/sn3)
    – Device 4 (name4/sn4)

  • – …
    – …

This is easy enough to setup as YAML configuration but I’m not understanding if, or how, this can be handled in the UI as a config_flow…

My integration needs to create a single MQTT instance for each “account” and then subscribe to various topics for each “device” within that “account”. I need the user to be able to add (or remove) accounts and/or devices under each account…

Unless I’m missing something the docs don’t seem to address this specifically. I found a couple examples online but they are not quite the same as what I’m trying to do and the examples are older so I’m hoping the newest versions of HA can in fact do this and I have just not discovered exactly how…

Pointers, links, and code examples appreciated!

Sorry, not in a postion to give code examples but the basic premise with your config flow will be to base it on a single account but support multiple instances. Then your users will add a new instance of your integration for each account. Setting the unique id in your config flow to something account id based and validating that account has not already been added is important. Also ensuring your main code supports having multiple instances when storing any data against the hass object is key.

In order to be able to add devices to your instance, would need to understand more about whether these can be detected from the account or need to be manually added and what info needs to be provided for each device.

I believe I understand and have code examples for adding multiple “entries” (accounts in this case) via async_setup_entry using input from the UI gathered using async_step_user it is the sub-entries (devices) where I’m getting lost in terms of how to code that and/or if the UI can even accommodate it.

Yes, there is a way to obtain a list of devices linked to the account, however, the types of devices can be diverse, my integration will only support certain types, and there is not currently a definitive way to differentiate the device type by ID (serial number). Even if there were a unique device type identifier I would not want to arbitrarily link every supported device for a variety of reasons (not the least of which is that each device can easily have 100 or more sensor entities).

For now it will suffice to ask the user for the serial number of a supported device as well as a friendly name they wish to use to identify it (and it’s related entities). I just need to know if it is possible to “add an account” then “add devices to account” in such a way that devices can be added, removed, or disabled and accounts can be add/removed/disabled as a whole (with support for multiple account). I can envision how to do this operationally, for the most part, and the nested hierarchy I might create in a YAML config but I’m trying to do this in the UI with config_flow and I’m not finding much beyond basic overview and simple examples and I’m not certain where to start if I were to try to deep dive into the code base to learn the details of how it works.

This is the basic structure I’m looking at in terms of schema:

from .const import (
    CONFIG_ACCESS_KEY,
    CONFIG_ACCOUNT_ID,
    CONFIG_DEVICE_ID,
    CONFIG_DEVICE_SN,
    CONFIG_SECRET_KEY,
    CONFIG_VERSION,
    DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

STEP_ACOUNT_DATA_SCHEMA = vol.Schema(
    {
        vol.Required(CONFIG_ACCOUNT_ID): str,
        vol.Required(CONFIG_ACCESS_KEY): str,
        vol.Required(CONFIG_SECRET_KEY): str,
    }
)

STEP_DEVICE_DATA_SCHEMA = vol.Schema(
    {
        vol.Required(CONFIG_DEVICE_ID): str,
        vol.Required(CONFIG_DEVICE_SN): str,
    }
)