
script:
  tv_twitch:
    icon: mdi:twitch
    alias: TV · Start Twitch stream
    sequence:
      - service: script.tv_prepare_for_playback
      - service: androidtv.adb_command
        entity_id: media_player.nvidia_shield
        data_template:
          command: "am start -a android.intent.action.VIEW -d twitch://stream/{{ channel }}"

  tv_bulsatcom_toggle:
    icon: mdi:remote-tv
    alias: TV · Bulsatcom toggle
    sequence:
      - service: script.bulsatcom_power
      - service: media_player.turn_on
        entity_id: media_player.living_room_tv
      - service: script.hdmi_switch_1
      - service: media_player.select_source
        entity_id: media_player.living_room_tv
        data:
          source: Switch

  latest_pds:
    alias: Play the latest PDS
    sequence:
      - service: media_extractor.play_media
        entity_id: media_player.shield
        data_template:
          media_content_id: "{{ state_attr('sensor.philip_defranco', 'url') }}"
          media_content_type: video/youtube

  tv_prepare_for_playback:
    alias: TV · Prepare for playback
    sequence:
      - service: media_player.unjoin
        entity_id: media_player.living_room
      - service: media_player.select_source
        entity_id: media_player.living_room
        data:
          source: TV
      - service: media_player.volume_set
        entity_id: media_player.living_room
        data_template:
          volume_level: "{{ volume_level | default(0.3) }}"
      - service: media_player.turn_on
        entity_id: media_player.nvidia_shield
      - service: media_player.select_source
        entity_id: media_player.living_room_tv
        data:
          source: Shield

  shield_screensaver:
    sequence:
      - service: media_player.turn_on
        entity_id: media_player.nvidia_shield
      - service: androidtv.adb_command
        entity_id: media_player.nvidia_shield
        data:
          command: "adb shell am start -n \"com.android.systemui/.Somnambulator\""

automation:
  - alias: TV · Turn on
    trigger:
      - platform: webostv.turn_on
        entity_id: media_player.living_room_tv
    action:
      - service: wake_on_lan.send_magic_packet
        data:
          mac: 78:5D:C8:C9:DB:76

  - alias: TV · Join Beam to Sonos group when off
    trigger:
      platform: state
      entity_id: media_player.living_room_tv
      to: "off"
    condition:
      condition: state
      entity_id: sensor.house_mode
      state: "day"
    action:
      - service: script.sonos_join_bedroom
      - service: logbook.log
        data:
          name: "📺 Media Module · "
          message: "Sonos Group · TV off - joining Beam to Sonos Group ··"
          domain: media_player

  - alias: TV · State · Sync TV state
    mode: single
    trigger:
      platform: state
      entity_id: media_player.living_room_tv
    action:
      - choose:
          - conditions: "{{ trigger.to_state.state == 'off' and not is_state('media_player.nvidia_shield', 'off') }}"
            sequence:
              - service: media_player.turn_off
                entity_id: media_player.nvidia_shield
          - conditions: "{{ trigger.to_state.state == 'on' }}"
            sequence:
              - service: media_player.turn_on
                entity_id: media_player.nvidia_shield
      - service: logbook.log
        data_template:
          name: "📺 Media Module · "
          message: "Sync TV state · Turning Shield {{ trigger.to_state.state }} - TV is {{ trigger.to_state.state }} ··"
          domain: media_player

  - alias: TV · State · Sync Shield state
    trigger:
      - platform: state
        entity_id: media_player.nvidia_shield
        to: "off"
        id: "off"
      - platform: state
        entity_id: media_player.nvidia_shield
        from: "off"
        to: "idle"
        id: "on"
      - platform: state
        entity_id: media_player.nvidia_shield
        from: "off"
        to: "playing"
        id: "on"
    action:
      - choose:
          - conditions: "{{ trigger.id == 'on' and not is_state('media_player.living_room_tv', 'on') }}"
            sequence:
              - service: media_player.turn_on
                entity_id: media_player.living_room_tv
              - delay: "00:00:15"
              - service: media_player.select_source
                entity_id: media_player.living_room_tv
                data:
                  source: "Shield"
          - conditions: "{{ trigger.id == 'off' and not is_state('media_player.living_room_tv', 'off') }}"
            sequence:
              - service: media_player.turn_off
                entity_id: media_player.living_room_tv
      - service: logbook.log
        data_template:
          name: "📺 Media Module · "
          message: "Sync TV state · Turning TV {{ trigger.id }} - Shield is {{ trigger.id }} ··"
          domain: media_player

  - alias: TV · Prepare for casting
    trigger:
      platform: state
      entity_id: media_player.nvidia_shield
      to: 'playing'
    condition: "{{ is_state_attr('media_player.nvidia_shield', 'source', 'Google Cast') }}"
    action:
      - service: script.sonos_join_tv
      - service: logbook.log
        data:
          name: "📺 Media Module · "
          message: "TV · Google Cast - Joining Sonoses"
          domain: media_player
      - delay: "00:00:10"
      - service: media_player.select_source
        entity_id: media_player.living_room_tv
        data:
          source: Shield

homeassistant:
  customize:
    media_player.living_room_tv:
      icon: mdi:television
      friendly_name: LG TV
    media_player.nvidia_shield:
      icon: mdi:set-top-box
      friendly_name: Nvidia Shield
