###############################################################################
#   @author         :   Jeffrey Stone 
#   @date           :   02/19/2019
#   @package        :   Events
#   @description    :   Special Events.
###############################################################################

input_boolean:
  rex_manning_audio:
    name: Rex Manning audio
    icon: mdi:speaker-wireless
  blackout_time:
    name: Blackout Time
    icon: mdi:closed-octagon
  school_in_session:
    name: School In Session
    icon: mdi:bus-school
  school_is_digital:
    name: Digital Learning
    icon: mdi:laptop
  school_today:
    name: School Today
    icon: mdi:bus-school
  school_early_release:
    name: School Early Release
    icon: mdi:bus-school

input_datetime:
  school_first_day:
    name: First Day of School
    has_date: true
    has_time: false
  school_last_day:
    name: Last Day of School
    has_date: true
    has_time: false
  school_day_start:
    name: School Day Start
    has_date: false
    has_time: true
  school_day_end:
    name: School Day End
    has_date: false
    has_time: true
  fall_break_start:
    name: Fall Break
    has_date: true
    has_time: false
  thanksgiving_break_start:
    name: Thanksgiving Break
    has_date: true
    has_time: false
  christmas_break_start:
    name: Christmas Break
    has_date: true
    has_time: false
  winter_break_start:
    name: Winter Break
    has_date: true
    has_time: false
  spring_break_start:
    name: Spring Break
    has_date: true
    has_time: false
  school_day_start_reminder:
    name: School Day Start Reminder
    has_date: false
    has_time: true
  school_day_end_reminder:
    name: School Day End Reminder
    has_date: false
    has_time: true  
  blackout_time_start:
    name: Blackout Time Start
    has_date: false
    has_time: true
  blackout_time_end:
    name: Blackout Time End
    has_date: false
    has_time: true
  pest_service:
    name: Pest Service
    has_date: true
    has_time: true
  gutter_service:
    name: Pest Service
    has_date: true
    has_time: true
  


sensor:
  - platform: template
    sensors:
      school_start_days2go:
        value_template: "{{ ((state_attr('input_datetime.school_first_day', 'timestamp')-as_timestamp(now())) | int /60/1440) | round(0) }}"
        unit_of_measurement: 'Days'
      school_end_days2go:
        value_template: "{{ ((state_attr('input_datetime.school_last_day', 'timestamp')-as_timestamp(now())) | int /60/1440) | round(0) }}"
        unit_of_measurement: 'Days'
      vacation_days2go:
        value_template: "{{ ((as_timestamp(strptime(state_attr('calendar.vacation', 'start_time'), '%Y-%m-%d'))-as_timestamp(now())) | int /60/1440) | round(0) }}"
        unit_of_measurement: 'Days'
  - platform: rest
    resource: https://raw.githubusercontent.com/thejeffreystone/home-assistant-configuration/master/config/packages/json_data/school.json
    name: School Lunch
    scan_interval: 14400
    value_template: >
      {% set today = now().month  ~ '/' ~ now().day  %}
      {% set lunch =  value_json.MENU.static[ today ] %}
      {%- if lunch %}
        {{ lunch  }}
      {% else %}
        Nothing
      {%- endif %}

  - platform: rest
    resource: https://raw.githubusercontent.com/thejeffreystone/home-assistant-configuration/master/config/packages/json_data/school.json
    name: School Event
    scan_interval: 14400
    value_template: >
      {% set today = now().month  ~ '/' ~ now().day  %}
      {% set event =  value_json.EVENTS.static[ today ] %}
      {%- if event %}
        {{ event  }}
      {% else %}
        Nothing
      {%- endif %}

