# Lighting Package

# -----------------------------------------------------------------------------
# Lighting State Helpers
input_select:
  ht_lighting_mode:
    name: Home Theater Lighting Mode
    options:
      - Normal        # Lights Dim on Playing/Pause, Low on Stopped
      - Theater       # Lights Off on Playing, Dim on Pause, No Change on Stopped
      - Disabled      # No Lighting Control
    initial: Normal

input_button:
  ht_reload_lights:
    name: 'Reload Home Theater Lights'
    icon: mdi:lightbulb-on-outline

# -----------------------------------------------------------------------------
# Lighting Scenes
scene:

# Nighstand Scenes
  - name: JP Nightstand High
    entities:
      light.jp_nightstand_light:
        state: "on"
        brightness: 255

  - name: JP Nightstand Low
    entities:
      light.jp_nightstand_light:
        state: "on"
        brightness: 100

  - name: JP Nightstand Dim
    entities:
      light.jp_nightstand_light:
        state: "on"
        brightness: 50

  - name: JP Nightstand Off
    entities:
      light.jp_nightstand_light:
        state: "off"

  - name: Jen Nightstand High
    entities:
      light.jen_nightstand_light:
        state: "on"
        brightness: 255

  - name: Jen Nightstand Low
    entities:
      light.jen_nightstand_light:
        state: "on"
        brightness: 100

  - name: Jen Nightstand Dim
    entities:
      light.jen_nightstand_light:
        state: "on"
        brightness: 50

  - name: Jen Nightstand Off
    entities:
      light.jen_nightstand_light:
        state: "off"


  - name: Both Nightstands High
    entities:
      light.jp_nightstand_light:
        state: "on"
        brightness: 255
      light.jen_nightstand_light:
        state: "on"
        brightness: 255

  - name: Both Nightstands Low
    entities:
      light.jp_nightstand_light:
        state: "on"
        brightness: 100
      light.jen_nightstand_light:
        state: "on"
        brightness: 100

  - name: Both Nightstands Dim
    entities:
      light.jp_nightstand_light:
        state: "on"
        brightness: 50
      light.jen_nightstand_light:
        state: "on"
        brightness: 50

  - name: Both Nightstands Off
    entities:
      light.jp_nightstand_light:
        state: "off"
      light.jen_nightstand_light:
        state: "off"

# Home Theater Lighting Scenes
  - name: Home Theater Normal
    entities:
      light.living_room_light:
        state: "on"
        brightness: 128

  - name: Home Theater Low
    entities:
      light.living_room_light:
        state: "on"
        brightness: 80

  - name: Home Theater Dim
    entities:
      light.living_room_light:
        state: "on"
        brightness: 32

  - name: Home Theater Off
    entities:
      light.living_room_light: "off"

# Dining Room Scenes
  - name: Dining Room Dim
    entities:
      light.dining_room_light:
        state: "on"
        brightness: 64

  - name: Dining Room Full
    entities:
      light.dining_room_light:
        state: "on"
        brightness: 255

  - name: Dining Room Off
    entities:
      light.dining_room_light:
        state: "off"

# Outdoor/Patio Scenes
  - name: Pergola Low
    entities:
      light.pergola_lights:
        state: "on"
        brightness: 128

  - name: Pergola Full
    entities:
      light.pergola_lights:
        state: "on"
        brightness: 255

  - name: Pergola Off
    entities:
      light.pergola_lights:
        state: "off"

# -----------------------------------------------------------------------------
# Lighting Scripts
script:

  # Update Home Theater Lighting State
  ht_player_lighting_update:
    alias: Home Theater Lighting State Update
    mode: restart
    sequence:
      - alias: Only run during Twilight/Night
        condition: "{{ state_attr('sun.sun', 'elevation') < 6.0 }}"

      # Select Home Theater Player State
      - choose:
          # Playing
          - conditions: "{{ is_state('input_select.ht_player_state', 'Playing') }}"
            sequence:
              - choose:
                  - conditions: "{{ is_state('input_select.ht_lighting_mode', 'Normal') }}"
                    sequence:
                      - service: scene.turn_on
                        target:
                          entity_id: scene.home_theater_dim
                        data:
                          transition: 6
                  - conditions: "{{ is_state('input_select.ht_lighting_mode', 'Theater') }}"
                    sequence:
                      - service: scene.turn_on
                        target:
                          entity_id: scene.home_theater_off
                        data:
                          transition: 6

          # Paused
          - conditions: "{{ is_state('input_select.ht_player_state', 'Paused') }}"
            sequence:
              - choose:
                  - conditions: "{{ is_state('input_select.ht_lighting_mode', 'Normal') }}"
                    sequence:
                      - service: scene.turn_on
                        target:
                          entity_id: scene.home_theater_dim
                        data:
                          transition: 6
                  - conditions: "{{ is_state('input_select.ht_lighting_mode', 'Theater') }}"
                    sequence:
                      - service: scene.turn_on
                        target:
                          entity_id: scene.home_theater_dim
                        data:
                          transition: 6

          # Stopped
          - conditions: "{{ is_state('input_select.ht_player_state', 'Stopped') }}"
            sequence:
              - choose:
                  - conditions: "{{ is_state('input_select.ht_lighting_mode', 'Normal') }}"
                    sequence:
                      - service: scene.turn_on
                        target:
                          entity_id: scene.home_theater_low
                        data:
                          transition: 6

# -----------------------------------------------------------------------------
# Lighting Automations
automation:

# Automations for Home Theater
  - alias: Home Theater Lighting
    mode: queued
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: input_button.ht_reload_lights
      - platform: state
        entity_id: input_select.ht_player_state
      - platform: numeric_state
        entity_id: sun.sun
        attribute: elevation
        below: 6.0
    action:
      - service: script.ht_player_lighting_update

# Automations for Bedroom Nightstand Lamps
  - alias: Jen Nightstand Lamp
    mode: single
    trigger:
      - device_id: 98d9a86c7838fdbfcbdaeceef9a98f36
        domain: zha
        platform: device
        type: remote_button_short_press
        subtype: remote_button_short_press
    condition: []
    action:
      - type: toggle
        device_id: 444b8c5db71e96e192187688e1140d9a
        entity_id: light.jen_nightstand_light
        domain: light

  - alias: J.P. Nightstand Lamp
    mode: single
    trigger:
      - device_id: f223ed24fe135bfd6bb7006dddb6b9f8
        domain: zha
        platform: device
        type: remote_button_short_press
        subtype: remote_button_short_press
    condition: []
    action:
      - type: toggle
        device_id: a9a219b3e184e045a40959605b4c2be0
        entity_id: light.jp_nightstand_light
        domain: light

  - alias: Nighstand Lamps Auto
    mode: single
    trigger:
      - platform: state
        entity_id: media_player.living_room_receiver
        to: 'off'
    condition:
      - condition: time
        after: '20:00:00'
        before: '01:00:00'
    action:
      - service: scene.turn_on
        target:
          entity_id: scene.jen_nightstand_high

# Automations for Presence Changes
  - alias: Turn On Porch Light after Chorale
    trigger:
      - platform: zone
        entity_id: person.jpk
        zone: zone.chorale_performance
        event: leave
      - platform: zone
        entity_id: person.jpk
        zone: zone.chorale_rehearsal
        event: leave
      - platform: zone
        entity_id: person.jpk
        zone: zone.chorale_rehearsal_alt
        event: leave
    condition:
      - condition: state
        alias: "Sun down"
        entity_id: sun.sun
        state: "below_horizon"
    mode: queued
    action:
      - service: light.turn_on
        entity_id: light.porch_light
