################################################################################
# Quiet Time On / Off Actions Package
################################################################################
#
# TITLE:
#   Quiet Time On / Off Actions
#
# FILE:
#   quiet_time_on_off_actions.yaml
#
# VERSION:
#   V1.2 2026-06-20
#
# VERSION HISTORY:
#   V1.2 2026-06-20
#     - Added Quiet Time OFF action to change OpenHASP panel openhasp_1
#       back to page 1.
#
#   V1.1 2026-06-15
#     - Added separate Quiet Time ON and Quiet Time OFF action sections.
#     - Added support for non-binary Quiet Time OFF actions.
#     - Sets input_number.openhasp_1_backlight_level to 200 when Quiet Time ends.
#
#   V1.0 2026-06-15
#     - Initial standalone package split out from the main Quiet Time package.
#     - Turns selected entities on/off when input_boolean.quiet_time changes.
#
# PURPOSE:
#   Runs local Home Assistant actions when Quiet Time is turned ON or OFF.
#
#   The main Quiet Time package should continue to own:
#     - input_boolean.quiet_time
#     - daily reset behaviour
#     - MQTT quiet_time state publishing
#
#   This package only handles extra local actions that should happen when
#   Quiet Time starts or ends.
#
# EDITING NOTES:
#   - Add normal on/off entities to the relevant entity lists.
#   - Empty lists are fine and will simply do nothing.
#   - Use the "Other Quiet Time OFF Actions" section for things that are not
#     simple on/off actions, such as setting input_numbers, selects, scenes,
#     scripts, MQTT commands, or custom service calls.
#
################################################################################

################################################################################
# AUTOMATIONS
################################################################################

automation:
  - id: quiet_time_state_change_actions
    alias: "Quiet Time - State Change Actions"
    description: "Runs configured Home Assistant actions when Quiet Time is turned on or off."
    mode: single

    variables:
      ##########################################################################
      # QUIET TIME ON ACTION LISTS
      ##########################################################################
      #
      # These actions run when input_boolean.quiet_time changes to "on".
      #

      quiet_time_on_turn_off_entities:
        - light.led_matrix_display_led_matrix_display_display

      quiet_time_on_turn_on_entities: []

      ##########################################################################
      # QUIET TIME OFF ACTION LISTS
      ##########################################################################
      #
      # These actions run when input_boolean.quiet_time changes to "off".
      #

      quiet_time_off_turn_off_entities: []

      quiet_time_off_turn_on_entities:
        - light.led_matrix_display_led_matrix_display_display

      ##########################################################################
      # OTHER QUIET TIME OFF ACTION VALUES
      ##########################################################################
      #
      # Use this list for input_number helpers that should be set when Quiet Time
      # ends.
      #
      # Format:
      #   - entity_id: input_number.some_helper
      #     value: 123
      #

      quiet_time_off_input_number_values:
        - entity_id: input_number.openhasp_1_backlight_level
          value: 200

      ##########################################################################
      # OPENHASP QUIET TIME OFF PAGE ACTION
      ##########################################################################
      #
      # Sends the OpenHASP panel back to the main page when Quiet Time ends.
      #
      # Current OpenHASP MQTT command topic:
      #   hasp/openhasp_1/command/page
      #

      quiet_time_off_openhasp_page_topic: "hasp/openhasp_1/command/page"
      quiet_time_off_openhasp_page: 1

    triggers:
      - trigger: state
        entity_id: input_boolean.quiet_time
        from:
          - "on"
          - "off"
        to:
          - "on"
          - "off"

    actions:
      - choose:
          ######################################################################
          # QUIET TIME ON ACTIONS
          ######################################################################
          #
          # Runs when Quiet Time starts.
          #

          - conditions:
              - condition: template
                value_template: "{{ trigger.to_state.state == 'on' }}"
            sequence:
              - repeat:
                  for_each: "{{ quiet_time_on_turn_off_entities }}"
                  sequence:
                    - action: homeassistant.turn_off
                      target:
                        entity_id: "{{ repeat.item }}"

              - repeat:
                  for_each: "{{ quiet_time_on_turn_on_entities }}"
                  sequence:
                    - action: homeassistant.turn_on
                      target:
                        entity_id: "{{ repeat.item }}"

          ######################################################################
          # QUIET TIME OFF ACTIONS
          ######################################################################
          #
          # Runs when Quiet Time ends.
          #

          - conditions:
              - condition: template
                value_template: "{{ trigger.to_state.state == 'off' }}"
            sequence:
              ##################################################################
              # Quiet Time OFF - binary/entity actions
              ##################################################################

              - repeat:
                  for_each: "{{ quiet_time_off_turn_off_entities }}"
                  sequence:
                    - action: homeassistant.turn_off
                      target:
                        entity_id: "{{ repeat.item }}"

              - repeat:
                  for_each: "{{ quiet_time_off_turn_on_entities }}"
                  sequence:
                    - action: homeassistant.turn_on
                      target:
                        entity_id: "{{ repeat.item }}"

              ##################################################################
              # Quiet Time OFF - input_number set actions
              ##################################################################

              - repeat:
                  for_each: "{{ quiet_time_off_input_number_values }}"
                  sequence:
                    - action: input_number.set_value
                      target:
                        entity_id: "{{ repeat.item.entity_id }}"
                      data:
                        value: "{{ repeat.item.value | float }}"

              ##################################################################
              # Quiet Time OFF - OpenHASP page action
              ##################################################################
              #
              # Change OpenHASP panel back to page 1 when Quiet Time ends.
              #

              - action: mqtt.publish
                data:
                  topic: "{{ quiet_time_off_openhasp_page_topic }}"
                  payload: "{{ quiet_time_off_openhasp_page | int }}"
                  retain: false

              ##################################################################
              # Quiet Time OFF - other custom actions
              ##################################################################
              #
              # Add any extra non-binary service calls here later.
              #
              # Examples:
              #
              # - action: script.some_script
              #
              # - action: select.select_option
              #   target:
              #     entity_id: select.some_select
              #   data:
              #     option: "Some Option"
              #
              ##################################################################
