# yaml-language-server: $schema=null
###############################################################################
# PACKAGE: Lounge scenes controller
#
# PURPOSE:
# - Provides selectable lounge lighting scenes from Home Assistant.
# - Scenes are selected via input_select.lounge_scene.
# - The selector resets back to "Select Scene:" after each scene is applied, so
#   the same scene can be selected repeatedly.
# - Scenes are delta-only: only entities listed in a scene are changed.
# - Manual override can block automatic scene changes.
# - Last remembered scene is tracked so TV automations can avoid changing lights
#   when the lounge was deliberately set to All Off.
#
# SUPPORTED SCENE VALUE TYPES:
# - light.*:
#     0        = turn off
#     1-100    = turn on at brightness percentage
#     "on"     = turn on
#     "off"    = turn off
#     "toggle" = toggle on/off, using 100 percent when turning on
#     "toggle_75" = if on, turn off; if off, turn on at 75 percent
#
# - switch.*:
#     "on"     = turn on
#     "off"    = turn off
#     "toggle" = toggle on/off
#
# - number.*:
#     numeric value is sent using number.set_value
#
# - input_number.*:
#     numeric value is sent using input_number.set_value
#     Example: input_number.openhasp_1_backlight_level: 180
#
# NOTES:
# - Keep on/off/toggle values quoted for clarity.
# - Numeric values can be left unquoted.
# - Only scenes not listed in excluded_scenes update input_text.lounge_last_scene.
###############################################################################

input_select:
  lounge_scene:
    name: Lounge Scene
    options:
      - "Select Scene:"
      - Bright
      - TV General
      - Balanced
      - Movie
      - Reading On/Off
      - Dining On/Off
      - Night Light
      - Warm Cozy
      - All Off
    initial: "Select Scene:"

input_boolean:
  lounge_manual_override:
    name: Lounge Lighting Scene Manual Override
    initial: off

input_text:
  lounge_last_scene:
    name: Lounge Last Scene
    initial: "All Off"
    max: 40

timer:
  lounge_override_timer:
    name: Lounge Lighting Scene Override Timer
    duration: "02:00:00"

