##########################################################################################
# Master Bedroom Heat Pump Schedules
##########################################################################################
#
# TITLE:
#   Master Bedroom Heat Pump Schedules
#
# FILE:
#   packages/heat_pump_master_bedroom_schedules.yaml
#
# VERSION:
#   V1.1 2026-07-20
#
# VERSION HISTORY:
#   V1.1 2026-07-20
#     - Added structured documentation for schedules, boost, and helper behavior.
#
#   V1.0
#     - Initial master bedroom heat pump schedules and boost controls.
#
# PURPOSE:
#   Provides optional 21:00 start and midnight stop schedules, a combined Night
#   Power mode, and a manual one-hour boost for the master bedroom heat pump.
#
# DEPENDENCIES:
#   - climate.master_bedroom provides the Panasonic Comfort Cloud heat pump.
#   - The climate entity must support the "boost" and "none" preset modes.
#   - packages/heat_pump_master_bedroom_helpers.yaml provides companion
#     dashboard, setpoint, HVAC mode, profile, and sleep-timer controls.
#
# SCHEDULE MODES:
#   - Night Power Mode turns the heat pump on at 21:00, applies boost for 30
#     minutes, returns it to the normal preset, and turns it off at midnight.
#   - 9pm On turns the heat pump on at 21:00 without changing its preset.
#   - Midnight Off turns the heat pump off at midnight.
#   - Enabling Night Power disables the separate 9pm On and Midnight Off modes.
#   - Enabling either separate mode disables Night Power. The separate 9pm On
#     and Midnight Off modes may be enabled together.
#
# MANUAL BOOST:
#   - script.master_bedroom_boost_1hr applies boost for one hour.
#   - If the heat pump was off when boost started, it is turned off afterward.
#   - If it was already running, boost ends by restoring the normal preset and
#     leaving the heat pump running.
#
# INTERNAL STATE:
#   - input_boolean.master_bedroom_boost_restore_off records whether manual
#     boost should restore the heat pump to off.
#   - timer.master_bedroom_boost_timer controls manual and scheduled boost time.
#
# OPERATION NOTES:
#   - The 21:00 and midnight schedules use exact-time triggers.
#   - There is no startup catch-up if Home Assistant is offline at either time.
#
##########################################################################################

##########################################################################################
# HELPERS
##########################################################################################

input_boolean:
  master_bedroom_night_power_mode:
    name: Night Power Mode
    icon: mdi:weather-night

  master_bedroom_9pm_on:
    name: 9pm On
    icon: mdi:clock-start

  master_bedroom_midnight_off:
    name: Midnight Off
    icon: mdi:clock-end

  master_bedroom_boost_restore_off:
    name: Master Bedroom Boost Restore Off
    icon: mdi:power

##########################################################################################
# TIMERS
##########################################################################################

timer:
  master_bedroom_boost_timer:
    name: Master Bedroom Boost Timer
    duration: "00:00:00"

##########################################################################################
# SCRIPTS
##########################################################################################

script:
  master_bedroom_boost_1hr:
    alias: Master Bedroom Boost 1hr
    mode: restart
    sequence:
      # Remember if the heat pump was OFF when boost started
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.master_bedroom_boost_restore_off

      - choose:
          - conditions:
              - condition: state
                entity_id: climate.master_bedroom
                state: "off"
            sequence:
              - service: input_boolean.turn_on
                target:
                  entity_id: input_boolean.master_bedroom_boost_restore_off

      # Start boost (this will turn it on if it was off)
      - service: climate.turn_on
        target:
          entity_id: climate.master_bedroom

      - service: climate.set_preset_mode
        target:
          entity_id: climate.master_bedroom
        data:
          preset_mode: "boost"

      - service: timer.start
        target:
          entity_id: timer.master_bedroom_boost_timer
        data:
          duration: "01:00:00"

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

automation:
  ##########################################################################################
  # CANCEL LOGIC
  ##########################################################################################

  - alias: MB Cancel Others When Night Power Enabled
    trigger:
      - platform: state
        entity_id: input_boolean.master_bedroom_night_power_mode
        to: "on"
    action:
      - service: input_boolean.turn_off
        target:
          entity_id:
            - input_boolean.master_bedroom_9pm_on
            - input_boolean.master_bedroom_midnight_off

  - alias: MB Cancel Night Power If 9pm On Enabled
    trigger:
      - platform: state
        entity_id: input_boolean.master_bedroom_9pm_on
        to: "on"
    action:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.master_bedroom_night_power_mode

  - alias: MB Cancel Night Power If Midnight Off Enabled
    trigger:
      - platform: state
        entity_id: input_boolean.master_bedroom_midnight_off
        to: "on"
    action:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.master_bedroom_night_power_mode

  ##########################################################################################
  # 9PM LOGIC
  ##########################################################################################

  - alias: MB 9PM Start Logic
    trigger:
      - platform: time
        at: "21:00:00"
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.master_bedroom_night_power_mode
                state: "on"
            sequence:
              - service: climate.turn_on
                target:
                  entity_id: climate.master_bedroom

              - service: climate.set_preset_mode
                target:
                  entity_id: climate.master_bedroom
                data:
                  preset_mode: "boost"

              - service: timer.start
                target:
                  entity_id: timer.master_bedroom_boost_timer
                data:
                  duration: "00:30:00"

          - conditions:
              - condition: state
                entity_id: input_boolean.master_bedroom_9pm_on
                state: "on"
            sequence:
              - service: climate.turn_on
                target:
                  entity_id: climate.master_bedroom

  ##########################################################################################
  # MIDNIGHT LOGIC
  ##########################################################################################

  - alias: MB Midnight Off Logic
    trigger:
      - platform: time
        at: "00:00:00"
    action:
      - choose:
          - conditions:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: input_boolean.master_bedroom_midnight_off
                    state: "on"
                  - condition: state
                    entity_id: input_boolean.master_bedroom_night_power_mode
                    state: "on"
            sequence:
              - service: climate.turn_off
                target:
                  entity_id: climate.master_bedroom

  ##########################################################################################
  # BOOST TIMER FINISHED
  ##########################################################################################

  - alias: MB Boost Timer Finished
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.master_bedroom_boost_timer
    action:
      - choose:
          # If boost started while the heat pump was OFF, ensure it ends OFF
          - conditions:
              - condition: state
                entity_id: input_boolean.master_bedroom_boost_restore_off
                state: "on"
            sequence:
              - service: climate.turn_off
                target:
                  entity_id: climate.master_bedroom

          # Otherwise, revert to Normal (preset_mode none) and keep running
          - conditions:
              - condition: state
                entity_id: input_boolean.master_bedroom_boost_restore_off
                state: "off"
            sequence:
              - service: climate.set_preset_mode
                target:
                  entity_id: climate.master_bedroom
                data:
                  preset_mode: "none"

      # Always clear the helper
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.master_bedroom_boost_restore_off
