blueprint:
  name: TV — Live channel button (Freeview / aerial)
  description: >-
    Press a Zigbee button to switch the TV to the Live TV (Freeview)
    source and tune to a specific channel — the dongle's Cast app is
    closed first so the casts and live TV don't fight.


    Used in the colour-coded TV setup for a "News" button that tunes
    BBC News on Freeview channel 231, but works for any Freeview
    channel number.


    What it does on each press:

    1. Pause + close any current Cast session.

    2. Wake the TV (CEC) if it's off; wait for it to be ready.

    3. Switch to the Live TV source on the LG.

    4. Send the channel digits via the WebOS API, then ENTER.


    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-live-channel-button.yaml

  input:
    button_device:
      name: Zigbee button
      description: The Zigbee button that triggers the channel.
      selector:
        device:
          integration: zha

    cast_media_player:
      name: TV Cast media player
      description: >-
        The Google Cast media player (used to close any Cast session
        before switching to live TV).
      selector:
        entity:
          domain: media_player

    webos_tv:
      name: LG WebOS TV
      description: >-
        The LG WebOS TV media player entity (used for power state,
        source switching, and digit-press for tuning).
      selector:
        entity:
          domain: media_player

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

    live_tv_source_name:
      name: Live TV source name
      description: >-
        The exact source name the LG TV uses for the Freeview / aerial
        input. Usually "Live TV" on LG OLEDs.
      default: Live TV
      selector:
        text:

    channel_number:
      name: Channel number
      description: >-
        The Freeview channel number to tune to. Each digit will be
        sent as a separate WebOS button press, then ENTER.
        Example: 231 for BBC News.
      selector:
        number:
          min: 1
          max: 9999
          step: 1
          mode: box

    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

variables:
  channel: !input channel_number

action:

  # 1. Pause and close the Cast session.
  - 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 (CEC wake if needed).
  - 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 to the Live TV source.
  - service: media_player.select_source
    target:
      entity_id: !input webos_tv
    data:
      source: !input live_tv_source_name

  # 4. Brief grace period before sending button presses.
  - delay:
      seconds: 3

  # 5. Send each digit in turn, then ENTER. `channel | string | list`
  #    splits e.g. 231 → ["2","3","1"]; the trailing ENTER confirms.
  - repeat:
      for_each: "{{ channel | string | list }}"
      sequence:
        - service: webostv.button
          data:
            entity_id: !input webos_tv
            button: "{{ repeat.item }}"
        - delay:
            seconds: 0.5
  - service: webostv.button
    data:
      entity_id: !input webos_tv
      button: ENTER