###############################################################################
# SCRIPTS
###############################################################################
script:
  lounge_set_scene:
    alias: "Lounge: Set scene by name"
    mode: restart
    fields:
      scene_name:
        description: One of the options from input_select.lounge_scene
    sequence:
      - service: input_select.select_option
        target: { entity_id: input_select.lounge_scene }
        data:
          option: "{{ scene_name }}"

      # Wait briefly for HA to commit the new state before reading it.
      - wait_template: "{{ states('input_select.lounge_scene') == scene_name }}"
        timeout: "00:00:02"
        continue_on_timeout: true

      - service: script.lounge_apply_and_reset

  lounge_apply_scene:
    alias: "Lounge: Apply current scene"
    mode: restart
    sequence:
      - variables:
          sel: "{{ states('input_select.lounge_scene') }}"

          #####################################################################
          # EXCLUDED SCENES
          # These are temporary/action scenes and are not remembered as last.
          #####################################################################
          excluded_scenes:
            - "Select Scene:"
            - "Reading On/Off"
            - "Dining On/Off"
            #- "Night Light"

          #####################################################################
          # SCENE SETTINGS
          # Delta only - only list entities you want to change.
          #####################################################################
          scene_settings:
            Bright:
              light.esp_lounge6chdimmer_couch_spots_left: 0
              light.esp_lounge6chdimmer_couch_spots_right: 0
              #light.esp_lounge6chdimmer_dining_table_light: 100
              light.esp_lounge6chdimmer_lounge_downlights_centre: 100
              light.esp_lounge6chdimmer_lounge_downlights_south: 100
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 100
              #switch.esp_loungemiddleswitch_relay_3_downlights_north: "on"
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "on" # non dimmable pair
              light.esp_breakfastbarleds_breakfast_bar_leds: 90
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 100
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 90
              switch.lounge_cupboard_dual_usb_xus09_l2: "on"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "on"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "on"
              #switch.tasmo_ks811t_0702_lounge_3c: "on"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 100
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 100
              input_number.openhasp_1_backlight_level: 100
              number.lounge_nspanel_touchscreen_display_brightness: 100

            TV General:
              light.esp_lounge6chdimmer_couch_spots_left: 60
              light.esp_lounge6chdimmer_couch_spots_right: 60
              #light.esp_lounge6chdimmer_dining_table_light: 100
              light.esp_lounge6chdimmer_lounge_downlights_centre: 60
              light.esp_lounge6chdimmer_lounge_downlights_south: 60
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 40
              light.esp_breakfastbarleds_breakfast_bar_leds: 60
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 66
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 60
              #switch.esp_loungemiddleswitch_relay_3_downlights_north: "off" # non-dimmable pair
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "off" # non dimmable pair
              switch.lounge_cupboard_dual_usb_xus09_l2: "on"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "off"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "off"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 60
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 60
              input_number.openhasp_1_backlight_level: 30
              number.lounge_nspanel_touchscreen_display_brightness: 210

            Balanced:
              light.esp_lounge6chdimmer_couch_spots_left: 40
              light.esp_lounge6chdimmer_couch_spots_right: 40
              #light.esp_lounge6chdimmer_dining_table_light: 100
              light.esp_lounge6chdimmer_lounge_downlights_centre: 40
              light.esp_lounge6chdimmer_lounge_downlights_south: 40
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 30
              light.esp_breakfastbarleds_breakfast_bar_leds: 50
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 55
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 50
              #switch.esp_loungemiddleswitch_relay_3_downlights_north: "off" # non-dimmable pair
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "off" # non dimmable pair
              switch.lounge_cupboard_dual_usb_xus09_l2: "on"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "off"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "off"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 50
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 50
              input_number.openhasp_1_backlight_level: 30
              number.lounge_nspanel_touchscreen_display_brightness: 80

            Movie:
              light.esp_lounge6chdimmer_couch_spots_left: 20
              light.esp_lounge6chdimmer_couch_spots_right: 20
              #light.esp_lounge6chdimmer_dining_table_light: 100
              light.esp_lounge6chdimmer_lounge_downlights_centre: 10
              light.esp_lounge6chdimmer_lounge_downlights_south: 30
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 0
              light.esp_breakfastbarleds_breakfast_bar_leds: 40
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 44
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 40
              #switch.esp_loungemiddleswitch_relay_3_downlights_north: "off"
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "off" # non dimmable pair
              switch.lounge_cupboard_dual_usb_xus09_l2: "on"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "off"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "off"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 40
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 40
              input_number.openhasp_1_backlight_level: 10
              switch.esp_mainkitchenlights_relay_1_main_lights: "off"
              switch.esp_mainkitchenlights_relay_2_benchtop_lights: "off"
              number.lounge_nspanel_touchscreen_display_brightness: 40

            Dining On/Off:
              light.esp_lounge6chdimmer_dining_table_light: "toggle_100"
              #light.esp_lounge6chdimmer_dining_table_light: 60
              #light.esp_lounge6chdimmer_lounge_downlights_centre: 45
              #light.esp_lounge6chdimmer_lounge_downlights_south: 35
              #light.esp_lounge6chdimmer_lounge_rafter_buttons: 20
              #switch.tasmo_ks811t_0702_lounge_3c: "on"

            Reading On/Off:
              light.esp_lounge6chdimmer_couch_spots_left: "toggle_75"
              light.esp_lounge6chdimmer_couch_spots_right: "toggle_75"
              #light.esp_lounge6chdimmer_dining_table_light: 60
              #light.esp_lounge6chdimmer_lounge_downlights_centre: 45
              #light.esp_lounge6chdimmer_lounge_downlights_south: 35
              #light.esp_lounge6chdimmer_lounge_rafter_buttons: 20
              #switch.tasmo_ks811t_0702_lounge_3c: "on"

            Night Light:
              #light.esp_lounge6chdimmer_couch_spots_left: 20
              #light.esp_lounge6chdimmer_couch_spots_right: 20
              #light.esp_lounge6chdimmer_dining_table_light: 100
              light.esp_lounge6chdimmer_lounge_downlights_centre: 0
              light.esp_lounge6chdimmer_lounge_downlights_south: 0
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 0
              light.esp_breakfastbarleds_breakfast_bar_leds: 5
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 4
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 5
              #switch.esp_loungemiddleswitch_relay_3_downlights_north: "off"
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "off" # non dimmable pair
              switch.lounge_cupboard_dual_usb_xus09_l2: "off"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "off"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "off"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 5
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 5
              input_number.openhasp_1_backlight_level: 6
              number.lounge_nspanel_touchscreen_display_brightness: 30

            Warm Cozy:
              light.esp_lounge6chdimmer_couch_spots_left: 20
              light.esp_lounge6chdimmer_couch_spots_right: 20
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 15
              light.esp_breakfastbarleds_breakfast_bar_leds: 25
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 33
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 30
              #light.tasmo_h801_loungeled1_6180_a: 25
              #light.tasmo_h801_loungeled1_6180_b: 25
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "off" # non dimmable pair
              switch.lounge_cupboard_dual_usb_xus09_l2: "on"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "on"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "on"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 20
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 20
              input_number.openhasp_1_backlight_level: 12
              number.lounge_nspanel_touchscreen_display_brightness: 40

            All Off:
              light.esp_lounge6chdimmer_couch_spots_left: 0
              light.esp_lounge6chdimmer_couch_spots_right: 0
              light.esp_lounge6chdimmer_dining_table_light: 0
              light.esp_lounge6chdimmer_lounge_downlights_centre: 0
              light.esp_lounge6chdimmer_lounge_downlights_south: 0
              light.esp_lounge6chdimmer_lounge_rafter_buttons: 0
              #switch.esp_loungemiddleswitch_relay_3_downlights_north: "off"
              switch.lounge_nspanel_touchscreen_lounge_stair_downlights: "off" # non dimmable pair
              light.esp_breakfastbarleds_breakfast_bar_leds: 0
              number.esp_loungecabinetleds_lounge_cabinet_leds_output_set_0_100: 0
              light.esp_loungebookshelfleds_lounge_bookshelf_leds: 0
              switch.lounge_cupboard_dual_usb_xus09_l2: "off"
              switch.esp_loungesouthleftswitch_relay_1_wall_washer_left: "off"
              switch.esp_loungesouthrightswitch_relay_2_wall_washer_right: "off"
              #switch.esp_loungenorthswitch_relay_1_main_lights: "on"
              number.esp_loungepelmetleds2_lounge_pelmet_leds_a_output_set_0_100: 0
              number.esp_loungepelmetleds2_lounge_pelmet_leds_b_output_set_0_100: 0
              input_number.openhasp_1_backlight_level: 60
              #switch.esp_mainkitchenlights_relay_1_main_lights: "off"
              #switch.esp_mainkitchenlights_relay_2_benchtop_lights: "off"
              number.lounge_nspanel_touchscreen_display_brightness: 80

      #########################################################################
      # REMEMBER LAST SCENE
      #########################################################################
      - choose:
          - conditions: "{{ sel not in excluded_scenes }}"
            sequence:
              - service: input_text.set_value
                target: { entity_id: input_text.lounge_last_scene }
                data:
                  value: "{{ sel }}"

      #########################################################################
      # APPLY SELECTED SCENE
      #########################################################################
      - repeat:
          for_each: "{{ (scene_settings.get(sel, {})).keys() | list }}"
          sequence:
            - variables:
                ent: "{{ repeat.item }}"
                val: "{{ (scene_settings.get(sel, {}))[ent] }}"
                is_light: "{{ ent.startswith('light.') }}"
                is_switch: "{{ ent.startswith('switch.') }}"
                is_number_ent: "{{ ent.startswith('number.') }}"
                is_input_number_ent: "{{ ent.startswith('input_number.') }}"
                is_num: "{{ val is number }}"
                vstr: "{{ (val|string) if not is_num else '' }}"
                is_toggle: "{{ (not is_num) and (vstr|lower).startswith('toggle') }}"
                toggle_pct: "{{ (vstr.split('_')|last)|int if ('_' in vstr) else 100 }}"
                currently_on: "{{ is_state(ent, 'on') }}"

            - choose:
                # 0a) Numbers: set numeric value
                - conditions: "{{ is_number_ent and is_num }}"
                  sequence:
                    - service: number.set_value
                      data:
                        entity_id: "{{ ent }}"
                        value: "{{ val|float }}"

                # 0b) Input numbers: set numeric value
                - conditions: "{{ is_input_number_ent and is_num }}"
                  sequence:
                    - service: input_number.set_value
                      data:
                        entity_id: "{{ ent }}"
                        value: "{{ val|float }}"

                # 1) Lights: toggle[_NN] -> if on turn off, else turn on to NN
                - conditions: "{{ is_light and is_toggle }}"
                  sequence:
                    - choose:
                        - conditions: "{{ currently_on }}"
                          sequence:
                            - service: light.turn_off
                              data:
                                entity_id: "{{ ent }}"
                      default:
                        - service: light.turn_on
                          data:
                            entity_id: "{{ ent }}"
                            brightness_pct: "{{ toggle_pct }}"

                # 2) Switches: toggle -> flip state
                - conditions: "{{ is_switch and is_toggle }}"
                  sequence:
                    - service: switch.toggle
                      data:
                        entity_id: "{{ ent }}"

                # 3) Lights: numeric brightness, where 0 = off
                - conditions: "{{ is_light and is_num and (val|int) > 0 }}"
                  sequence:
                    - service: light.turn_on
                      data:
                        entity_id: "{{ ent }}"
                        brightness_pct: "{{ val|int }}"

                - conditions: "{{ is_light and is_num and (val|int) == 0 }}"
                  sequence:
                    - service: light.turn_off
                      data:
                        entity_id: "{{ ent }}"

                # 4) Lights: textual/boolean on/off
                - conditions: "{{ is_light and not is_num and (val in [true, 'on', 'On', 'ON']) }}"
                  sequence:
                    - service: light.turn_on
                      data:
                        entity_id: "{{ ent }}"

                - conditions: "{{ is_light and not is_num and (val in [false, 'off', 'Off', 'OFF']) }}"
                  sequence:
                    - service: light.turn_off
                      data:
                        entity_id: "{{ ent }}"

                # 5) Switches: textual/boolean on/off
                - conditions: "{{ is_switch and (val in [true, 'on', 'On', 'ON']) }}"
                  sequence:
                    - service: switch.turn_on
                      data:
                        entity_id: "{{ ent }}"

                - conditions: "{{ is_switch and (val in [false, 'off', 'Off', 'OFF']) }}"
                  sequence:
                    - service: switch.turn_off
                      data:
                        entity_id: "{{ ent }}"

  lounge_apply_and_reset:
    alias: "Lounge: Apply scene + reset selector"
    mode: restart
    sequence:
      - service: script.lounge_apply_scene
      - delay: "00:00:05"
      - service: input_select.select_option
        target: { entity_id: input_select.lounge_scene }
        data:
          option: "Select Scene:"

