#:########################################################################################:#
# PACKAGE: Zorro Evening Feeding Reminder (P017-S013)
# FILE: packages/p017_s013_zorro_feeding_reminder.yaml
# VERSION: v1.0.1 2026-07-26
#:########################################################################################:#
# PURPOSE:
#   Remind to feed Zorro (evening feeding) if not completed.
#   LED matrix shows "Feed Zorro" every 30 min from 5pm to 10pm.
#   Voice says "Zorro has not been fed this afternoon" every 30 min from 5pm to 8pm.
#   Each announcement has a random delay of 0-10 min for natural variation.
#   Clears LED matrix immediately when evening feeding is completed.
#:########################################################################################:#
# DEPENDENCIES:
#   - packages/view_road_announcement_router.yaml for LED/voice announcements
#   - packages/Job_Chore_Tracking/jobs_tracker_jobs.yaml for evening feeding sensor
#   - ESPHome LED matrix for cancel_id MQTT topic
#:########################################################################################:#

input_boolean:
  zorro_feeding_reminder_enabled:
    name: Zorro Feeding Reminder
    icon: mdi:dog
    initial: true

automation:
  ##############################################################################
  # EVENING FEEDING REMINDER
  ##############################################################################

  - id: zorro_evening_feeding_reminder
    alias: "Zorro - Evening Feeding Reminder"
    mode: single

    trigger:
      - trigger: time_pattern
        minutes: "/30"
      - trigger: homeassistant
        event: start

    condition:
      - condition: state
        entity_id: input_boolean.zorro_feeding_reminder_enabled
        state: "on"

      - condition: template
        value_template: >
          {{ now().hour >= 17 and now().hour < 22 }}

      - condition: template
        value_template: >
          {{ states('sensor.jobs_tracker_dog_feeding_evening') != 'Complete' }}

    action:
      - variables:
          voice_hours: "{{ now().hour < 20 }}"
          quiet_time: "{{ is_state('input_boolean.quiet_time', 'on') }}"

      - delay:
          seconds: "{{ range(0, 601) | random | int }}"

      - action: script.view_road_announcement
        data:
          channels: "led_matrix_1, sony_tv_lounge"
          title: "Zorro"
          short_announcement: "Feed Zorro"
          indicator: "zorro,1,15,10,6,5,0,0"

      - if:
          - condition: template
            value_template: "{{ voice_hours and not quiet_time }}"
        then:
          - action: script.view_road_announcement
            data:
              channels: "lounge_google_home_voice"
              title: "Zorro"
              short_announcement: "Zorro has not been fed this afternoon"

  ##############################################################################
  # CLEAR LED MATRIX WHEN FED
  ##############################################################################

  - id: zorro_clear_led_when_fed
    alias: "Zorro - Clear LED Matrix When Fed"
    mode: single

    trigger:
      - trigger: state
        entity_id: sensor.jobs_tracker_dog_feeding_evening
        to: "Complete"

    action:
      - action: mqtt.publish
        data:
          topic: "viewroad-commands/ledmatrix1/cancel_id"
          payload: "zorro"
          qos: 0
          retain: false
