Reusable (input) components

Is it possible to create reusable components?

For example, I’ve some input_select components, one for each day of the week.
The code for these components are nearly the same (besides the identifier and name).

It depends on what you are doing. Can you post all your code? I can see if it can be simplified.

Here is a link to the code on my GitHub repository. Thanks for taking a look!

https://github.com/maartenpaauw/home-assistant-config/tree/master/input_selections

Are you trying to reuse the same selection device (input_select) or automation?

I mean, I’m creating 7 (nearly) the same components (monday to sunday). Which is the same code. I was wondering if it is possible to create or reuse 2 or more (7 in my case) “the same” components. Something like:

dinner_planner_[variable]:
  name: [variable]
  options: !include options/meals.yaml
  icon: mdi:food

You can by creating your own custom component to create a bunch with python. Unfortunately, there is no way to automatically declare ‘interface objects’ using yaml. You can only automate where templating is aloud.

Use customize_glob. Example…

2 Likes

TIL

@maartenpaauw, ignore my last post!

Thanks a lot!

No problem, thanks for the help :slight_smile:

I’ve to come back to this solution. I can’t get it to work.

The error I got:

    - Invalid config for [input_select]: required key not provided @ data['input_select']['dinner_planner_monday']['options']. Got None. (See /Volumes/docker/home-assistant/./configuration.yaml, line 86). Please check the docs at https://home-assistant.io/components/input_select/

The code (under customize_glob):

input_select.dinner_planner_*:
  config:
    icon: mdi:food
    options:
      - "Test 1"
      - "Test 2"
      - "Test 3"

The input_select code:

dinner_planner_monday:
  name: Maandag
  # options: !include options/meals.yaml

I also couldn’t find the key config listed in the possible values list.

Yeah, for sub-key options (I don’t know what they’re called!) you would use an include for the repeated code :+1:

Okay thanks for letting me know :grinning:.

1 Like