Make confirmable notification blueprint use unique actions (#50706)

This commit is contained in:
Zac West 2021-05-15 22:50:24 -07:00 committed by GitHub
parent edccb7eb58
commit b8713774c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 17 deletions

View File

@ -51,6 +51,10 @@ blueprint:
mode: restart mode: restart
sequence: sequence:
- alias: "Set up variables"
variables:
action_confirm: "{{ 'CONFIRM_' ~ context.id }}"
action_dismiss: "{{ 'DISMISS_' ~ context.id }}"
- alias: "Send notification" - alias: "Send notification"
domain: mobile_app domain: mobile_app
type: notify type: notify
@ -59,16 +63,22 @@ sequence:
message: !input message message: !input message
data: data:
actions: actions:
- action: "CONFIRM" - action: "{{ action_confirm }}"
title: !input confirm_text title: !input confirm_text
- action: "DISMISS" - action: "{{ action_dismiss }}"
title: !input dismiss_text title: !input dismiss_text
- alias: "Awaiting response" - alias: "Awaiting response"
wait_for_trigger: wait_for_trigger:
- platform: event - platform: event
event_type: mobile_app_notification_action event_type: mobile_app_notification_action
event_data:
action: "{{ action_confirm }}"
- platform: event
event_type: mobile_app_notification_action
event_data:
action: "{{ action_dismiss }}"
- choose: - choose:
- conditions: "{{ wait.trigger.event.data.action == 'CONFIRM' }}" - conditions: "{{ wait.trigger.event.data.action == action_confirm }}"
sequence: !input confirm_action sequence: !input confirm_action
- conditions: "{{ wait.trigger.event.data.action == 'DISMISS' }}" - conditions: "{{ wait.trigger.event.data.action == action_dismiss }}"
sequence: !input dismiss_action sequence: !input dismiss_action

View File

@ -7,7 +7,8 @@ from unittest.mock import patch
from homeassistant.components import script from homeassistant.components import script
from homeassistant.components.blueprint.models import Blueprint, DomainBlueprints from homeassistant.components.blueprint.models import Blueprint, DomainBlueprints
from homeassistant.core import HomeAssistant, callback from homeassistant.core import Context, HomeAssistant, callback
from homeassistant.helpers import template
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.util import yaml from homeassistant.util import yaml
@ -70,44 +71,48 @@ async def test_confirmable_notification(hass: HomeAssistant) -> None:
) )
turn_on_calls = async_mock_service(hass, "homeassistant", "turn_on") turn_on_calls = async_mock_service(hass, "homeassistant", "turn_on")
context = Context()
with patch( with patch(
"homeassistant.components.mobile_app.device_action.async_call_action_from_config" "homeassistant.components.mobile_app.device_action.async_call_action_from_config"
) as mock_call_action: ) as mock_call_action:
# Trigger script # Trigger script
await hass.services.async_call(script.DOMAIN, "confirm") await hass.services.async_call(script.DOMAIN, "confirm", context=context)
# Give script the time to attach the trigger. # Give script the time to attach the trigger.
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
hass.bus.async_fire("mobile_app_notification_action", {"action": "CONFIRM"}) hass.bus.async_fire("mobile_app_notification_action", {"action": "ANYTHING_ELSE"})
hass.bus.async_fire(
"mobile_app_notification_action", {"action": "CONFIRM_" + Context().id}
)
hass.bus.async_fire(
"mobile_app_notification_action", {"action": "CONFIRM_" + context.id}
)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(mock_call_action.mock_calls) == 1 assert len(mock_call_action.mock_calls) == 1
_hass, config, variables, _context = mock_call_action.mock_calls[0][1] _hass, config, variables, _context = mock_call_action.mock_calls[0][1]
title_tpl = config.pop("title") template.attach(hass, config)
message_tpl = config.pop("message") rendered_config = template.render_complex(config, variables)
title_tpl.hass = hass
message_tpl.hass = hass
assert config == { assert rendered_config == {
"title": "Lord of the things",
"message": "Throw ring in mountain?",
"alias": "Send notification", "alias": "Send notification",
"domain": "mobile_app", "domain": "mobile_app",
"type": "notify", "type": "notify",
"device_id": "frodo", "device_id": "frodo",
"data": { "data": {
"actions": [ "actions": [
{"action": "CONFIRM", "title": "Confirm"}, {"action": "CONFIRM_" + _context.id, "title": "Confirm"},
{"action": "DISMISS", "title": "Dismiss"}, {"action": "DISMISS_" + _context.id, "title": "Dismiss"},
] ]
}, },
} }
assert title_tpl.async_render(variables) == "Lord of the things"
assert message_tpl.async_render(variables) == "Throw ring in mountain?"
assert len(turn_on_calls) == 1 assert len(turn_on_calls) == 1
assert turn_on_calls[0].data == { assert turn_on_calls[0].data == {
"entity_id": ["mount.doom"], "entity_id": ["mount.doom"],