###############################################################################
#   @author         :   Jeffrey Stone 
#   @date           :   02/19/2019
#   @package        :   Notify
#   @description    :   A Collection of Notification Scripts and Configs. 
#                       Package modified from https://github.com/skalavala/smarthome/blob/master/packages/notify.yaml with inspiration
# 		                  from https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/config/script/speech_engine.yaml		
###############################################################################

###############################################################################
# AWS Polly Options:
# pause:
#   <break time="3s"/>
# emphasis: 
#   <emphasis level="strong">really like</emphasis>
#   level attribute values:
#     Strong: Increases the volume and slows the speaking rate so that the speech is louder and slower.
#     Moderate: Increases the volume and slows the speaking rate, but less than strong. Moderate is the default.
#     Reduced: Decreases the volume and speeds up the speaking rate. Speech is softer and faster.
# Pause Between Paragraphs:
#   Wrap text in <p> </p>
# Adding a Pause Between Sentences:
#   Ending a sentence with a period (.)
# Controlling How Special Types of Words Are Spoken:
#   <say-as interpret-as="value">[text to be interpreted]</say-as>
#   characters or spell-out: Spells out each letter of the text, as in a-b-c. 
#   cardinal or number: Interprets the numerical text as a cardinal number, as in 1,234.
#   ordinal: Interprets the numerical text as an ordinal number, as in 1,234th.
#   digits: Spells out each digit individually, as in 1-2-3-4.
#   fraction: Interprets the numerical text as a fraction. This works for both common fractions such as 3/20, and mixed fractions, such as 2 ½. See below for more information.
#   unit: Interprets a numerical text as a measurement. The value should be either a number or a fraction followed by a unit with no space in between as in 1/2inch, or by just a unit, as in 1meter.
#   date: Interprets the text as a date. The format of the date must be specified with the format attribute. See below for more information.
#   time: Interprets the numerical text as duration, in minutes and seconds, as in 1'21".
#   address: Interprets the text as part of a street address.
#   expletive: "Beeps out" the content included within the tag.
#   telephone: Interprets the numerical text as a 7-digit or 10-digit telephone number, as in 2025551212. You can also use this value for handle telephone extensions, as in 2025551212x345. See below for more information.	
################################################################################


tts:
  - platform: google_translate
    cache: true
    cache_dir: /tmp/tts
    time_memory: 300
  - platform: amazon_polly
    aws_access_key_id: !secret aws_key
    aws_secret_access_key: !secret aws_secret
    region_name: 'us-east-1'
    text_type: ssml
    voice: Brian
    cache: True

notify:
  - platform: ios
  - name: all_ios
    platform: group
    services:
      - service: mobile_app_jeffreys_iphone_11
      - service: mobile_app_jeffreys_ipad
      - service: mobile_app_iphone
      - service: mobile_app_skylars_iphone
  - name: ios_parents
    platform: group
    services:
      - service: mobile_app_jeffreys_iphone_11
      - service: mobile_app_jeffreys_ipad
      - service: mobile_app_iphone
  - name: jeff_ios
    platform: group
    services:
      - service: mobile_app_jeffreys_iphone_11
      - service: mobile_app_jeffreys_ipad
  - name: kat_ios
    platform: group
    services:
      - service: mobile_app_iphone
  - name: skylar_ios
    platform: group
    services:
      - service: mobile_app_skylars_iphone
  - name: twitter
    platform: twitter
    consumer_key: !secret twitter_consumer_key
    consumer_secret: !secret twitter_consumer_secret
    access_token: !secret twitter_access_token
    access_token_secret: !secret twitter_access_secret
  - name: twitter_js
    platform: twitter
    consumer_key: !secret twitter2_consumer_key
    consumer_secret: !secret twitter2_consumer_secret
    access_token: !secret twitter2_access_token
    access_token_secret: !secret twitter2_access_secret
  - name: twitter_sl
    platform: twitter
    consumer_key: !secret twitter3_consumer_key
    consumer_secret: !secret twitter3_consumer_secret
    access_token: !secret twitter3_access_token
    access_token_secret: !secret twitter3_access_secret
  - platform: alexa_media
    name: alexa_media


