Skip to content

Instantly share code, notes, and snippets.

@w35l3y
Last active July 31, 2023 14:52
Show Gist options
  • Save w35l3y/fa7086172c441fdaec9cd1f0d4f72e36 to your computer and use it in GitHub Desktop.
Save w35l3y/fa7086172c441fdaec9cd1f0d4f72e36 to your computer and use it in GitHub Desktop.
Alexa Intents - Main file is "examples_request.yaml"
>-
{%- set response = states("input_text.alexa_custom_skill_notification_response_rejected") -%}
{%- set text = (response if response else [
"Tudo bem",
"OK"
] | random) if states("input_text.alexa_custom_skill_notification_event_id") else "Negativo" -%}
{{- "<speak>%s</speak>" % (("<amazon:effect name=\"whispered\">%s</amazon:effect>" % text) if (as_timestamp(today_at()) <= as_timestamp(now()) <= as_timestamp(today_at("08:30:00"))) else text) }}
>-
{%- set response = states("input_text.alexa_custom_skill_notification_response_resolved") -%}
{%- set text = (response if response else [
"Tudo bem",
"OK",
"Feito",
"Prontinho"
] | random) if states("input_text.alexa_custom_skill_notification_event_id") else "Positivo" -%}
{{- "<speak>%s</speak>" % (("<amazon:effect name=\"whispered\">%s</amazon:effect>" % text) if (as_timestamp(today_at()) <= as_timestamp(now()) <= as_timestamp(today_at("08:30:00"))) else text) }}
# blueprints/automation/homeassistant/handle_alexa_notification_event.yaml
blueprint:
name: Alexa Notification Event Handler
description: Handles Alexa Notification Event
domain: automation
input:
event_id:
selector:
text:
intent:
selector:
text:
action:
selector:
action:
trigger:
platform: event
event_type: alexa_custom_skill_notification
event_data:
event_id: !input event_id
intent: !input intent
action: !input action
intent_script: !include intent_scripts.yaml
input_text:
alexa_custom_skill_notification_speech:
name: Speech - Alexa Custom Skill Notification
min: 0
max: 255
initial: ""
mode: text
alexa_custom_skill_notification_reprompt:
name: Reprompt - Alexa Custom Skill Notification
min: 0
max: 255
initial: ""
mode: text
alexa_custom_skill_notification_event_id:
name: Event - Alexa Custom Skill Notification
min: 0
max: 255
initial: ""
pattern: "^\\w+$"
mode: text
alexa_custom_skill_notification_fallback_intent:
name: Fallback Intent - Alexa Custom Skill Notification
min: 0
max: 255
initial: ""
mode: text
alexa_custom_skill_notification_response_resolved:
name: Response resolved - Alexa Custom Skill Notification
min: 0
max: 255
initial: ""
mode: text
alexa_custom_skill_notification_response_rejected:
name: Response rejected - Alexa Custom Skill Notification
min: 0
max: 255
initial: ""
mode: text
alexa_custom_skill_notification_params:
name: Params
min: 0
max: 255
initial: ""
mode: text
# Aqui são os exemplos de como a execução inicia.
# Basicamente, é uma chamada para o script "script.run_alexa_custom_skill_notification" com parâmetros específicos.
# No momento, a limitação para que isso funcione é que precisa informar "reprompt", obrigatoriamente.
# Isso se deve ao fato de que Home Assistant apenas envia o comando de manter a sessão esperando por uma resposta quando esta opção foi enviada.
# Talvez, futuramente, o Home Assistant permita parametrizar a continuação da sessão mesmo sem "reprompt" preenchido.
# Estes exemplos, assim como a resposta deles, são meramente ilustrativos, ou seja, não servem para copiar e colar tal como estão pois eles incluem certas entidades e scripts que não fazem parte do escopo do tutorial.
# exemplo 1
- service: script.run_alexa_custom_skill_notification
data:
speech: O sensor da janela não está funcionando. Fecho a cortina do quarto
mesmo assim?
event_id: confirmar_fechamento_cortina_quarto
reprompt: Fecho a cortina do quarto?
target: media_player.echo_dot_do_quarto_do_casal
# exemplo 2
- service: script.run_alexa_custom_skill_notification
data:
speech: Está na hora de trabalhar. Ligo os monitores?
event_id: workday
target: media_player.echo_dot_do_quarto_do_casal
reprompt: Ligo os monitores?
fallback: AMAZON.YesIntent
# exemplo 3
- service: script.run_alexa_custom_skill_notification
data:
speech: Há {{ states('sensor.occupied_rooms') | int }} cômodos ocupados. Habilito
o modo visitante?
reprompt: Habilito o modo visitante?
event_id: habilitar_modo_visitante
target: media_player.echo_dot_da_sala
fallback: AMAZON.NoIntent
# exemplo 4
- service: script.run_alexa_custom_skill_notification
data:
speech: Desligo as luzes da sala?
reprompt: Desligo as luzes da sala?
target: media_player.echo_dot_da_sala
event_id: desligar_sala_meia_noite
fallback: AMAZON.YesIntent
# Aqui são os exemplos de como as respostas são tratadas.
# Basicamente, são automações que são executadas para um conjunto específico de dados de um evento.
# exemplo 1
- alias: Alexa Intents - Fechar cortina do quarto
trigger:
- platform: event
event_type: alexa_custom_skill_notification
event_data:
event_id: confirmar_fechamento_cortina_quarto
intent: AMAZON.YesIntent
action:
- service: script.set_cover_position
data:
action: closed
target:
- cover.cortina_do_quarto
mode: single
# exemplo 2
- alias: Alexa Intents - Ligar monitores para trabalhar
trigger:
- platform: event
event_type: alexa_custom_skill_notification
event_data:
event_id: workday
intent: AMAZON.YesIntent
action:
- service: input_boolean.turn_on
entity_id: input_boolean.acoes_trabalho
- service: switch.turn_on
target:
entity_id: switch.monitores
mode: single
# exemplo 3
- alias: Alexa Intents - Habilitar Modo Visitante
use_blueprint:
path: homeassistant/handle_alexa_notification_event.yaml
input:
event_id: habilitar_modo_visitante
intent: AMAZON.YesIntent
action:
- service: input_datetime.set_datetime
data:
datetime: '{{ today_at() + timedelta(days = 2) }}'
target:
entity_id: input_datetime.modo_visitante
- service: notify.meu_celular
data:
message: Modo visitante ativado
# exemplo 4
- alias: Alexa Intents - Desligar sala a meia-noite
use_blueprint:
path: homeassistant/handle_alexa_notification_event.yaml
input:
event_id: desligar_sala_meia_noite
intent: AMAZON.YesIntent
action:
- service: switch.turn_off
target:
entity_id:
- switch.luz_da_mesa
- switch.luz_do_sofa
- condition: template
value_template: '{{ trigger.event.data.fallback }}'
- service: media_player.turn_off
target:
entity_id: media_player.tv
amzn1.ask.skill.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:
async_action: true
speech:
type: ssml
text: >
{%- set text = states("input_text.alexa_custom_skill_notification_speech") -%}
{{- "<speak>%s</speak>" % text if text else "" -}}
reprompt:
type: ssml
text: >
{%- set text = states("input_text.alexa_custom_skill_notification_reprompt") -%}
{{- "<speak>%s</speak>" % text if text else "" -}}
AMAZON.YesIntent:
async_action: true
speech:
type: ssml
text: !include alexa_resolve.yaml
action:
- choose:
- conditions:
- condition: template
value_template: '{{ true if states("input_text.alexa_custom_skill_notification_event_id") else false }}'
sequence:
- event: alexa_custom_skill_notification
event_data:
fallback: false
event_id: '{{ states("input_text.alexa_custom_skill_notification_event_id") }}'
intent: AMAZON.YesIntent
params: '{{ states("input_text.alexa_custom_skill_notification_params") }}'
speech: '{{ states("input_text.alexa_custom_skill_notification_speech") }}'
- service: script.clear_alexa_custom_skill_notification
data: {}
AMAZON.NoIntent:
async_action: true
speech:
type: ssml
text: !include alexa_reject.yaml
action:
- choose:
- conditions:
- condition: template
value_template: '{{ true if states("input_text.alexa_custom_skill_notification_event_id") else false }}'
sequence:
- event: alexa_custom_skill_notification
event_data:
fallback: false
event_id: '{{ states("input_text.alexa_custom_skill_notification_event_id") }}'
intent: AMAZON.NoIntent
params: '{{ states("input_text.alexa_custom_skill_notification_params") }}'
speech: '{{ states("input_text.alexa_custom_skill_notification_speech") }}'
- service: script.clear_alexa_custom_skill_notification
data: {}
amzn1.ask.skill.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.SessionEndedRequest:
async_action: true
action:
- choose:
- conditions:
- condition: template
value_template: '{{ true if states("input_text.alexa_custom_skill_notification_fallback_intent") else false }}'
sequence:
- event: alexa_custom_skill_notification
event_data:
fallback: true
event_id: '{{ states("input_text.alexa_custom_skill_notification_event_id") }}'
intent: '{{ states("input_text.alexa_custom_skill_notification_fallback_intent") }}'
params: '{{ states("input_text.alexa_custom_skill_notification_params") }}'
speech: '{{ states("input_text.alexa_custom_skill_notification_speech") }}'
- service: script.clear_alexa_custom_skill_notification
data: {}
AMAZON.FallbackIntent:
async_action: true
action:
- choose:
- conditions:
- condition: template
value_template: '{{ true if states("input_text.alexa_custom_skill_notification_fallback_intent") else false }}'
sequence:
- event: alexa_custom_skill_notification
event_data:
fallback: true
event_id: '{{ states("input_text.alexa_custom_skill_notification_event_id") }}'
intent: '{{ states("input_text.alexa_custom_skill_notification_fallback_intent") }}'
params: '{{ states("input_text.alexa_custom_skill_notification_params") }}'
speech: '{{ states("input_text.alexa_custom_skill_notification_speech") }}'
- service: script.clear_alexa_custom_skill_notification
data: {}
AMAZON.StopIntent:
async_action: true
speech:
type: ssml
text: !include alexa_resolve.yaml
action:
- choose:
- conditions:
- condition: template
value_template: '{{ true if states("input_text.alexa_custom_skill_notification_event_id") else false }}'
sequence:
- event: alexa_custom_skill_notification
event_data:
fallback: false
event_id: '{{ states("input_text.alexa_custom_skill_notification_event_id") }}'
intent: AMAZON.StopIntent
params: '{{ states("input_text.alexa_custom_skill_notification_params") }}'
speech: '{{ states("input_text.alexa_custom_skill_notification_speech") }}'
- service: script.clear_alexa_custom_skill_notification
data: {}
run_alexa_custom_skill_notification:
description: "Activates an actionable notification on a specific echo device"
# mode: queued
mode: single
fields:
speech:
description: Texto para Alexa falar.
example: "Tudo bem com você?"
default: "Tudo bem com você?"
required: true
selector:
template:
event_id:
description: ID do evento para identificar a resposta.
example: "saudacao_alexa"
default: ""
required: false
advanced: true
selector:
text:
reprompt:
description: Resposta esperada após responder para Alexa. Se for vazio, Alexa não aguardará por resposta.
example: "Está tudo bem?"
default: "Está tudo bem?"
required: false
selector:
template:
target:
description: Dispositivo da Alexa que irá falar.
example: "media_player.echo_dot_do_quarto"
default: "media_player.echo_dot_do_quarto"
required: false
selector:
entity:
domain: media_player
fallback:
description: 'Intenção de fallback caso não seja respondido em tempo.'
example: 'AMAZON.YesIntent'
default: 'AMAZON.YesIntent'
required: false
advanced: true
selector:
text:
response_resolved:
description: Texto para Alexa falar numa resposta positiva.
example: "Feito!"
default: ""
required: false
selector:
template:
response_rejected:
description: Texto para Alexa falar numa resposta negativa.
example: "Ok"
default: ""
required: false
selector:
template:
params:
description: 'Parâmetros opcionais'
required: false
advanced: true
selector:
object:
sequence:
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_speech
value: '{{- speech | default("") | regex_replace("<(\/)?speak>", "") -}}'
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_reprompt
value: '{{- reprompt | default("") | regex_replace("<(\/)?speak>", "") -}}'
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_event_id
value: '{{- event_id | default("generic_alexa_notification") -}}'
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_fallback_intent
value: '{{- fallback | default("") -}}'
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_params
value: '{{- params | default({}) -}}'
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_response_resolved
value: '{{- response_resolved | default("") -}}'
- service: input_text.set_value
data:
entity_id: input_text.alexa_custom_skill_notification_response_rejected
value: '{{- response_rejected | default("") -}}'
- service: media_player.play_media
data:
entity_id: '{{- target | default("media_player.echo_dot_da_sala") -}}'
media_content_type: skill
media_content_id: !secret alexa_media_content_id
clear_alexa_custom_skill_notification:
description: "Clears alexa notification"
mode: single
sequence:
- service: input_text.set_value
data:
entity_id:
- input_text.alexa_custom_skill_notification_speech
- input_text.alexa_custom_skill_notification_reprompt
- input_text.alexa_custom_skill_notification_event_id
- input_text.alexa_custom_skill_notification_fallback_intent
- input_text.alexa_custom_skill_notification_response_resolved
- input_text.alexa_custom_skill_notification_response_rejected
value: ""
alexa_media_content_id: amzn1.ask.skill.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment