# https://www.home-assistant.io/integrations/telegram
# https://www.home-assistant.io/integrations/telegram_chatbot
telegram_bot:
  - platform: polling
    api_key: !secret telegram_token
    allowed_chat_ids:
      - !secret telegram_georgi_chat_id

# https://www.home-assistant.io/integrations/tts
tts:
  - platform: amazon_polly
    aws_access_key_id: !secret aws_polly_client_id
    aws_secret_access_key: !secret aws_polly_client_secret
    region_name: us-east-1
    voice: Amy
    engine: neural

input_boolean:
  speech_notifications:
    name: Speech notifications
    icon: mdi:chat

  sarah_talking:
    name: SARAH talking
    icon: mdi:chat
    initial: off

script:
  say:
    alias: Speech · Say
    sequence:
      # Don't talk while in sleep mode.
      - condition: state
        entity_id: input_boolean.sleep_mode
        state: "off"
      # Combine partials for speech message.
      - service: script.say_discretely
        data_template:
          message: >-
            {%- if partials is defined -%}
              {%- for partial in partials -%}
                {{ partial }}{{ " " }}
              {%- endfor -%}
            {%- else -%}
              {{ message }}
            {%- endif -%}

  say_discretely:
    alias: Speech · Say discretely
    sequence:
      # Figure out whether to talk or send a text.
      - service_template: >
          {% if is_state(states('input_select.sonos_leader'), 'playing')
              or is_state('input_boolean.speech_notifications', 'off')
              or is_state('input_boolean.georgi_home', 'off') %}
            notify.telegram
          {% else %}
            script.sonos_blast
          {% endif %}
        data_template:
          title: "💬 *Speech Module*" # For telegram only
          message: "{{ message }}"

  sonos_blast:
    alias: Speech · Sonos blast
    sequence:
      # Mark SARAH as talking.
      - service: input_boolean.turn_on
        entity_id: input_boolean.sarah_talking
      # Don't shout :)
      - service: media_player.volume_set
        entity_id: group.all_sonos
        data_template:
          volume_level: >-
            {% if now().strftime('%H')|int < 6 and now().strftime('%H')|int > 0 %}
              0.2
            {% else %}
              0.4
            {% endif %}
      # Say message.
      - service: tts.amazon_polly_say
        data_template:
          entity_id: "{{ states('input_select.sonos_leader') }}"
          message: "{{ message }}"
      # Wait for phrase to be over and mark as not talking.
      - delay: 00:00:01
      - wait_template: "{{ is_state('binary_sensor.sonos_leader_paused', 'on') }}"
        timeout: 00:02:00
      - service: input_boolean.turn_off
        entity_id: input_boolean.sarah_talking

  sonos_say:
    alias: Speech · Sonos say
    sequence:
      - service: sonos.snapshot
        data_template:
          with_group: yes
          entity_id: media_player.master_bedroom
      - delay: 00:00:02
      - service: script.sonos_blast
        data_template:
          message: "{{ message }}"
      - delay: 00:00:05
      - service: sonos.restore
        data_template:
          with_group: yes
          entity_id: media_player.master_bedroom

automation:
  - alias: Speech · Telegram · Action · Do nothing
    trigger:
      platform: event
      event_type: telegram_callback
      event_data:
        command: "/do_nothing"
    action:
      - service: telegram_bot.delete_message
        data_template:
          message_id: "{{ trigger.event.data.message.message_id}}"
          chat_id: "{{ trigger.event.data.chat_id }}"
