Get multiple variables from the configuration.yaml

hey everyone i’m trying to make a component with a list of attributes obtained from the config.yaml file . what i have now is:

my_component:
  username: my_username
  password: !secret a_password

my component has the following code in it to retrieve the attributes from the config file. I’m not actually sure how it works, but it works.

from homeassistant.const import (CONF_PASSWORD, CONF_USERNAME)

DOMAIN = "my_component"

CONFIG_SCHEMA = vol.Schema({
  DOMAIN: vol.Schema({
    vol.Required(CONF_USERNAME): cv.string,
    vol.Required(CONF_PASSWORD): cv.string,
  }),
}, extra=vol.ALLOW_EXTRA)

def setup(hass, config):
  email = config[DOMAIN].get(CONF_USERNAME, "no username")
  password = config[DOMAIN].get(CONF_PASSWORD, "no password")

what is want to do is this is to have something like the following in my config file and somehow retrieve all the attributes in my component:

#config.yaml
my_component:
  usernames:
    - my_username
    - my_username2
  passwords:
    - !secret password1
    - !secret password2    

the config yaml doesn’t have to work exactly like this, perhaps getting the passwords from a list in the secrets.yaml could work or maybe somehow a list that has both the username and the password for each entry. what i’m just trying to do is get multiple usernames and corresponding passwords and load them into the component.

I suggest finding a component that has the format that you want, then go to the GitHub repository for that component and emulate the config.

google cast would be a good example, instead of using host and port, use username: and password:

https://github.com/home-assistant/home-assistant/blob/master/homeassistant/components/media_player/cast.py

your config would look like this:

my_component:
 - user: foo
   password: foo
 - user: foo2
   password: foo2
1 Like

thanks for the pointer. i think this will help me figure it out.