################################################################################
# Quiet Time Package
################################################################################
#
# TITLE:
#   Quiet Time
#
# FILE:
#   quiet_time.yaml
#
# VERSION:
#   V1.4 2026-07-20
#
# VERSION HISTORY:
#   V1.4 2026-07-20
#     - Updated documentation for the standalone Quiet Time actions package.
#     - Documented the current flattened MQTT status topic.
#
#   V1.3 2026-06-07
#     - Added quiet time start and stop entity action lists.
#     - Turns the LED matrix display OFF when quiet time starts.
#     - Turns the LED matrix display ON when quiet time ends.
#
#   V1.2 2026-05-21
#     - Added MQTT publishing when input_boolean.quiet_time changes.
#     - Published quiet_time state to viewroad-status/homeassistant/quiet-time.
#       This was the historical topic; the current topic is documented below.
#
#   V1.1 2026-05-21
#     - Reduced daily reset check frequency from every minute to every 30 minutes.
#     - Reduced reset dropdown options to 00:30 through 10:00 only.
#
#   V1.0 2026-05-21
#     - Initial package.
#     - Adds input_boolean.quiet_time.
#     - Adds input_select.quiet_time_daily_reset.
#     - Adds daily automation to turn quiet_time OFF at the selected reset time.
#
# OPERATION NOTES:
#   - input_boolean.quiet_time is the master quiet time toggle.
#   - Other packages can check input_boolean.quiet_time before sending
#     announcements, notifications, TTS messages, or similar.
#   - input_select.quiet_time_daily_reset selects the daily time that quiet_time
#     will automatically be turned OFF.
#   - Default reset time is 07:00.
#   - The quiet_time state is published to MQTT whenever it changes.
#   - The companion package handles local actions when quiet_time changes.
#
# COMPANION PACKAGE:
#   - quiet_time_on_off_actions.yaml owns the editable entity action lists and
#     other local actions that run when Quiet Time starts or ends.
#   - This package owns input_boolean.quiet_time, the daily reset behaviour,
#     and MQTT status publishing.
#
# MQTT STATUS:
#   - Topic:
#       viewroad-status/quiet-time
#   - Payload:
#       on
#       off
#
################################################################################

################################################################################
# INPUT BOOLEAN
################################################################################

input_boolean:
  quiet_time:
    name: Quiet Time
    icon: mdi:bell-off

################################################################################
# INPUT SELECT
################################################################################

input_select:
  quiet_time_daily_reset:
    name: Quiet time daily reset
    icon: mdi:clock-end
    options:
      - "00:30"
      - "01:00"
      - "01:30"
      - "02:00"
      - "02:30"
      - "03:00"
      - "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"
    initial: "07:00"

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

automation:
  - id: quiet_time_daily_reset_turn_off
    alias: "Quiet Time - Daily Reset Off"
    description: "Turns Quiet Time off once per day at the selected reset time."
    mode: single

    triggers:
      - trigger: time_pattern
        minutes: "/30"
        seconds: "0"

    conditions:
      - condition: state
        entity_id: input_boolean.quiet_time
        state: "on"

      - condition: template
        value_template: >
          {{ states('input_select.quiet_time_daily_reset') == now().strftime('%H:%M') }}

    actions:
      - action: input_boolean.turn_off
        target:
          entity_id: input_boolean.quiet_time

  - id: quiet_time_mqtt
    alias: "Quiet Time - State to MQTT"
    description: "Publishes the Quiet Time state to MQTT"
    mode: single

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

    actions:
      - action: mqtt.publish
        data:
          topic: viewroad-status/quiet-time
          payload: "{{ trigger.to_state.state }}"