automation:
  - id: refresh_special_event_sensors
    alias: Refresh special event sensors
    initial_state: true
    trigger:
    - platform: time
      at: '00:00:01'
    - platform: homeassistant
      event: start
    action:
    - service: python_script.special_events
      data:
        name: Skylar
        type: birthday
        date: !secret skylar_bday
    - service: python_script.special_events
      data:
        name: Jeff
        type: birthday
        date: !secret jeff_bday
    - service: python_script.special_events
      data:
        name: Kat
        type: birthday
        date: !secret kat_bday
    - service: python_script.special_events
      data:
        name: Our wedding
        type: anniversary
        date: !secret wed_anniversary

  - id: e1cb2d02-0423-11eb-adc1-0242ac120002
    alias: School today
    initial_state: true
    trigger:
    - platform: time
      at: '06:00:00'
    condition:
    - condition: template
      value_template: >
        {%- if as_timestamp(strptime(state_attr('calendar.skylar_school', 'start_time'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom("%Y-%m-%d") == now().strftime("%Y-%m-%d") %}
          true
        {%- endif -%}
    action:
    - choose:
      - conditions:
        - condition: template
          value_template: >
            {{ 'Early Release' in states('sensor.school_event') }}
        sequence:
        - service: input_boolean.turn_on
          entity_id: input_boolen.school_early_release
        - service: input_datetime.set_datetime
          target:
            entity_id: input_datetime.school_day_end
          data:
            time: "12:15:00"
      default:
      - service: input_datetime.set_datetime
        target:
          entity_id: input_datetime.school_day_end
        data:
          time: "14:45:00" 
    - service: input_boolean.turn_on
      entity_id: input_boolean.school_today  
    - service: script.turn_on
      entity_id: script.school_in_session
    - service: script.turn_on
      entity_id: script.digital_learning_day
    
    

  - id: rex_manning_audio
    alias: Rex Manning Day Audio
    initial_state: true
    trigger:
    - platform: time
      at: '14:00:00'
    condition:
    - condition: state
      entity_id: calendar.anchorage_holidays
      state: "on"
    - condition: template
      value_template: >
        {%- set event=states.calendar.anchorage_holidays.attributes.message %}
        {%- if event == 'Rex Manning Day' %}
          true
        {%- endif -%}
    action:
    - service: script.turn_on
      entity_id: script.rex_manning_audio
    - delay:
        seconds: 40
    - service: script.turn_off
      entity_id: script.rex_manning_audio
    - service: media_player.media_stop
      entity_id: media_player.ha_blue

  - id: new_ha_notification
    alias: New HA Notification
    initial_state: true
    trigger:
    - platform: state
      entity_id: binary_sensor.updater
      from: 'off'
      to: 'on'
    action:
    - service: script.twitter_new_ha
    # - service: ifttt.trigger
    #   data_template: {"event":"GitPost", "value1":"Home Assistant {{ states.binary_sensor.updater.attributes.newest_version }} is now available.", "value2":"{{ states.binary_sensor.updater.attributes.newest_version }}"}
    - service: rest_command.new_version_github_issue
      data_template:
        version: "{{ states.binary_sensor.updater.attributes.newest_version }}"
  
  # - id: blackout_time_start
  #   initial_state: true
  #   alias: Blackout time Start
  #   trigger:
  #     platform: template
  #     value_template: "{{ states('sensor.time') == (state_attr('input_datetime.blackout_time_start', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
  #   condition:
  #     condition: template
  #     value_template: '{{ states.calendar.skylar_school.state == "Off" }}'
  #   action:
  #   - wait_template: "{{ not is_state('media_player.ha_blue', 'playing') }}"
  #   - service: script.status_annc
  #     data_template:
  #       who: '{{ states.sensor.room_presence.state }}'
  #       call_interuption: 1
  #       speech_message: "Blackout time has started."
  #   - service: input_boolean.turn_on
  #     entity_id: input_boolean.blackout_time

  # - id: blackout_time_end
  #   initial_state: true
  #   alias: Blackout time End
  #   trigger:
  #     platform: template
  #     value_template: "{{ states('sensor.time') == (state_attr('input_datetime.blackout_time_end', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
  #   condition:
  #     condition: state
  #     entity_id: input_boolean.blackout_time
  #     state: 'on'
  #   action:
  #   - wait_template: "{{ not is_state('media_player.ha_blue', 'playing') }}"
  #   - service: script.status_annc
  #     data_template:
  #       who: '{{ states.sensor.room_presence.state }}'
  #       call_interuption: 1
  #       speech_message: "Blackout time has ended"
  #   - service: input_boolean.turn_off
  #     entity_id: input_boolean.blackout_time

  

 


script:

  digital_learning_day:
    sequence:
    - condition: template
      value_template: >
        {{ 'Digital Learning' in states('sensor.school_event') }}
    - service: input_boolean.turn_on
      entity_id: input_boolen.school_is_digital
    
  school_in_session:
    sequence:
    - condition: state
      entity_id: input_boolean.school_in_session
      state: 'off'
    - service: input_boolean.turn_on
      entity_id: input_boolen.school_in_session

  school_year_over:
    sequence:
    - condition: state
      entity_id: input_boolean.school_in_session
      state: 'on'
    - condition: template
      value_template: >
        {{ states('input_datetime.school_last_day') == now().strftime("%Y-%m-%d") }}
    - service: input_boolean.turn_off
      entity_id: input_boolen.school_in_session

  rex_manning_audio:
    sequence:
    - condition: state
      entity_id: input_boolean.audible_notifications
      state: 'on'
    - condition: state
      entity_id: group.family
      state: 'home'
    - service: media_player.turn_on
      entity_id: media_player.ha_blue
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.ha_blue
        volume_level: .4
    - service: media_player.play_media
      entity_id: media_player.ha_blue
      data:
        media_content_id: /media/music/Rex_Manning_-_Say_No_More_Mon_Amour.mp3
        media_content_type: "music"

  digital_learning_notification:
    sequence:
    - condition: state
      entity_id: group.people
      state: 'home'
    - service: script.status_annc
      data_template:
        who: '{{ states.sensor.room_presence.state }}'
        call_interuption: 1
        speech_message: "Skylar, digital learning will begin in about thirty minutes. I have turned on the lighting around your desk."

  class_alarm:
    sequence:
    - service: script.status_annc
      data_template:
        who: '{{ states.sensor.room_presence.state }}'
        call_interuption: 1
        speech_message: >
          {{ class }} starts in two minutes.
    - service: script.text_notify
      data:
        who: 'parents'
        message: >
          {{ class }} starts in two minutes.

rest_command:
  new_version_github_issue:
    url: https://api.github.com/repos/thejeffreystone/home-assistant-configuration/issues
    method: POST
    headers:
      Authorization: !secret github_issue_token
    payload: '{"title":"Home-Assistant {{ version }} available","body":"Home-Assistant {{ version }} is now available"}'