#:########################################################################################:#
# Pool Timer Selection Package                                                            #
#:########################################################################################:#
#
# TITLE:
#   Pool Timer Selection
#
# FILE:
#   packages/Pool_Controls/pool_timers_selections.yaml
#
# VERSION:
#   V1.1 2026-07-20
#
# VERSION HISTORY:
#   V1.1 2026-07-20
#     - Corrected the documented package filename.
#     - Documented command selectors versus ESPHome timer feedback.
#
#   V1.0 2026-06-29
#     - Initial package.
#     - Adds Home Assistant input_select helpers for Pool Pump and Pool Light timer times.
#     - Publishes selected times to the ESPHome MQTT timer command topics.
#     - "Default" publishes the matching ESPHome default time for that selector.
#
# PURPOSE:
#   Provides Home Assistant dropdown selectors for the Pool Pump and Pool Light
#   morning/evening ON/OFF timer settings.
#
# MQTT NOTES:
#   ESPHome devices subscribe to:
#     viewroad-commands/poolpump-timer/morning-on
#     viewroad-commands/poolpump-timer/morning-off
#     viewroad-commands/poolpump-timer/evening-on
#     viewroad-commands/poolpump-timer/evening-off
#
#     viewroad-commands/poollight-timer/morning-on
#     viewroad-commands/poollight-timer/morning-off
#     viewroad-commands/poollight-timer/evening-on
#     viewroad-commands/poollight-timer/evening-off
#
#   Payload format is HH:MM.
#
# STATUS AND FEEDBACK:
#   - These input_select helpers send retained MQTT commands to ESPHome.
#   - They do not synchronize their displayed options from the ESPHome devices.
#   - The ESPHome Timeclock sensors are authoritative for the currently applied
#     timer values.
#   - A selector displaying "Default" does not prove that the device currently
#     uses its default time.
#   - Native ESPHome mode controls coexist with this package, while timer
#     schedule changes continue to use the MQTT topics above.
#
# IMPORTANT:
#   The current ESPHome timer logic only handles same-day timer windows:
#     current_mins >= on_time AND current_mins < off_time
#
#   Because of this, after-midnight evening selections such as 00:30 or 01:00
#   are intentionally not included here.
#
#   24:00 is included for OFF selectors as "run until midnight".
#
#:########################################################################################:#

input_select:
  #:######################################################################################:#
  # Pool Pump Timer Selectors                                                             #
  #:######################################################################################:#

  pool_pump_morning_on_time:
    name: "Pool Pump Morning On Time"
    icon: "mdi:timer-play-outline"
    options: &pool_timer_options_0730
      - "Default"
      - "03:30"
      - "04:00"
      - "04:30"
      - "05:00"
      - "05:30"
      - "06:00"
      - "06:30"
      - "07:00"
      - "07:30"
      - "08:00"
      - "08:30"
      - "09:00"
      - "09:30"
      - "10:00"
      - "10:30"
      - "11:00"
      - "11:30"

  pool_pump_morning_off_time:
    name: "Pool Pump Morning Off Time"
    icon: "mdi:timer-stop-outline"
    options: *pool_timer_options_0730

  pool_pump_evening_on_time:
    name: "Pool Pump Evening On Time"
    icon: "mdi:timer-play-outline"
    options: &pool_timer_options_2100_same_day
      - "Default"
      - "17:00"
      - "17:30"
      - "18:00"
      - "18:30"
      - "19:00"
      - "19:30"
      - "20:00"
      - "20:30"
      - "21:00"
      - "21:30"
      - "22:00"
      - "22:30"
      - "23:00"
      - "23:30"

  pool_pump_evening_off_time:
    name: "Pool Pump Evening Off Time"
    icon: "mdi:timer-stop-outline"
    options: &pool_timer_options_2230_same_day
      - "Default"
      - "18:30"
      - "19:00"
      - "19:30"
      - "20:00"
      - "20:30"
      - "21:00"
      - "21:30"
      - "22:00"
      - "22:30"
      - "23:00"
      - "23:30"
      - "24:00"

  #:######################################################################################:#
  # Pool Light Timer Selectors                                                            #
  #:######################################################################################:#

  pool_light_morning_on_time:
    name: "Pool Light Morning On Time"
    icon: "mdi:timer-play-outline"
    options: *pool_timer_options_0730

  pool_light_morning_off_time:
    name: "Pool Light Morning Off Time"
    icon: "mdi:timer-stop-outline"
    options: *pool_timer_options_0730

  pool_light_evening_on_time:
    name: "Pool Light Evening On Time"
    icon: "mdi:timer-play-outline"
    options: &pool_timer_options_1900
      - "Default"
      - "15:00"
      - "15:30"
      - "16:00"
      - "16:30"
      - "17:00"
      - "17:30"
      - "18:00"
      - "18:30"
      - "19:00"
      - "19:30"
      - "20:00"
      - "20:30"
      - "21:00"
      - "21:30"
      - "22:00"
      - "22:30"
      - "23:00"

  pool_light_evening_off_time:
    name: "Pool Light Evening Off Time"
    icon: "mdi:timer-stop-outline"
    options: *pool_timer_options_2230_same_day

