Incoherent behaviour with Universal Media Player made of Native+Cast children

I’m trying to build a Universal Media Player blending together the native and Google Cast integrations of my Sony Bravia. This to overcome the limitations in the native integration due to Sony not exposing the app_name in attributes.

This is my config (stripping out not relevant code for conciseness):

- platform: universal
  name: Sony Bravia TV
  unique_id: sony_bravia_combined
  device_class: tv
  children:
    - media_player.sony_bravia_psk
    - media_player.sony_bravia_cast
  active_child_template: >
    {% if state_attr('media_player.sony_bravia_psk', 'media_content_id') %}
        media_player.sony_bravia_psk
    {% endif %}
  attributes:
    source: media_player.sony_bravia_psk|source
    source_list: media_player.sony_bravia_psk|source_list
    volume_level: media_player.sony_bravia_psk|volume_level
    app_id: media_player.sony_bravia_cast|app_id
    app_name: media_player.sony_bravia_cast|app_name
  browse_media_entity: media_player.sony_bravia_psk
  commands:    
     [...]
    select_source:
      service: media_player.select_source
      data:
        entity_id: media_player.sony_bravia_psk
        source: "{{ source }}"
    media_play:
      service: media_player.media_play
      target:
        entity_id: media_player.sony_bravia_psk
    play_media:
      service: media_player.play_media
      data:
        entity_id: media_player.sony_bravia_psk
        media_content_type: "{{ media_content_type }}"
        media_content_id: "{{ media_content_id }}"

This way I can create some toggle switch to tune to a specific source and stats counter to count the time the switch is on (to measure streaming services usage btw). Like this one:

    sony_bravia_tv_youtube_switch:
      value_template: >
        {{ state_attr('media_player.sony_bravia_tv', 'app_name') | lower == 'youtube' }}
      turn_on:
        - if:
            - condition: template
              value_template: >
                {{ states('media_player.sony_bravia_tv') in ['off'] }}
          then:
            - service: media_player.turn_on
              metadata: {}
              data: {}
              target:
                entity_id: media_player.sony_bravia_tv
            - delay: 6
        - service: media_player.play_media
          data:
            media_content_id: YouTube
            media_content_type: app
          target:
            entity_id: media_player.sony_bravia_tv
      turn_off:
        - service: media_player.turn_off
          target:
            entity_id: media_player.sony_bravia_tv
          data: {}

But I get a very incoherent behaviour, without an apparent pattern.I think i must tweak the Universal setup above, but I don’t know how :frowning:

  1. When I’m into an app without playing any media, I’ve this states. As you may see, ‘app_name’ is not pushed into the universal media player, as attribute override should I think, so the template switch stays off.

  1. I start playing a media in the app, state of universal goes to ‘playing’ reflecting Cast status and everything could be ok (not the way i want the switch to behave but ok). Unfortunately this is not consistent, and depends of the app. For example, Jellyfin stays on ‘idle’ even during playing, thus the switch stays off all the time (I know it’s probably a bug of the app, but I’m searching a workaround in HA in the meantime they eventually fix)

  1. From YouTube I go back to apps home page: Universal switch back to ‘on’ while cast goes on ‘idle’. In Universal app_id, and app_name disappears again while app_name in cast switch to a completely random app (Jellyfin again, in this case; i suspect it’s running in background or something)

  1. From here I go to Prime Video again and start playing media. No way, Cast stay stuck on idle/Jellyfin. I go to YouTube, still stuck; but this time when I start playing a media, it goes correctly into the playing state.

  2. Once “unstuck”, going to Prime Video again produces the expected results

I can do the same using Netflix. This app apparently has another bug: it’s always in “playing” state, even if no media is really playing. Nonetheless, this side effect will wipe out the floating idle state from the Cast.

  1. Once in a coherent state again, I finally go the apps home page. Cast goes to ‘off’

  1. If i commute off the universal media player at this state, everything works as expected: both children go to off, thus universal goes to off.

The problem pops out if I try to commute off when Cast is ‘idle’ (for ex. in Prime Video without playing any media) or ‘playing’.

Here I get an inconsistent behaviour again. I suspect some kind of racing conditions here.

Sometimes it goes off (both children off), sometimes it stays idle (psd=off, cast=idle → universal=idle). If universal dangles in idle, I cannot commute on anymore (the only way is to use the phisical remote or the toggle switches above, which uses turn_on services unregarding of the state).

I’m aware it is not necessarily a bug, but the combined behaviour is a mess indeed.

Some bugs in Cast integration? Maybe
Some bugs in android apps? Probably

Nonetheless I’m really getting insane to find a way to fix those tweaking the Universal Media routing.

any idea? :confounded:

Ok, toggle states incoherency is probably easy to solve using state_template in Universal definition this way:

state_template: >
    {{ states('media_player.sony_bravia_psk') }}

While about Jellyfin always in idle, this is somewhat that had surfaced already.

Unfortunately, the proposed workaround (set chromecast version to ‘unstable’ in playback settings) doesn’t work

digging into other issues; will post here my experiments