###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   10/28/2017
#   @package        :   Holidays
#   @description    :   Retrieves the holiday
###############################################################################

homeassistant:
  customize:
    sensor.holiday:
      hidden: true
      icon: mdi:beach
      friendly_name: US Holiday

###############################################################################
# Sensor updates once every 4 hours (14400 seconds) & runs 6 times in 24 hours
#
# First it checks for holiday in static section, if that doesn't exist,
# it checks in the dynamic section. If neither exists, the value will be empty
###############################################################################
sensor:
  - platform: rest
    resource: https://raw.githubusercontent.com/skalavala/mysmarthome/master/holidays.json
    name: Holiday
    scan_interval: 14400
    value_template: >
      {% set today = now().month  ~ '/' ~ now().day  %}
      {% set holiday =  value_json.MAJOR_US.static[ today ] %}
      {% if holiday | trim == "" %}
        {% set today = now().month  ~ '/' ~ now().day ~ '/' ~ now().year %}
        {% set holiday =  value_json.MAJOR_US.dynamic[ today ] %}
      {% endif %}
      {{ holiday }}

###############################################################################
#                _                        _   _
#     /\        | |                      | | (_)
#    /  \  _   _| |_ ___  _ __ ___   __ _| |_ _  ___  _ __  ___
#   / /\ \| | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \/ __|
#  / ____ \ |_| | || (_) | | | | | | (_| | |_| | (_) | | | \__ \
# /_/    \_\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_|___/
#
###############################################################################
automation:
  - alias: Notify Holiday State Change
    initial_state: true
    trigger:
      - platform: state
        entity_id: sensor.holiday
    condition:
      - condition: template
        value_template: "{{ states('sensor.holiday') != 'unknown' }}"
      - condition: template
        value_template: "{{ states('sensor.holiday') | trim != '' }}"
    action:
      - service: persistent_notification.create
        data:
          message: "Today is {{ states('sensor.holiday') }}."
          title: "{{ states('sensor.holiday') }}"
      - service: script.notify_me
        data_template:
          message: "Today is: {{states('sensor.holiday') }}"

  - alias: Notify Indian Holidays
    initial_state: true
    trigger:
      platform: time
      at: "09:00:00"
    condition:
      - condition: template
        value_template: >-
          {%- set days2NextEvent = 0 -%}
          {%- if now().year == strptime(states.calendar.holidays_in_india.attributes.start_time, '%Y-%m-%d %H:%M:%S').year -%}
            {%- set days2NextEvent = strptime(states.calendar.holidays_in_india.attributes.start_time, '%Y-%m-%d %H:%M:%S').strftime('%j') | int - (now().strftime('%j') | int) -%}
          {%- else -%}
            {% set days2NextEvent = strptime((now().year ~ "-12-31 12:02:05"), '%Y-%m-%d %H:%M:%S').strftime('%j') | int - (now().strftime('%j') | int) + (strptime(states.calendar.holidays_in_india.attributes.start_time, '%Y-%m-%d %H:%M:%S').strftime('%j') | int) -%}
          {%- endif -%}
          {%- if days2NextEvent <= (states('input_number.calendar_remind_before_days') | int)  -%}
            {%- if days2NextEvent |int == 0 -%}
               true
            {%- elif days2NextEvent | int == 1 -%}
               true
            {%- elif days2NextEvent | int == 2 -%}
               true
            {%- else -%}
              false
            {%- endif -%}
          {%- else -%}
            false
          {%- endif -%}
    action:
      - service: script.notify_me
        data_template:
          message: >-
            {%- set days2NextEvent = 0 -%}
            {%- if now().year == strptime(states.calendar.holidays_in_india.attributes.start_time, '%Y-%m-%d %H:%M:%S').year -%}
              {%- set days2NextEvent = strptime(states.calendar.holidays_in_india.attributes.start_time, '%Y-%m-%d %H:%M:%S').strftime('%j') | int - (now().strftime('%j') | int) -%}
            {%- else -%}
              {% set days2NextEvent = strptime((now().year ~ "-12-31 12:02:05"), '%Y-%m-%d %H:%M:%S').strftime('%j') | int - (now().strftime('%j') | int) + (strptime(states.calendar.holidays_in_india.attributes.start_time, '%Y-%m-%d %H:%M:%S').strftime('%j') | int) -%}
            {%- endif -%}
            {%- if days2NextEvent <= (states('input_number.calendar_remind_before_days') | int) -%}
              {%- if days2NextEvent |int == 0 -%}
                 Today is {{ states.calendar.holidays_in_india.attributes.message | replace(".", "") | title }}.
              {%- elif days2NextEvent |int == 1 -%}
                 Tomorrow is {{ states.calendar.holidays_in_india.attributes.message | replace(".", "") | title }}.
              {%- elif days2NextEvent |int == 2 -%}
                 {{ states.calendar.holidays_in_india.attributes.message | replace(".", "")  | title }}. is day after tomorrow.
              {%- endif -%}
            {%- endif -%}