automation:

  - id: audio_isssue
    alias: Audio Issue
    initial_state: true
    trigger:
    - platform: state
      entity_id: media_player.ha_speaker
      to: 'unavailable'
    condition:
    - condition: state
      entity_id: binary_sensor.day
      state: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.audible_notifications
      - service: script.text_notify
        data_template:
          who: "jeff"
          title: "Audio Issue"
          message: "HA Audible Notifications turning off due to Chromecast Issue"

  - id: audio_isssue_cleared
    alias: Audio Issue Cleared
    initial_state: true
    trigger:
    - platform: state
      entity_id: media_player.ha_speaker
      from: 'unavailable'
      to: 'off'
    condition:
    - condition: state
      entity_id: binary_sensor.day
      state: 'on'
    action:
      - service: script.text_notify
        data_template:
          who: "jeff"
          title: "Audio Issue Cleared"
          message: "Main hromecast is back on"
      - service: script.turn_on_ha_speaker


  
script:


###############################################################################
# Txt Notify
###############################################################################
  text_notify_all:
    sequence:
      - service: notify.all_ios
        data_template:
          message: >
            {{ message }}

  text_notify_jeff:
    sequence:
      - condition: state
        entity_id: input_boolean.text_notify_jeff
        state: 'on'
      - service: notify.jeff_ios
        data_template:
          message: >
            {{ message }}

  text_notify_kat:
    sequence:
      - condition: state
        entity_id: input_boolean.text_notify_kat
        state: 'on'
      - service: notify.kat_ios
        data_template:
          message: >
            {{ message }}

  text_notify:
    sequence:
      - condition: state
        entity_id: input_boolean.text_notifications
        state: 'on'
      - service: >
            {% if who == 'jeff' %}
              notify.jeff_ios
            {% elif who == 'kat' %}
              notify.kat_ios
            {% elif who == 'skylar' %}
              notify.skylar_ios
            {% elif who == 'parents' %}
              notify.ios_parents
            {% else %}
              notify.all_ios
            {% endif %}
        data:
          title: '{{ title }}'
          message: '{{ message }}'
          #data:
          #   attachment:
          #     url: '{{ url }}'
          #     content-type: '{{ content_type }}'
          #     hide-thumbnail: false
          #  push:
          #     sound: '{{ ios_sound }}'
          #    badge: 0
          #     category: '{{ ios_category }}'
          #   entity_id: '{{ camera_entity }}'

  text_alert:
    sequence:
      - service: >
            {% if who == 'jeff' %}
              notify.jeff_ios
            {% elif who == 'kat' %}
              notify.kat_ios
            {% elif who == 'skylar' %}
              notify.skylar_ios
            {% elif who == 'parents' %}
              notify.ios_parents
            {% else %}
              notify.all_ios
            {% endif %}
        data:
          title: '{{ title }}'
          message: '{{ message }}'
          #data:
          #   attachment:
          #     url: '{{ url }}'
          #     content-type: '{{ content_type }}'
          #     hide-thumbnail: false
          #  push:
          #     sound: '{{ ios_sound }}'
          #    badge: 0
          #     category: '{{ ios_category }}'
          #   entity_id: '{{ camera_entity }}'

