blueprint:
  name: TV — Stream button (radio / internet stream)
  description: >-
    Press a Zigbee button to play an internet audio or video stream on the
    TV via Cast. Used in the colour-coded TV setup for a "Radio" button
    that plays Classic FM, but works for any Cast-compatible stream URL.


    What it does on each press:

    1. Pause + close any current Cast session (avoids the "stale Cast
       session silently ignores commands" failure mode).

    2. If the TV is off, send a CEC wake via the Android TV Remote and
       wait up to 15 seconds for the WebOS entity to come on.

    3. Switch to the HDMI source the dongle is on.

    4. Play the stream URL.


    Listens for all Third Reality button command variants for firmware
    reliability.


    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-stream-button.yaml

  input:
    button_device:
      name: Zigbee button
      description: The Zigbee button that triggers stream playback.
      selector:
        device:
          integration: zha

    cast_media_player:
      name: TV Cast media player
      description: >-
        The Google Cast / Chromecast media player on the TV
        (e.g. media_player.living_room_tv).
      selector:
        entity:
          domain: media_player

    webos_tv:
      name: LG WebOS TV
      description: >-
        The LG WebOS TV media player entity (used to detect TV power
        state and to switch HDMI source).
      selector:
        entity:
          domain: media_player

    tv_remote:
      name: Android TV Remote
      description: >-
        The Android TV Remote entity for the dongle
        (e.g. remote.living_room_tv). Used to wake the TV via CEC when
        it's off.
      selector:
        entity:
          domain: remote

    hdmi_source_name:
      name: HDMI source name on the TV
      description: >-
        The exact source name the LG TV uses for the HDMI port the
        Cast dongle is on. Default matches an LG OLED's "Living room TV".
      default: Living room TV
      selector:
        text:

    stream_url:
      name: Stream URL
      description: >-
        The URL to play. Must be reachable from the Cast device.
        Example for Classic FM:
        https://media-ice.musicradio.com/ClassicFMMP3
      selector:
        text:

    media_content_type:
      name: Media content type
      description: >-
        Cast media type. Use "music" for audio streams (radio) and
        "video/mp4" or similar for video streams.
      default: music
      selector:
        select:
          options:
            - music
            - audio/mp3
            - video/mp4
          custom_value: true

    wake_timeout:
      name: TV wake timeout (seconds)
      description: >-
        How long to wait for the TV to come on after CEC wake before
        giving up and continuing anyway.
      default: 15
      selector:
        number:
          min: 5
          max: 60
          step: 1
          unit_of_measurement: seconds
          mode: slider

mode: single
max_exceeded: silent

trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input button_device
      command: remote_button_short_press
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input button_device
      command: single
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input button_device
      command: 'on'
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input button_device
      command: remote_button_long_press
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input button_device
      command: hold

action:

  # 1. Pause then close any current Cast session. Closing prevents the
  #    "idle Cast silently ignores commands" gotcha when the next play
  #    happens.
  - service: media_player.media_pause
    target:
      entity_id: !input cast_media_player
    continue_on_error: true
  - service: media_player.turn_off
    target:
      entity_id: !input cast_media_player
    continue_on_error: true

  # 2. Ensure the TV is on. If already on, skip the wake to keep response
  #    snappy. Otherwise CEC-wake via the Android TV Remote and wait for
  #    the WebOS entity to flip to "on".
  - choose:
      - conditions:
          - condition: state
            entity_id: !input webos_tv
            state: 'on'
        sequence: []
    default:
      - service: remote.turn_on
        target:
          entity_id: !input tv_remote
      - wait_for_trigger:
          - platform: state
            entity_id: !input webos_tv
            to: 'on'
        timeout:
          seconds: !input wake_timeout
        continue_on_timeout: true
      - delay:
          seconds: 2

  # 3. Switch the LG to the HDMI input the Cast dongle is on. This is
  #    needed after News (Live TV source) — without it, casts go to a
  #    black screen.
  - service: media_player.select_source
    target:
      entity_id: !input webos_tv
    data:
      source: !input hdmi_source_name

  # 4. Brief grace period before casting (the dongle needs a moment after
  #    source-switch).
  - delay:
      seconds: 3

  # 5. Cast the stream.
  - service: media_player.play_media
    target:
      entity_id: !input cast_media_player
    data:
      media_content_id: !input stream_url
      media_content_type: !input media_content_type