script:
  #:######################################################################################:#
  # MQTT Publish Script                                                                    #
  #:######################################################################################:#

  pool_timer_publish_mqtt_time:
    alias: "Pool Timer - Publish MQTT Time"
    description: "Publishes one selected pool timer HH:MM value to MQTT."
    mode: queued
    max: 20

    fields:
      mqtt_timer_topic:
        name: "MQTT timer topic"
        example: "viewroad-commands/poolpump-timer"
      timer_command:
        name: "Timer command"
        example: "morning-on"
      selected_value:
        name: "Selected value"
        example: "Default"
      default_time:
        name: "Default time"
        example: "07:30"

    variables:
      #:##################################################################################:#
      # Editable MQTT Publish Settings                                                     #
      #:##################################################################################:#
      default_label: "Default"
      mqtt_retain_timer_commands: true
      mqtt_qos: 0

      payload_to_send: >-
        {% set selected = selected_value | string %}
        {% if selected == default_label %}
          {{ default_time }}
        {% else %}
          {{ selected }}
        {% endif %}

    sequence:
      - condition: template
        value_template: "{{ (selected_value | string) not in ['unknown', 'unavailable', 'none', 'None', ''] }}"

      - action: mqtt.publish
        data:
          topic: "{{ mqtt_timer_topic }}/{{ timer_command }}"
          payload: "{{ payload_to_send }}"
          qos: "{{ mqtt_qos | int }}"
          retain: "{{ mqtt_retain_timer_commands | bool }}"

automation:
  #:######################################################################################:#
  # Publish Timer Selector Changes                                                        #
  #:######################################################################################:#

  - id: pool_timer_selection_publish_mqtt
    alias: "Pool Timer Selection - Publish MQTT"
    description: "Publishes pool timer selector changes to the matching ESPHome MQTT topic."
    mode: queued
    max: 20

    variables:
      #:##################################################################################:#
      # Editable MQTT Topic Settings                                                       #
      #:##################################################################################:#
      # No leading slash, to match the ESPHome mqtt_timer_topic substitutions.
      mqtt_command_root: "viewroad-commands"
      pool_pump_timer_topic_suffix: "poolpump-timer"
      pool_light_timer_topic_suffix: "poollight-timer"

      #:##################################################################################:#
      # Editable ESPHome Default Timer Values                                              #
      #:##################################################################################:#
      # Pool Pump ESPHome defaults
      pool_pump_morning_on_default: "07:30"
      pool_pump_morning_off_default: "07:30"
      pool_pump_evening_on_default: "21:00"
      pool_pump_evening_off_default: "22:30"

      # Pool Light ESPHome defaults
      pool_light_morning_on_default: "07:30"
      pool_light_morning_off_default: "07:30"
      pool_light_evening_on_default: "19:00"
      pool_light_evening_off_default: "22:30"

    triggers:
      - trigger: state
        entity_id: input_select.pool_pump_morning_on_time
        id: pool_pump_morning_on

      - trigger: state
        entity_id: input_select.pool_pump_morning_off_time
        id: pool_pump_morning_off

      - trigger: state
        entity_id: input_select.pool_pump_evening_on_time
        id: pool_pump_evening_on

      - trigger: state
        entity_id: input_select.pool_pump_evening_off_time
        id: pool_pump_evening_off

      - trigger: state
        entity_id: input_select.pool_light_morning_on_time
        id: pool_light_morning_on

      - trigger: state
        entity_id: input_select.pool_light_morning_off_time
        id: pool_light_morning_off

      - trigger: state
        entity_id: input_select.pool_light_evening_on_time
        id: pool_light_evening_on

      - trigger: state
        entity_id: input_select.pool_light_evening_off_time
        id: pool_light_evening_off

    conditions:
      - condition: template
        value_template: "{{ trigger.from_state is not none and trigger.to_state is not none }}"

      - condition: template
        value_template: "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"

      - condition: template
        value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"

    actions:
      - choose:
          #:################################################################################:#
          # Pool Pump MQTT Publishes                                                         #
          #:################################################################################:#

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_pump_morning_on' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_pump_timer_topic_suffix }}"
                  timer_command: "morning-on"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_pump_morning_on_default }}"

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_pump_morning_off' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_pump_timer_topic_suffix }}"
                  timer_command: "morning-off"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_pump_morning_off_default }}"

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_pump_evening_on' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_pump_timer_topic_suffix }}"
                  timer_command: "evening-on"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_pump_evening_on_default }}"

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_pump_evening_off' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_pump_timer_topic_suffix }}"
                  timer_command: "evening-off"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_pump_evening_off_default }}"

          #:################################################################################:#
          # Pool Light MQTT Publishes                                                        #
          #:################################################################################:#

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_light_morning_on' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_light_timer_topic_suffix }}"
                  timer_command: "morning-on"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_light_morning_on_default }}"

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_light_morning_off' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_light_timer_topic_suffix }}"
                  timer_command: "morning-off"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_light_morning_off_default }}"

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_light_evening_on' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_light_timer_topic_suffix }}"
                  timer_command: "evening-on"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_light_evening_on_default }}"

          - conditions:
              - condition: template
                value_template: "{{ trigger.id == 'pool_light_evening_off' }}"
            sequence:
              - action: script.pool_timer_publish_mqtt_time
                data:
                  mqtt_timer_topic: "{{ mqtt_command_root }}/{{ pool_light_timer_topic_suffix }}"
                  timer_command: "evening-off"
                  selected_value: "{{ trigger.to_state.state }}"
                  default_time: "{{ pool_light_evening_off_default }}"
