###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   03/05/2020
#   @package        :   Pill Reminder Package
#   @description    :   A package to help you remind pills you need to take
###############################################################################

###############################################################################
#   _____ _ _ _   _____                _           _
#  |  __ (_) | | |  __ \              (_)         | |
#  | |__) || | | | |__) |___ _ __ ___  _ _ __   __| | ___ _ __
#  |  ___/ | | | |  _  // _ \ '_ ` _ \| | '_ \ / _` |/ _ \ '__|
#  | |   | | | | | | \ \  __/ | | | | | | | | | (_| |  __/ |
#  |_|   |_|_|_| |_|  \_\___|_| |_| |_|_|_| |_|\__,_|\___|_|
#
###############################################################################

homeassistant:

input_label:
  pill_taken_at:
    name: Last Pills Taken At
    icon: mdi:pill

input_boolean:
  pill_taken:
    name: Pill Taken
    icon: mdi:pill

  pill_voice_notification:
    name: Pill Voice Notification
    icon: mdi:speaker

timer:
  timer_pill_reminder:
    duration: "00:30:00"

input_datetime:
  pill_reminder_time:
    name: Pill Reminder Time
    has_date: false
    has_time: true
    initial: "09:00"

###############################################################################
#                 _                        _   _
#      /\        | |                      | | (_)
#     /  \  _   _| |_ ___  _ __ ___   __ _| |_ _  ___  _ __  ___
#    / /\ \| | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \/ __|
#   / ____ \ |_| | || (_) | | | | | | (_| | |_| | (_) | | | \__ \
#  /_/    \_\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_|___/
###############################################################################
automation:
  ###############################################################################
  # Turn On "Pill Taken" Input Boolean when the Pill Box is Opened
  # Opening the Pills Box signifies that the pills are taken!
  # Make sure the pill box is not accessible to children and only you use it daily
  ###############################################################################
  - alias: Pill Box is Opened
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d00040ad8ec
      to: "on"
      from: "off"
    action:
      - service: input_label.set_value
        data_template:
          entity_id: input_label.pill_taken_at
          value: "{{ as_timestamp(now()) | timestamp_custom('%m/%d/%Y %I:%M %p') }}"
      - delay:
          seconds: 15
      - service: script.pill_taken

  ###############################################################################
  # Reset at mid night every day.
  # When you get up in the morning, you will get a fresh reminder
  ###############################################################################
  - alias: Reset Pill Taken
    initial_state: true
    trigger:
      platform: time
      at: "00:00:00"
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.pill_taken

  ###############################################################################
  # Checks if the pills were taken - trigger on the pill reminder time
  # Also, only remind if not on vacation or away from home
  # Right after the notification, start the timer, and keep reminders going
  # until acknowledged
  ###############################################################################
  - alias: Check if pills were taken
    initial_state: true
    trigger:
      platform: template
      value_template: "{{ states('sensor.time') ==
        states('input_datetime.pill_reminder_time')[:-3] }}"
    condition:
      - condition: template
        value_template: "{{ states('input_boolean.pill_taken') == 'off' }}"
      - condition: template
        value_template: "{{ states('input_boolean.home_mode_away') == 'off' }}"
    action:
      - service: script.remind_pill

  ###############################################################################
  # Timer Elapsed - Time for a reminder if the pill is not taken yet
  ###############################################################################
  - alias: Pill Reminder Timer Elapsed
    initial_state: true
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_pill_reminder
    action:
      - service_template: script.remind_pill

script:
  ###############################################################################
  # Notifies to take pills, starts timer again!
  # Voice notifications at home - only if the option is selected.
  ###############################################################################
  remind_pill:
    sequence:
      - service: script.notify_family
        data:
          message: "ALERT: PLEASE TAKE YOUR TABLETS!"
          notify_options:
            - telegram
      - service: timer.start
        entity_id: timer.timer_pill_reminder

  ###############################################################################
  # Stops timer, Records that the pills are taken, and notifies!
  ###############################################################################
  pill_taken:
    sequence:
      - service: timer.cancel
        entity_id: timer.timer_pill_reminder
      - service: input_boolean.turn_on
        entity_id: input_boolean.pill_taken
      - service: script.notify_family
        data_template:
          message: >
            Thank you for taking tablets.
            {%- if state_attr('sensor.allergy_index_today', 'rating') != None %}
              {{- " Today's pollen level is : " ~ state_attr('sensor.allergy_index_today', 'rating') }}.
            {%- endif -%}
          notify_options:
            - telegram
            - tv
            - led