###############################################################################
# Alert Notify
# Conditions:
#   => Vacation mode should be off
# Services:
#   => Audible 
#   => iOS 
###############################################################################
  alert_notify:
    sequence:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: 'off'
      - service: switch.turn_on 
        entity_id: switch.ha_speaker
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.guest_tv
          message: >
            {{ message }}

  jarvis_alert:
    sequence:
    - service: mqtt.publish
      data_template:
        topic: 'house/polly/lastmsg'
        payload: 'This message is from {{ now().strftime("%-I") }}:{{ now().strftime("%M") }} {{ now().strftime("%p") }}. {{ message | truncate(220)}}'
        retain: true
    - service: media_player.turn_on
      data_template:
        entity_id: >
          {% if who == 'kitchen' %}
            media_player.kitchen_display
          {% elif who == 'croft' %}
            media_player.upstairs_speaker
          {% elif who == 'master_bedroom' %}
            media_player.master_bedroom_speaker
          {% elif who == 'studio' %}
            media_player.theater
          {% elif who == 'all_google' %}
            media_player.house
          {% elif who == 'main' %}
            media_player.ha_speaker
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
            media_player.ha_speaker
            {% else %}
            media_player.house
            {% endif %}
          {% endif %}
    - service: media_player.volume_set
      data_template:
        entity_id: >
          {% if who == 'kitchen' %}
            media_player.kitchen_display
          {% elif who == 'croft' %}
            media_player.upstairs_speaker
          {% elif who == 'master_bedroom' %}
            media_player.master_bedroom_speaker
          {% elif who == 'studio' %}
            media_player.theater
          {% elif who == 'all_google' %}
            media_player.house
          {% elif who == 'main' %}
            media_player.ha_speaker
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
            media_player.ha_speaker
            {% else %}
            media_player.house
            {% endif %}
          {% endif %}
        volume_level: >
          {% if who == 'kitchen' %}
            .80
          {% elif who == 'croft' %}
            .50
          {% elif who == 'master_bedroom' %}
            .50
          {% elif who == 'studio' %}
            .30
          {% elif who == 'all_google' %}
            .99
          {% elif who == 'main' %}
            .99
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
              .75
            {% else %}
              .99
            {% endif %}
          {% endif %}
    - service: tts.amazon_polly_say
      data_template:
        entity_id: >
          {% if who == 'kitchen' %}
            media_player.kitchen_display
          {% elif who == 'croft' %}
            media_player.upstairs_speaker
          {% elif who == 'master_bedroom' %}
            media_player.master_bedroom_speaker
          {% elif who == 'studio' %}
            media_player.theater
          {% elif who == 'all_google' %}
            media_player.house
          {% elif who == 'main' %}
            media_player.ha_speaker
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
            media_player.ha_speaker
            {% else %}
            media_player.house
            {% endif %}
          {% endif %}
        message: >-
          <speak>
          <amazon:auto-breaths>
            {{ message }}
          </amazon:auto-breaths>
          </speak>
        cache: true




###############################################################################
# Voice Notify
# Conditions:
#   => Only Announce when people are home.
#   => Only Announce when Audible Notifications are on
###############################################################################

  voice_notify:
    sequence:
      - condition: template
        value_template: '{{ states.input_boolean.audible_notifications.state == "on" }}'
      - condition: state
        entity_id: sensor.family_status
        state: 'Home'
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: 'off'
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.guest_tv
          message: >-
            {{message }}

 

  jarvis_voice:
    sequence:
    - service: mqtt.publish
      data_template:
        topic: 'house/polly/lastmsg'
        payload: 'This message is from {{ now().strftime("%-I") }}:{{ now().strftime("%M") }} {{ now().strftime("%p") }}. {{ message | truncate(220)}}'
        retain: true
    - condition: template
      value_template: '{{ states.binary_sensor.quiet_time.state == "off" }}'
    - condition: state
      entity_id: group.family
      state: 'home'
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    - service: media_player.turn_on
      data_template:
        entity_id: >
          {% if who == 'kitchen' %}
            media_player.kitchen_display
          {% elif who == 'croft' %}
            media_player.upstairs_speaker
          {% elif who == 'master_bedroom' %}
            media_player.master_bedroom_speaker
          {% elif who == 'theater' %}
            media_player.theater
          {% elif who == 'all_google' %}
            media_player.house
          {% elif who == 'main' %}
            media_player.ha_speaker
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
            media_player.ha_speaker
            {% else %}
            media_player.house
            {% endif %}
          {% endif %}
    - service: media_player.volume_set
      data_template:
        entity_id: >
          {% if who == 'kitchen' %}
            media_player.kitchen_display
          {% elif who == 'croft' %}
            media_player.upstairs_speaker
          {% elif who == 'master_bedroom' %}
            media_player.master_bedroom_speaker
          {% elif who == 'theater' %}
            media_player.theater
          {% elif who == 'all_google' %}
            media_player.house
          {% elif who == 'main' %}
            media_player.ha_speaker
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
            media_player.ha_speaker
            {% else %}
            media_player.house
            {% endif %}
          {% endif %}
        volume_level: >
            {% if states.input_boolean.audible_notifications.state == 'on' %}
              .6
            {% else %}
              .4
            {% endif %}
    - service: tts.amazon_polly_say
      data_template:
        entity_id: >
          {% if who == 'kitchen' %}
            media_player.kitchen_display
          {% elif who == 'croft' %}
            media_player.upstairs_speaker
          {% elif who == 'master_bedroom' %}
            media_player.master_bedroom_speaker
          {% elif who == 'studio' %}
            media_player.theater
          {% elif who == 'all_google' %}
            media_player.house
          {% elif who == 'main' %}
            media_player.ha_speaker
          {% else %}
            {% if states.input_boolean.audible_notifications.state == 'on' %}
            media_player.ha_speaker
            {% else %}
            media_player.house
            {% endif %}
          {% endif %}
        message: >-
          <speak>
          <break time="1s"/>
          <amazon:auto-breaths>
            {{ message }}
          </amazon:auto-breaths>
          </speak>
        cache: true
  
  

