Programming question: how do I read a list of numbers from configuration.yaml?

In my code now I’m using a list of numbers to select certain parts of a json-payload with 23 items:

id = [6,7,8,15,16,17,18,19,20,22]
x = 0
for data in list:
        "some code"
        if x in id:
               print (payload)
         x = x+1

I only want to use the json-items 6, 7, 8, 15, 16, etc.
This works very nicely.

Now I want to import this list from configuration.yaml.

What should I write under:

CONFIG_SCHEMA = vol.Schema({
    DOMAIN: vol.Schema({
                  ....
                  vol.Optional(CONF_XYZ):cv.xyz
                  ....

in my code to import this list? And what should I write in configuration.yaml to make this work?

  vol.Optional(CONF_XYZ): cv.ensure_list(cv.positive_int)

Also, is there any reason why you are iterating across data and incrementing a counter? There could be faster methods to that process. If you could post more of your code I could help you with that.

1 Like

Thanks! I’ll try this tonight.

My not yet finished code is on my github: https://github.com/bouwew/aurum-home-assistant/tree/dev
In the file init.py

Yeah, my coding-skills are minimal, I’m more of a system-engineering kind-of-person.
My code comes from examples found on the web, from looking at other custom-components with similar functions, and what I learned many years ago in Uni, so all very basic and straightforward.

FWIW, I think this would work better:

vol.Optional(CONF_XYZ): vol.All(cv.ensure_list, [cv.positive_int]),

E.g.:

>>> vol.Schema({'xyz': vol.All(cv.ensure_list, [cv.positive_int])})({'xyz': '5'})
{'xyz': [5]}
>>> vol.Schema({'xyz': cv.ensure_list(cv.positive_int)})({'xyz': '5'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/phil/repos/home-assistant/venv/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/home/phil/repos/home-assistant/venv/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 589, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/home/phil/repos/home-assistant/venv/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 427, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: expected a list for dictionary value @ data['xyz']
1 Like

Yah, I didn’t try it. Was just taking a stab in the dark and it looked correct.

And what do I then write in configuration.yaml?
Just something like list = [6,7,8,15,16,17,18,19,20,22]?

list: 6,7,8,15,16,17,18,19,20,22

or

list: 
  - 6
  - 7
  - etc

Of course, : :smile:
Thanks! I’ll try this soon.

It works (meaning hassio ha check results in ok) when I use this:

CONF_LIST = '_list'
DEFAULT_LIST = '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22'
....
vol.Optional(CONF_LIST, default=DEFAULT_LIST): vol.All(cv.ensure_list, [cv.string]),
....

But, I think I will need to translate the list of strings to a list of integers?

Sorry to be picky, but since I’ve already butted in once… :wink:

list: 6,7,8,15,16,17,18,19,20,22

would actually need to be:

list: [6,7,8,15,16,17,18,19,20,22]

The first would be interpreted as a string and not validate correctly. Unless you changed cv.ensure_list to cv.ensure_list_csv, but I’ve already been yelled at once for using that where it’s not intended. lol!

@bouwew, not sure why you would use this:

CONF_LIST = '_list'
DEFAULT_LIST = '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22'
....
vol.Optional(CONF_LIST, default=DEFAULT_LIST): vol.All(cv.ensure_list, [cv.string]),
....

I would recommend:

CONF_LIST = 'list'
DEFAULT_LIST = list(range(23))
....
vol.Optional(CONF_LIST, default=DEFAULT_LIST): vol.All(cv.ensure_list, [cv.positive_int]),
....

This will make sure the configured input will always be returned as a list where the entries have been converted to int’s if necessary and have values of at least zero.

1 Like

Please bear with me, I’m very inexperienced with this stuff :slight_smile:

But, thanks for the suggestions, I will try them.

1 Like

so it’s only intended on entity_id’s? I was just trying to make it easier on the config setup. Meaning no dashes or because people tend to not understand that.

Yeah, pretty much. I used cv.ensure_list_csv in my custom life360 component, but during the “standardization” process I was told not to use it. I thought it made things more compact when the entered list was small, but the comment (can’t find it anymore) was they wanted YAML to be YAML, or something to that effect.

1 Like

Based on the advice from @pnbruckner, it is working now :smile:

@petro, I tried to follow your idea as well, but I got stuck on transforming the input-string to a list of integers. The solutions provided on Stackoverflow did not work, I tried several, did not give the wanted result, instead, always an error. So, I gave up.

Gents, thanks very much for your suggestions!

The solution I was trying to provide is what @pnbruckner provided. I’ve never added a list to voluptuous before so I was taking stabs in the dark.