###############################
# input_datetime - So the time report automation fires can be set in the UI
###############################

input_datetime:
  morning_report:
    name: Morning Report
    has_date: false
    has_time: true
    icon: mdi:home-analytics
  nightly_report:
    name: Nightly Report
    has_date: false
    has_time: true
    icon: mdi:home-analytics
  daily_report:
    name: Daily Report
    has_date: false
    has_time: true
    icon: mdi:home-analytics
  kallen_morning_briefing:
    name: Kallen Morning Briefing
    has_date: false
    has_time: true
    icon: mdi:weather-sunset-up
  kallen_nightly_briefing:
    name: Kallen Nightly Briefing
    has_date: false
    has_time: true
    icon: mdi:weather-night
  audible_notification_on:
    name: Audible Notifications On
    has_date: false
    has_time: true
    icon: mdi:volume-high
  audible_notification_off:
    name: Audible Notifications Off
    has_date: false
    has_time: true
    icon: mdi:volume-off
  announcement_delay:
    name: Announcement Delay
    has_date: false
    has_time: true
    icon: mdi:clock-edit

input_select:
  kallen_morning_briefing_location:
    name: Kallen Morning Briefing Location
    options:
      - Kallen Bedroom
      - Common Areas
    icon: mdi:map-marker

################################
# Announcment Automation - fires at the time of the above input_datetimes
################################

automation:
  
  - id: 81bee5ee-6820-4626-aebf-3deb8de69e4d
    alias: Turn On Audible Notifications
    initial_state: true
    trigger:
      platform: time
      at: input_datetime.audible_notification_on
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.audible_notifications
    - service: switch.turn_off
      target:
        entity_id:
          - switch.basement_echo_dot_do_not_disturb_switch
          - switch.living_room_echo_dot_do_not_disturb_switch

  # Turn off audible notifications if they have't been turned off yet. 
  - id: e0e9c774-6abe-42aa-bdab-32108bebb0e9
    alias: Turn Off Audible Notifications
    initial_state: true
    trigger:
    - platform: time
      at: input_datetime.audible_notification_off
    action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.audible_notifications

  - id: db50d96f-8e2a-4e48-8d7c-ce5968527b82
    alias: Kallen Morning Briefing
    trigger:
    - platform: time
      at: input_datetime.kallen_morning_briefing
    condition:
    - condition: state
      entity_id: input_boolean.kallen_overnight
      state: 'off'
    action:
    - service: script.kallen_morning_briefing

  - id: b8d21d04-c263-4d33-9590-9fb1e76b0de8
    alias: Kallen Nightly Briefing
    trigger:
    - platform: time
      at: input_datetime.kallen_nightly_briefing
    condition:
    - condition: state
      entity_id: input_boolean.kallen_overnight
      state: 'off'
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: script.kallen_nightly_briefing
      data:
        who: common

  - id: c53d84ee-9e6d-4268-8d44-d427c722602a
    alias: Daily Briefing
    trigger:
    - platform: time
      at: input_datetime.daily_report
    condition:
    - condition: state
      entity_id: binary_sensor.people_present
      state: 'on'
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: script.daily_briefing
      data:
        who: common

  - id: 5d06ba92-de10-4bf3-bc08-86f5be60c4a7
    alias: Morning Briefing
    trigger:
    - platform: time
      at: input_datetime.morning_report
    condition:
    - condition: state
      entity_id: binary_sensor.people_present
      state: 'on'
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: script.morning_report
      data:
        who: common

  - id: 933acebd-c788-4640-babd-6c30093cd2ba
    alias: Nightly Briefing
    trigger:
      platform: time
      at: input_datetime.nightly_report
    condition:
    - condition: state
      entity_id: binary_sensor.people_present
      state: 'on'
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: script.nightly_briefing
      data:
        who: common

  - id: d7641b5e-3cbb-4fb3-b6ed-5edf3157b88d
    alias: Kallen Briefing Location Change
    trigger:
      - platform: state
        entity_id: input_boolean.kallen_awake
        from: 'off'
        to: 'on'
    condition:
      - condition: state
        entity_id: input_boolean.school_today
        state: 'on'
      - condition: state
        entity_id: input_boolean.kallen_overnight
        state: 'off'
    action:
      - service: input_select.select_option
        data:
          option: Common Areas
        target:
          entity_id: input_select.kallen_morning_briefing_location

script:
  reset_annc_switches:
    sequence:
      - service: input_boolean.turn_off
        entity_id: input_boolean.good_morning_report

  morning_briefing:
    sequence:
      - service: button.press
        target:
          entity_id: button.good_morning

  kallen_nightly_briefing:
    sequence:
    - service: script.speech_engine
      data: 
        who: "{{ who }}"
        message: !include ../templates/speech/kallen_nightly_briefing.yaml
        voice: Emma
    - service: script.text_notify
      data:
        who: "parents"
        message: "I just gave Kallen's nightly briefing."

  kallen_morning_briefing:
    sequence:
      - choose:
        - conditions: 
          - condition: state
            entity_id: input_boolean.school_today
            state: 'on'
          sequence:
          - service: light.turn_on
            target:
              entity_id: light.kallen_bedroom_lights
        default: []
      - service: script.speech_engine
        data:
          who: >
            {% if is_state('input_select.kallen_morning_briefing_location','Kallen Bedroom') %}
              kallen_bedroom
            {% elif is_state('input_select.kallen_morning_briefing_location','Common Areas') %}
              common
            {% else %}
              common
            {% endif %}
          message: !include ../templates/speech/kallen_morning_briefing.yaml
          voice: Emma
      - service: script.text_notify
        data:
          who: "parents"
          message: "I just gave Kallen's morning briefing"

  kallen_school_status:
    sequence:
      - service: script.speech_engine
        data:
          who: "{{ who }}"
          message: !include ../templates/speech/kallen_school_status.yaml
          voice: Emma

  daily_briefing:
    sequence:
    - service: script.speech_engine
      data_template:
        who: "{{ who }}"
        message: !include ../templates/speech/daily_briefing.yaml

  nightly_briefing:
    sequence:
      - service: script.speech_engine
        data_template:
          who: "{{ who }}"
          message: !include ../templates/speech/nightly_briefing.yaml

  morning_report:
    sequence:
    - service: script.speech_engine
      data_template:
        who: "{{ who }}"
        message: !include ../templates/speech/morning_briefing.yaml

  dinner_is_ready:
    sequence:
    - service: script.status_annc
      data_template:
        who: 'everywhere'
        call_dinner_is_ready: 1

  welcome_home:
    sequence:
      - condition: state
        entity_id: input_boolean.welcome_home
        state: 'on'
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: 'off'
      - wait_template: "{{ is_state('binary_sensor.front_door','on') }}"
        timeout:
          minutes: 10
        continue_on_timeout: false
      - delay:
          seconds: 5
      - service: script.speech_engine
        data_template:
          who: living_room
          message: !include ../templates/speech/welcome_home.yaml
    mode: restart