###############################################################################
# Twitter
###############################################################################


  twitter_notify:
    sequence:
      - service: >
          {% if who == 'thejeffreystone' %}
            notify.twitter_js
          {% elif who == 'slackerlabs' %}
            notify.twitter_sl
          {% else %}
            notify.twitter
          {% endif %}
        data_template:
          message: >
            {% set msg = '' %}
            {% set msg = msg + ' ' + message %}
            {% set msg = msg + ' #iot #smarthome' %}
            {{ msg }}

  twitter_notify_image:
    sequence:
      - service: >
          {% if who == 'thejeffreystone' %}
            notify.twitter_js
          {% elif who == 'slackerlabs' %}
            notify.twitter_sl
          {% else %}
            notify.twitter
          {% endif %}
        data_template:
          message: >-
            {{ tweet }} #iot #smarthome
          data:
            media: >-
              {{ image }}

  twitter_snark:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_snark.yaml
        
  twitter_stats:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_stats.yaml 

  twitter_promos:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_promos.yaml
          who: '{{ who }}'

  twitter_follow_me:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_follow_me.yaml
          who: '{{ who }}'

  twitter_selfpromos:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_selfpromos.yaml
          who: '{{ who }}'

  twitter_recent_content:
    sequence:
      - service: script.twitter_notify
        data:
          who: '{{ who }}'
          message: !include ../templates/twitter_recent_content.yaml
          

  twitter_great_content:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_great_content.yaml
          who: '{{ who }}'
  
  twitter_new_ha:
    sequence:
      - service: script.twitter_notify
        data_template:
          message: !include ../templates/twitter_new_ha.yaml
          who: '{{ who }}'


###############################################################################
# Weather Report
###############################################################################
#   weather_report:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_weather_alerts: 1
#           call_current_conditions_outside: 1
#           call_current_conditions_inside: 1
#           call_current_conditions_garage: 1
#           call_daily_forecast: 1
#           call_overnight_forecast: 1
#           call_future_forecast: 1


# ###############################################################################
# # Traffic Report
# ###############################################################################
#   traffic_report:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_traffic_conditions: 1

# ###############################################################################
# # Security Report
# ###############################################################################
#   security_report:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_security_report: 1

# ###############################################################################
# # Jeff Traffic Reports
# ###############################################################################
#   jeff_eta_report:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_jeff_location: 1

#   jeff_headed_home_annc:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_jeff_headed_home: 1


# ###############################################################################
# # Kat Traffic Reports
# ###############################################################################
#   kat_eta_report:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_kat_location: 1

#   kat_headed_home_annc:
#     sequence:
#       - service: script.ah_report
#         data:
#           call_kat_headed_home: 1