Combine two media players on condition

Hi,

I’m using the Sony Bravia TV integration which more or less does exactly what I need. The only exception being information about played media in apps, which only shows as “Smart TV”. According to this issue on GitHub it’s a limitation of the API. The Chromechast integration on the other hand provides exactly that missing bit, but (obviously) doesn’t provide information about TV channels etc. So my goal is to combine the two media players into one which uses the sony entity as base and uses the source data from chromchast when sony reports “Smart TV”.

So far I’ve used a conditional card to display either of the media players, however I now have some automation relying on the data so having one merged entity would be quite helpful. Is there any way to have eg an universal media player with this kind of logic?

Conditional card configuration for reference:

  - type: custom:state-switch
    entity: >
      {% if not is_state("media_player.sony_bravia_tv", 'off') and not
      is_state("media_player.tv", "off") and
      is_state_attr('media_player.sony_bravia_tv', 'media_title', "Smart TV")
      and not is_state_attr('media_player.tv', 'app_name', "Netflix") %}
        cast
      {% else %}
        sony
      {% endif %}
    states:
      cast:
        type: media-control
        entity: media_player.tv
      sony:
        type: media-control
        entity: media_player.sony_bravia_tv

Look at the template media player integration.

I think its universal media player you want Universal Media Player - Home Assistant

1 Like

For those also searching this: The Universal Media Player got a new attribute in 2023.7 called active_child_template which now allows to properly handle this case, apparently exactly because of the sony integration. The docs provide a minimal solution, based on that I created a little more detailed one:

- platform: universal
  name: Sony Bravia
  unique_id: sony_bravia
  children:
    - media_player.sony_bravia_cast
    - media_player.sony_bravia_psk
  active_child_template: >
    {% if not is_state("media_player.sony_bravia_psk", 'off') and not is_state("media_player.sony_bravia_cast", "off") and is_state_attr('media_player.sony_bravia_psk', 'media_title', "Smart TV") and not is_state_attr('media_player.sony_bravia_cast', 'app_name', "Netflix") %}
      media_player.sony_bravia_cast
    {% else %}
      media_player.sony_bravia_psk
    {% endif %}
  browse_media_entity: media_player.sony_bravia_psk
  commands:
    turn_on:
      service: media_player.turn_on
      target:
        entity_id: media_player.sony_bravia_psk
    turn_off:
      service: media_player.turn_off
      target:
        entity_id: media_player.sony_bravia_psk
    volume_up:
      service: media_player.volume_up
      target:
        entity_id: media_player.sony_bravia_psk
    volume_down:
      service: media_player.volume_down
      target:
        entity_id: media_player.sony_bravia_psk
    volume_set:
      service: media_player.volume_set
      target:
        entity_id: media_player.sony_bravia_psk
      data:
        volume_level: "{{ volume_level }}"
    volume_mute:
      service: media_player.volume_mute
      target:
        entity_id: media_player.sony_bravia_psk
    select_source:
      service: media_player.select_source
      target:
        entity_id: media_player.sony_bravia_psk
      data:
        source: "{{ source }}"

  attributes:
    is_volume_muted: media_player.sony_bravia_psk|is_volume_muted
    volume_level: media_player.sony_bravia_psk|volume_level
    source_list: media_player.sony_bravia_psk|source_list

The differences:

  • All commands/attributes except the source are always taken from the integration (gives some more option and “turning off” the cast player would only end the cast instead of turning off the tv)
  • Netflix is using the integration instead of the cast. The reason here is that while netflix identifies as “Netflix” using cast, it doesn’t provide any additional information and ignores any commands like pausing so while using the integration displays “Smart TV” instead of “Netflix” at least pausing/resuming works

Would you be able to help me out with this? I have a Nuvo whole home audio amplifier that has 6 zones. I have it integrated into HA and I now have 6 different media player entities. I can create the universal media player no problem but in doing so I lose independent control over the 6 different media players. How would I go about setting up the universal media player to only control the media players that have their input source on 1.

I don’t think you can do that.