This topic will be to show a new architecture of the “configuration.yaml” file that allows multiple scenes / scripts to be used in a single component with a single input select, or one for each scene / script, such as the following images :
For the exemplification of the service of scenes was done the integration with the component lifx_cloud, already for exemplification of the services of choice of playlist and of source was made an adaptation of the service offered by GigabitGuy on here.
Firstly, there was a need to reuse code to possible the use of scripts in the singular form and in the components created and to separate code in multiple folders for a better organization. For this the technique was used splitting up the configuration as following:
group: !include_dir_merge_named groups/
automation: !include_dir_merge_list automations/
script: !include_dir_merge_named scripts/
The group/ folder was organized this way (ls group/):
complete_scenes.yaml multiples_scenes.yaml spotify_playlist.yaml
The automations/ folder was organized this way (ls automations/):
spotify_playlist.yaml
The scripts/ folder was organized this way (ls scripts/):
complete_scenes.yaml multiples_scenes.yaml spotify_playlist.yaml
In addition to these settings, it was necessary to create a new folder to reuse the code inside of the scripts called data_template_script/. This new folder stores the body of the script for reuse. In this way, the data_template_script/ folder was organized like this (ls data_template_script/):
scenes_for_complete_case.yaml scenes.yaml spotify_playlist_for_complete_case.yaml spotify_playlist.yaml spotify_source.yaml
The input select:
input_select:
spotify_playlist:
name: 'Playlist:'
options:
- Escolher roupa
- Festa
- Romantico
- Relaxante
- Trabalhar de dia
icon: mdi:spotify
spotify_source:
name: 'Dispositivo:'
options:
- GoogleCast
- iPhone de Artur
- Web Player (Firefox)
initial: GoogleCast
icon: mdi:speaker-wireless
scenes_ligth:
name: 'Cenário de iluminação:'
options:
- Escolher roupa
- Festa
- Romantico
- Relaxante
- Trabalhar de dia
initial: Trabalhar de dia
icon: mdi:lightbulb-on
complete_scenes:
name: 'Cenário completo:'
options:
- Escolher roupa
- Festa
- Romantico
- Relaxante
- Trabalhar de dia
initial: Romantico
icon: mdi:monitor-multiple
groups/spotify_playlist.yaml:
home_spotify_controls:
name: Spotify Playlist
control: hidden
entities:
- input_select.spotify_playlist
- input_select.spotify_source
- script.spotify_music
- input_number.spotify_volume
- input_boolean.spotify_shuffle
groups/multiples_scenes.yaml :
multiples_scenes:
name: Multiplos Cenários
entities:
- input_select.spotify_playlist
- input_select.spotify_source
- input_select.scenes_ligth
- script.multiples_scenes
groups/complete_scenes.yaml:
complete_scenes:
name: Cenários Completos
entities:
- input_select.complete_scenes
- script.complete_scenes
scripts/spotify_playlist.yaml:
spotify_music:
alias: "Play selected spotify playlist"
sequence:
- service: media_player.select_source
data_template: !include ../data_template_script/spotify_source.yaml
- service: media_player.play_media
data_template: !include ../data_template_script/spotify_playlist.yaml
scripts/multiples_scenes.yaml:
multiples_scenes:
alias: "Play selected spotify playlist"
sequence:
- service: scene.turn_on
data_template: !include ../data_template_script/scenes.yaml
- service: media_player.select_source
data_template: !include ../data_template_script/spotify_source.yaml
- service: media_player.play_media
data_template: !include ../data_template_script/spotify_playlist.yaml
scripts/complete_scenes.yaml:
complete_scenes:
alias: "Qual é o sentimento do seu ambiente?"
sequence:
- service: scene.turn_on
data_template: !include ../data_template_script/scenes_for_complete_case.yaml
- service: media_player.play_media
data_template: !include ../data_template_script/spotify_playlist_for_complete_case.yaml
data_template_script/scenes.yaml:
entity_id: >
{% if is_state("input_select.scenes_ligth", "Escolher roupa") %} scene.escolher_roupa
{% elif is_state("input_select.scenes_ligth", "Festa") %} scene.festa
{% elif is_state("input_select.scenes_ligth", "Romantico") %} scene.romantico
{% elif is_state("input_select.scenes_ligth", "Relaxante") %} scene.relaxante
{% elif is_state("input_select.scenes_ligth", "Filme") %} scene.filme
{% elif is_state("input_select.scenes_ligth", "Trabalhar de dia") %} scene.trabalhar_de_dia
{% endif %}
data_template_script/scenes_for_complete_case.yaml:
entity_id: >
{% if is_state("input_select.complete_scenes", "Escolher roupa") %} scene.escolher_roupa
{% elif is_state("input_select.complete_scenes", "Festa") %} scene.festa
{% elif is_state("input_select.complete_scenes", "Romantico") %} scene.romantico
{% elif is_state("input_select.complete_scenes", "Relaxante") %} scene.relaxante
{% elif is_state("input_select.complete_scenes", "Trabalhar de dia") %} scene.trabalhar_de_dia
{% endif %}
data_template_script/spotify_playlist_for_complete_case.yaml:
entity_id: media_player.spotify
media_content_type: playlist
media_content_id: >
{% if is_state("input_select.complete_scenes", "Escolher roupa") %} spotify:playlist:1KNClsO09ktIImSIXLlSTR
{% elif is_state("input_select.complete_scenes", "Trabalhar de dia") %} spotify:playlist:37i9dQZF1DWZ2DWnNKZTXs
{% elif is_state("input_select.complete_scenes", "Relaxante") %} spotify:album:6uP9caYdtuhk0WZ4bMtUc0
{% elif is_state("input_select.complete_scenes", "Festa") %} spotify:playlist:23zy2U5H1khLwlqFAkUyXr
{% elif is_state("input_select.complete_scenes", "Romantico") %} spotify:user:mendesiasmin_:playlist:6c4CefxKB86sPOdubDKwPK
{% endif %}
data_template_script/spotify_playlist.yaml:
entity_id: media_player.spotify
media_content_type: playlist
media_content_id: >
{% if is_state("input_select.spotify_playlist", "Escolher roupa") %} spotify:playlist:1KNClsO09ktIImSIXLlSTR
{% elif is_state("input_select.spotify_playlist", "Trabalhar de dia") %} spotify:playlist:37i9dQZF1DWZ2DWnNKZTXs
{% elif is_state("input_select.spotify_playlist", "Relaxante") %} spotify:album:6uP9caYdtuhk0WZ4bMtUc0
{% elif is_state("input_select.spotify_playlist", "Festa") %} spotify:playlist:23zy2U5H1khLwlqFAkUyXr
{% elif is_state("input_select.spotify_playlist", "Romantico") %} spotify:user:mendesiasmin_:playlist:6c4CefxKB86sPOdubDKwPK
{% endif %}
spotify_source.yaml:
entity_id: media_player.spotify
media_content_type: playlist
media_content_id: >
{% if is_state("input_select.spotify_playlist", "Escolher roupa") %} spotify:playlist:1KNClsO09ktIImSIXLlSTR
{% elif is_state("input_select.spotify_playlist", "Trabalhar de dia") %} spotify:playlist:37i9dQZF1DWZ2DWnNKZTXs
{% elif is_state("input_select.spotify_playlist", "Relaxante") %} spotify:album:6uP9caYdtuhk0WZ4bMtUc0
{% elif is_state("input_select.spotify_playlist", "Festa") %} spotify:playlist:23zy2U5H1khLwlqFAkUyXr
{% elif is_state("input_select.spotify_playlist", "Romantico") %} spotify:user:mendesiasmin_:playlist:6c4CefxKB86sPOdubDKwPK
{% endif %}
These files exemplify the use of multiple services or scenes as if it were a single scene. If you need to add a new service/scene to the complete solution, you must separate your service into the data_template_script and place the call within the “complete_scenes” or “multiple_scenes” script.