###############################################################################
# AUTOMATIONS
###############################################################################
automation:
  - id: lounge_apply_on_selector_change
    alias: "Lounge - Apply when selector changes"
    trigger:
      - platform: state
        entity_id: input_select.lounge_scene
    condition:
      - condition: state
        entity_id: input_boolean.lounge_manual_override
        state: "off"
      - condition: template
        value_template: "{{ trigger.to_state.state != 'Select Scene:' }}"
    action:
      - service: script.turn_on
        target:
          entity_id: script.lounge_apply_and_reset

  - id: lounge_tv_on_sets_tv_bias
    alias: "Lounge - TV on sets TV General (unless override)"
    trigger:
      - platform: state
        entity_id: remote.lounge_tv
        to: "on"
        for: "00:00:02"
    condition:
      - condition: state
        entity_id: input_boolean.lounge_manual_override
        state: "off"
      - condition: template
        value_template: "{{ states('input_text.lounge_last_scene') != 'All Off' }}"
    action:
      - service: script.lounge_set_scene
        data:
          scene_name: "TV General"

  - id: lounge_tv_off_sets_bright
    alias: "Lounge - TV off sets Bright (unless override)"
    trigger:
      - platform: state
        entity_id: remote.lounge_tv
        to: "off"
        for: "00:00:02"
    condition:
      - condition: state
        entity_id: input_boolean.lounge_manual_override
        state: "off"
      - condition: template
        value_template: "{{ states('input_text.lounge_last_scene') != 'All Off' }}"
    action:
      - service: script.lounge_set_scene
        data:
          scene_name: "Bright"
