###############################################################################
# PACKAGE: OpenHASP Lounge 1 Wall Plate Backlight Control
# NODE/TOPIC ROOT: hasp/openhasp_1/
#
# INTENT:
# - Keep backlight control separate from the thermostat/heater UI package.
# - Avoid using brightness 0 as the main OFF method.
# - Wake the panel with "idle off" before applying brightness.
# - Quiet Time overrides everything and forces the backlight OFF.
#
# OPENHASP NOTES:
# - backlight brightness range is 1..255
# - backlight off is a state, not a brightness level
# - idle off clears idle/sleep state and behaves like a wake-up
###############################################################################

###############################################################################
# HELPERS
###############################################################################
input_boolean:
  openhasp_1_backlight_enable:
    name: OpenHASP 1 Backlight Enable
    icon: mdi:television-ambient-light
    initial: true

input_number:
  openhasp_1_backlight_level:
    name: OpenHASP 1 Backlight Level
    min: 1
    max: 255
    step: 1
    mode: slider
    initial: 255

###############################################################################
# AUTOMATIONS
###############################################################################
automation:
  # -------------------------------------------------------------------------
  # Quiet Time override
  # - Quiet Time ON  -> force backlight OFF
  # - Quiet Time OFF -> re-apply the requested backlight state
  # -------------------------------------------------------------------------
  - id: openhasp_1_backlight_quiet_time_changed
    alias: "OpenHASP 1: backlight quiet time changed"
    mode: restart
    trigger:
      - platform: state
        entity_id: input_boolean.quiet_time
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.quiet_time
                state: "on"
            sequence:
              - service: script.openhasp_1_backlight_off

        default:
          - service: script.openhasp_1_backlight_apply_requested_state

  # -------------------------------------------------------------------------
  # Backlight enable helper changed
  # - ON  -> wake panel and apply current brightness level, unless Quiet Time
  # - OFF -> tell openHASP to switch the backlight off
  # -------------------------------------------------------------------------
  - id: openhasp_1_backlight_enable_changed
    alias: "OpenHASP 1: backlight enable changed"
    mode: restart
    trigger:
      - platform: state
        entity_id: input_boolean.openhasp_1_backlight_enable
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.openhasp_1_backlight_enable
                state: "on"
              - condition: state
                entity_id: input_boolean.quiet_time
                state: "off"
            sequence:
              - service: script.openhasp_1_backlight_apply

        default:
          - service: script.openhasp_1_backlight_off

  # -------------------------------------------------------------------------
  # Backlight level helper changed
  # - If backlight is enabled and Quiet Time is OFF, apply the new level
  # - If disabled or Quiet Time is ON, just store the new level for later
  # -------------------------------------------------------------------------
  - id: openhasp_1_backlight_level_changed
    alias: "OpenHASP 1: backlight level changed"
    mode: restart
    trigger:
      - platform: state
        entity_id: input_number.openhasp_1_backlight_level
    condition:
      - condition: state
        entity_id: input_boolean.openhasp_1_backlight_enable
        state: "on"
      - condition: state
        entity_id: input_boolean.quiet_time
        state: "off"
    action:
      - service: script.openhasp_1_backlight_apply

  # -------------------------------------------------------------------------
  # Re-apply requested backlight state when Home Assistant starts
  # Quiet Time is checked inside the script.
  # -------------------------------------------------------------------------
  - id: openhasp_1_backlight_on_ha_start
    alias: "OpenHASP 1: apply backlight on HA start"
    mode: queued
    trigger:
      - platform: homeassistant
        event: start
    action:
      - delay: "00:00:10"
      - service: script.openhasp_1_backlight_apply_requested_state

  # -------------------------------------------------------------------------
  # Re-apply requested backlight state when the panel reconnects
  # Quiet Time is checked inside the script.
  # -------------------------------------------------------------------------
  - id: openhasp_1_backlight_on_panel_online
    alias: "OpenHASP 1: apply backlight on panel online"
    mode: queued
    trigger:
      - platform: mqtt
        topic: "hasp/openhasp_1/LWT"
    condition:
      - condition: template
        value_template: "{{ trigger.payload == 'online' }}"
    action:
      - delay: "00:00:05"
      - service: script.openhasp_1_backlight_apply_requested_state

  # -------------------------------------------------------------------------
  # If openHASP enters idle while HA says the backlight should be enabled,
  # wake it and re-apply the requested brightness.
  #
  # Quiet Time blocks this recovery, so the panel stays dark overnight.
  # -------------------------------------------------------------------------
  - id: openhasp_1_backlight_recover_from_idle
    alias: "OpenHASP 1: recover backlight from idle"
    mode: restart
    trigger:
      - platform: mqtt
        topic: "hasp/openhasp_1/state/idle"
    condition:
      - condition: state
        entity_id: input_boolean.openhasp_1_backlight_enable
        state: "on"
      - condition: state
        entity_id: input_boolean.quiet_time
        state: "off"
      - condition: template
        value_template: "{{ trigger.payload | lower != 'off' }}"
    action:
      - delay: "00:00:01"
      - service: script.openhasp_1_backlight_apply

###############################################################################
# SCRIPTS
###############################################################################
script:
  # -------------------------------------------------------------------------
  # Apply current requested brightness.
  #
  # Important:
  # - Send idle off first so the panel is awake.
  # - Then send a JSON backlight command with state ON and brightness 1..255.
  # -------------------------------------------------------------------------
  openhasp_1_backlight_apply:
    alias: "OpenHASP 1: apply backlight level"
    mode: restart
    sequence:
      - service: mqtt.publish
        data:
          topic: "hasp/openhasp_1/command"
          payload: "idle off"

      - delay: "00:00:00.25"

      - service: mqtt.publish
        data:
          topic: "hasp/openhasp_1/command"
          payload: >
            backlight {"state":"on","brightness":{{ states('input_number.openhasp_1_backlight_level') | int(255) }}}

      # Send once more shortly after. This helps with panels that miss the
      # brightness change while coming out of idle.
      - delay: "00:00:00.75"

      - service: mqtt.publish
        data:
          topic: "hasp/openhasp_1/command"
          payload: >
            backlight {"state":"on","brightness":{{ states('input_number.openhasp_1_backlight_level') | int(255) }}}

  # -------------------------------------------------------------------------
  # Turn the backlight off using the openHASP backlight OFF state.
  # Do not set brightness to 0.
  # -------------------------------------------------------------------------
  openhasp_1_backlight_off:
    alias: "OpenHASP 1: backlight off"
    mode: restart
    sequence:
      - service: mqtt.publish
        data:
          topic: "hasp/openhasp_1/command"
          payload: "backlight off"

  # -------------------------------------------------------------------------
  # Apply either ON/current-level or OFF according to helper state.
  # Used on HA startup, panel reconnect, and Quiet Time changes.
  #
  # Priority:
  # 1. Quiet Time ON -> backlight OFF
  # 2. Backlight Enable ON -> apply current level
  # 3. Backlight Enable OFF -> backlight OFF
  # -------------------------------------------------------------------------
  openhasp_1_backlight_apply_requested_state:
    alias: "OpenHASP 1: apply requested backlight state"
    mode: restart
    sequence:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.quiet_time
                state: "on"
            sequence:
              - service: script.openhasp_1_backlight_off

          - conditions:
              - condition: state
                entity_id: input_boolean.openhasp_1_backlight_enable
                state: "on"
            sequence:
              - service: script.openhasp_1_backlight_apply

        default:
          - service: script.openhasp_1_backlight_off
