blueprint:
  name: TV — YouTube auto-advance
  description: >-
    When a YouTube video on the Cast finishes, automatically play the
    next video in the playlist. One copy of this blueprint per YouTube
    show, paired with that show's tv-youtube-show-button.


    What it does:

    - Triggers when the Cast media player goes from "playing" to
      "idle" (YouTube end-of-video state).

    - Only fires if the WebOS TV is on (avoids playing into the void)
      AND if the input_select.current_show matches this show (so the
      right playlist is used).

    - Increments the show's playlist index, closes the stale Cast
      session, and casts the next video.


    Helpers needed (same ones as the show button — already created if
    you've imported tv-youtube-show-button):

    - input_select.current_show with one option per show.

    - input_number per show (the rotating playlist index).


    Part of the colour-coded TV button setup at
    https://remindmevoice.com/guide/simple-tv-buttons.

  domain: automation
  source_url: https://remindmevoice.com/blueprints/tv-youtube-auto-advance.yaml

  input:
    cast_media_player:
      name: TV Cast media player
      description: >-
        The Google Cast / Chromecast media player (e.g.
        media_player.living_room_tv). Same one your show button uses.
      selector:
        entity:
          domain: media_player

    webos_tv:
      name: LG WebOS TV
      description: >-
        The LG WebOS TV media player entity. Used as a guard — auto-
        advance only fires when the TV is on.
      selector:
        entity:
          domain: media_player

    current_show_helper:
      name: "current_show input_select helper"
      description: >-
        The input_select tracking which show is current.
      selector:
        entity:
          domain: input_select

    this_show_option:
      name: This show's option
      description: >-
        The option (in the input_select above) that represents this
        show — must match the value used in the show button blueprint
        (e.g. doc_martin, mr_bean, bgt).
      selector:
        text:

    playlist_index_helper:
      name: Playlist index helper (input_number)
      description: >-
        The same input_number the show button uses for this show.
      selector:
        entity:
          domain: input_number

    playlist_video_ids:
      name: Playlist of YouTube video IDs
      description: >-
        One YouTube video ID per line — same list as in the show
        button. Blank lines are ignored.
      selector:
        text:
          multiline: true

mode: single
max_exceeded: silent

trigger:
  - platform: state
    entity_id: !input cast_media_player
    from: playing
    to: idle

condition:
  # 1. TV is on (don't play into a black screen).
  - condition: state
    entity_id: !input webos_tv
    state: 'on'
  # 2. The current_show option matches this show. Without this,
  #    every auto-advance instance would fire on every video end.
  - condition: state
    entity_id: !input current_show_helper
    state: !input this_show_option

variables:
  idx_entity: !input playlist_index_helper
  playlist_raw: !input playlist_video_ids

action:

  # 1. Increment the playlist index modulo the playlist length.
  - service: input_number.set_value
    target:
      entity_id: !input playlist_index_helper
    data:
      value: >-
        {% set ids = playlist_raw.replace('\r', '').split('\n')
                     | map('trim') | reject('equalto', '') | list -%}
        {{ (states(idx_entity) | int(0) + 1) % (ids | length) }}

  # 2. Close the stale Cast session.
  - service: media_player.turn_off
    target:
      entity_id: !input cast_media_player
    continue_on_error: true

  # 3. Brief grace period.
  - delay:
      seconds: 3

  # 4. Cast the next video.
  - service: media_player.play_media
    target:
      entity_id: !input cast_media_player
    data:
      media_content_type: cast
      media_content_id: >-
        {% set ids = playlist_raw.replace('\r', '').split('\n')
                     | map('trim') | reject('equalto', '') | list -%}
        {% set idx = states(idx_entity) | int(0) % (ids | length) -%}
        {{ {'app_name': 'youtube', 'media_id': ids[idx]} | tojson }}
