# Media Player Package

# -----------------------------------------------------------------------------
# Media Player State Helpers
input_select:
  ht_player_state:
    name: Home Theater Player State
    options:
      - Idle
      - Playing
      - Paused
      - Stopped
      - System
      - Offline
      - Unknown
    initial: Idle

# -----------------------------------------------------------------------------
# Media Player Configuration Switches
input_boolean:
  ht_adjust_volume_hvac:
    name: Home Theater Adjust Volume for HVAC

# -----------------------------------------------------------------------------
# Media Player Scripts
script:

  # Update the Player State when an input device changes state. The state of
  # the overall player is taken from the state of the currently active input
  # device to the receiver.
  ht_player_state_update:
    alias: Home Theater Player State Update
    mode: restart
    sequence:
      - choose:
          # Receiver Off
          - conditions: "{{ is_state('media_player.living_room_receiver', 'off') }}"
            sequence:
              - service: input_select.select_option
                target:
                  entity_id: input_select.ht_player_state
                data:
                  option: Idle

          # Roku Input
          - conditions: "{{ state_attr('media_player.living_room_receiver', 'source') == 'Roku Ultra' }}"
            sequence:
              - choose:
                  - conditions: "{{ is_state('media_player.living_room_roku', 'playing') }}"
                    sequence:
                      - service: input_select.select_option
                        target:
                          entity_id: input_select.ht_player_state
                        data:
                          option: Playing

                  - conditions: "{{ is_state('media_player.living_room_roku', 'paused') }}"
                    sequence:
                      - service: input_select.select_option
                        target:
                          entity_id: input_select.ht_player_state
                        data:
                          option: Paused

                  - conditions: "{{ is_state('media_player.living_room_roku', 'on') }}"
                    sequence:
                      - service: input_select.select_option
                        target:
                          entity_id: input_select.ht_player_state
                        data:
                          option: Stopped

                  - conditions:
                      - condition: or
                        conditions:
                          - condition: template
                            value_template: "{{ is_state('media_player.living_room_roku', 'idle') }}"
                          - condition: template
                            value_template: "{{ is_state('media_player.living_room_roku', 'off') }}"
                    sequence:
                      - service: input_select.select_option
                        target:
                          entity_id: input_select.ht_player_state
                        data:
                          option: Idle

        # Fallback to Unknown State
        default:
          - service: input_select.select_option
            target:
              entity_id: input_select.ht_player_state
            data:
              option: Unknown

# -----------------------------------------------------------------------------
# Media Player Automations
automation:
  - alias: Home Theater Idle Timeout
    trigger:
      - platform: state
        entity_id: input_select.ht_player_state
        to: Stopped
        for:
          minutes: 10
    action:
      - service: input_select.select_option
        target: 
          entity_id: "{{ trigger.entity_id }}"
        data:
          option: Idle

  - alias: Home Theater Receiver State Update
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: media_player.living_room_receiver
      - platform: state
        entity_id: media_player.living_room_roku
    action:
      - service: script.ht_player_state_update

  - alias: Turn Up Volume when HVAC Fan Starts
    trigger:
      - platform: state
        entity_id: binary_sensor.hvac_fan_running
        to: "on"
    condition:
      - "{{ is_state('input_boolean.ht_adjust_volume_hvac', 'on') }}"
      - "{{ is_state('media_player.living_room_receiver', 'on') }}"
    action:
      - repeat:
          count: 6
          sequence:
            - delay:
                milliseconds: 250
            - service: media_player.volume_up
              target:
                entity_id: media_player.living_room_receiver

  - alias: Turn Down Volume when HVAC Fan Stops
    trigger:
      - platform: state
        entity_id: binary_sensor.hvac_fan_running
        from: "on"
        # The fan_state attribute turns off before the fan actually stops
        for:
          seconds: 45
    condition:
      - "{{ is_state('input_boolean.ht_adjust_volume_hvac', 'on') }}"
      - "{{ is_state('media_player.living_room_receiver', 'on') }}"
    action:
      - repeat:
          count: 6
          sequence:
            - delay:
                milliseconds: 250
            - service: media_player.volume_down
              target:
                entity_id: media_player.living_room_receiver
