Media_player.template

Tags: #<Tag:0x00007f7394ad9b48> #<Tag:0x00007f7394ad9a30>

Hi everyone,

I’m using the very nice App ControllerX (ControllerX. Bring full functionality to light and media player controllers) to control the volume of the current playing media player.

But a problem one of my media players (an old Harman Kardon) doesn’t have any way to control it but with IR-remote. So I setup an IR-sender with esphome but problem ControllerX expects a media_player entity so it can use the services set_volume(or in the dev version volume_up/down).

After searching I didn’t found a way to create my own media_player entity like you do with switches and lights (templating) So I started to create my own custom_component


At this moment it doesn’t have all the actions that a normal media_player has but it’s usable.

There’s one downside: to use this component you need to override all the template entities!
(if this is wrong please correct me)

How to install:

  1. Create a ‘custom_components’ folder in your config folder(if it doesn’t exist)
  2. Create ‘template’ folder in the custom_components folder
  3. Copy all the file from https://github.com/home-assistant/core/tree/dev/homeassistant/components/template to this folder (I read that this is needed otherwise the component isn’t overriden)
  4. Copy my media_player.py to this folder
  5. Create your config like following example:
media_player:
  - platform: template
    media_players:
      receiver:
        friendly_name: Receiver
        current_source_template: "{{ states('input_text.selected_source') }}"
        value_template: >
          {% if is_state("input_boolean.receiver_on", "on") -%}
            on
          {%- else -%}
            off
          {%- endif %}
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: switch.receiver_on
        turn_off:
          service: switch.turn_on
          data_template:
            entity_id: switch.receiver_off
        volume_up:
          service: switch.turn_on
          data_template:
            entity_id: switch.volume_up
        volume_down:
          service: switch.turn_on
          data_template:
            entity_id: switch.vol
        input_templates:
          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

Hope someone finds this usefull and if there’s anything wrong create a issue!

2 Likes

After searching I didn’t found a way to create my own media_player entity like you do with switches and lights (templating)

But there is the universal media player?

True but this needs existing media players (children) and you can control these children using switches. This component creates a media player from scratch.

Just making sure your efforts are worth it :+1:

@Sennevds
Maybe it’s better to pick a dedicated platform-name instead of template, that way you wouldn’t need to copy all files from original HA template components. Eventually like this project: https://github.com/custom-components/media_player.braviatv_psk

1 Like

Good idea. Still don’t get it why you should copy everything if you only want to override one file or add a file but maybe I should change it.

Can anyone please share config for implementing Harmony Hub receiver (I have Logitech Z5500) to this component.
I was experimenting but can not get around it to get it working.

I can control it through script but It would be much nicer to control it with media player which I can also port to HomeKit.

z5500_volume_up:
  alias: Z-5500 Volume Up
  sequence:
    service: remote.send_command
    data:
      entity_id: remote.living_room
      command:
        - VolumeUp
      device: 59435264

Thanks!

@mathew007 you could try something like this

media_player:
  - platform: media_player_template
    media_players:
      receiver:
        friendly_name: Receiver
        volume_up:
          service: remote.send_command
          data:
            entity_id: remote.living_room
            command:
              - VolumeUp
            device: 59435264
        volume_down:
          service: remote.send_command
          data:
            entity_id: remote.living_room
            command:
              - VolumeDown
            device: 59435264

Thanks for quick reply.
I’ve tried your suggestion but I get this error:

Invalid config for [media_player.media_player_template]: required key not provided @ data['media_players']['receiver']['turn_off']. Got None required key not provided @ data['media_players']['receiver']['turn_on']. Got None required key not provided @ data['media_players']['receiver']['value_template']. Got None. (See /config/configuration.yaml, line 147). Please check the docs at https://github.com/Sennevds/media_player.template

You need to have the turn_on and turn_off parameters.
You also need a value_template but if you don’t have a way to know if the receiver is on or off you could have a template that returns on

Thanks!
Now I am getting closer :slight_smile:

media_player:
  - platform: media_player_template
    media_players:
      receiver:
        friendly_name: Receiver
        value_template: >
          {% if is_state_attr('remote.living_room', 'current_activity', 'Listen to Music') -%}
            on
          {%- else -%}
            off
          {%- endif %}
        turn_on:
          service: remote.turn_on
          data:
            entity_id: remote.living_room
            activity: 'Listen to Music'
        turn_off:
          service: remote.turn_on
          data:
            entity_id: remote.living_room
            activity: 'PowerOff'
        volume_up:
          service: remote.send_command
          data:
            entity_id: remote.living_room
            command:
              - VolumeUp
            device: 59435264
        volume_down:
          service: remote.send_command
          data:
            entity_id: remote.living_room
            command:
              - VolumeDown
            device: 59435264
        mute:
          service: remote.send_command
          data:
            entity_id: remote.living_room
            command:
              - Mute
            device: 59435264

On media card there is only power button without volume buttons. For them to show up I have to click on more options. Is there a way for these buttons to show on the card?

use this one instead of the build in https://github.com/kalkih/mini-media-player

1 Like