Sennevds / media_player.template

Template media_player for Home Assistant
MIT License
126 stars 23 forks source link

Dynamic source list (inputs) #33

Open justr-1 opened 2 years ago

justr-1 commented 2 years ago

Hi,

is there a way to set a dynamic source list, e.g. all values from an input_select?

regards, justr

Sennevds commented 2 years ago

It's not possible at the moment. Maybe if I have some time I will add it but no promises

Sennevds commented 2 years ago

So I had some time to think about this and I don't know how to implement this. I could create a template to populate the list but what needs to happen when you select a input? How do you define the action that needs to execute. At this moment you need to define the inputs as followed:

inputs:
  source 1:
    service: input_boolean.turn_on
    data_template:
      entity_id: input_boolean.source_1
  source 2:
    service: input_boolean.turn_on
    data_template:
      entity_id: input_boolean.source_2

So you define the name of the input: "source 1" and the action what happens when you select the input:

service: input_boolean.turn_on
    data_template:
      entity_id: input_boolean.source_1

So I could implement the the names easily from an input_select but how should I define the action that needs to happen?

justr-1 commented 2 years ago

Hi Sennevds,

I was thinking about a similar solution which is implemented by the universal media player (https://www.home-assistant.io/integrations/universal/). Please take a look at the "Chromecast & Kodi control with switches" example.

The values for the source_list itself (in your case the inputs) are defined by an input_select. If the user selects an entry the "select_source" command is triggered. The source is available as a data variable.

In my case i would like to forward the selected source to a rest endpoint that is responsible for switching.

The configuration for the universional media player would be like this:

definition of the rest endpoint

  rest_command:
    change_stream:
      url: http://<ip>:<port>/clients/{{mac}}/stream/{{stream}}
      method: post

definition of the command within the media player template

      select_source:
        service: rest_command.change_stream
        data:
          mac: "000000000000"
          stream: "{{source}}"
Sennevds commented 2 years ago

Ooh that looks like a clean and easy solution. Looks like I can't think straight that I didn't think of it myself. Will see when I have the time to implement this.

Sennevds commented 2 years ago

Okay lasty question why don't you just use UniversalMediaPlayer? When I made this custom integration the universal media player needed at least one media player entity and because I didn't had one for my device I created this integration. But it looks like children are now optional so I think my integration is obsolete

justr-1 commented 2 years ago

I was testing and playing around with the universal media player before i found your solution. You are right, the children are optional. But without children i am not able to set attributes for artist, title and media image url. If i am correct, the are just completly ignored. that disturbs me...

CDaxi commented 1 year ago

Hello,

it would be great if there will be a yaml config like "source_list_template". Universal media player does not match my needs because I am using a complex setup with:

There is for example:

The goal is to get the data as these:

Example config:

template:
  - sensor:
    - name: "Multiroom Source Bathroom Active Player"
      state: "media_player.multiroom_bathroom_{% if is_state('media_player.multiroom_bathroom_airplay', 'playing') %}airplay{% else %}mopidy{% endif %}"
    - name: "Multiroom Bathroom Active Player"
      state: >
        {% set source = state_attr('media_player.snapcast_client_bathroom', 'source') %}
        {% set sourceParts = source.split(' + ') %}
        {% set hasBroadcast = sourceParts.count == 2 and sourceParts[1] == 'BC' %}
        {% if source == 'Broadcast' %}media_player.multiroom_broadcast_mopidy{%
        elif hasBroadcast and broadcast_mopidy == 'playing' %}media_player.multiroom_broadcast_mopidy{%
        else %}{{ states('sensor.multiroom_source_' ~ sourceParts[0].lower() ~ '_active_player') }}{%
        endif %}
media_players:
  - platform: media_player_template
    media_players:
      multiroom_bathroom:
        unique_id: 79a026d6-c577-4bf2-b705-c00c4d36ffea
        friendly_name: Multiroom Bathroom
        device_class: receiver
        value_template: "{{ states('media_player.snapcast_client_bathroom') }}"
        current_source_template: "{{ state_attr('media_player.snapcast_client_bathroom', 'source') }}"
        # source_list_template: "{{ state_attr('media_player.snapcast_client_bathroom', 'source_list') }}"
        current_volume_template: "{{ state_attr('media_player.snapcast_client_bathroom', 'volume_level') }}"
        current_is_muted_template: "{{ state_attr('media_player.snapcast_client_bathroom', 'is_volume_muted') }}"

        title_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'media_title') }}"
        album_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'media_album_name') }}"
        artist_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'media_artist') }}"
        current_position_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'media_position') }}"
        media_duration_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'media_duration') }}"
        media_content_type_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'media_content_type') }}"
        media_image_url_template: "{{ state_attr(states('sensor.multiroom_bathroom_active_player'), 'entity_picture') }}"

        volume_up:
          service: media_player.volume_up
          data_template:
            entity_id: "media_player.snapcast_client_bathroom"
        volume_down:
          service: media_player.volume_down
          data_template:
            entity_id: "media_player.snapcast_client_bathroom"
        set_volume:
          service: media_player.volume_set
          data_template:
            entity_id: "media_player.snapcast_client_bathroom"
            volume_level: "{{ volume }}"
        mute:
          service: media_player.volume_mute
          data_template:
            entity_id: "media_player.snapcast_client_bathroom"
            is_volume_muted: "{{ is_muted }}"
        next:
          service: media_player.media_next_track
          data_template:
            entity_id: "{{ states('sensor.multiroom_bathroom_active_player') }}"
        previous:
          service: media_player.media_previous_track
          data_template:
            entity_id: "{{ states('sensor.multiroom_bathroom_active_player') }}"
        stop:
          service: media_player.media_stop
          data_template:
            entity_id: "{{ states('sensor.multiroom_bathroom_active_player') }}"
        play:
          service: media_player.media_play
          data_template:
            entity_id: "{{ states('sensor.multiroom_bathroom_active_player') }}"
        pause:
          service: media_player.media_pause
          data_template:
            entity_id: "{{ states('sensor.multiroom_bathroom_active_player') }}"

Its works nearly perfect, only the source selection for the snapclient is not possible. It would be great if there is this template to "copy" the source list from media_player.snapclient_bathroom for example.

Thanks a lot. CDaxi