Make confirmable notification blueprint use unique actions (#50706)
parent
edccb7eb58
commit
b8713774c8
|
@ -51,6 +51,10 @@ blueprint:
|
|||
mode: restart
|
||||
|
||||
sequence:
|
||||
- alias: "Set up variables"
|
||||
variables:
|
||||
action_confirm: "{{ 'CONFIRM_' ~ context.id }}"
|
||||
action_dismiss: "{{ 'DISMISS_' ~ context.id }}"
|
||||
- alias: "Send notification"
|
||||
domain: mobile_app
|
||||
type: notify
|
||||
|
@ -59,16 +63,22 @@ sequence:
|
|||
message: !input message
|
||||
data:
|
||||
actions:
|
||||
- action: "CONFIRM"
|
||||
- action: "{{ action_confirm }}"
|
||||
title: !input confirm_text
|
||||
- action: "DISMISS"
|
||||
- action: "{{ action_dismiss }}"
|
||||
title: !input dismiss_text
|
||||
- alias: "Awaiting response"
|
||||
wait_for_trigger:
|
||||
- platform: event
|
||||
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:
|
||||
- conditions: "{{ wait.trigger.event.data.action == 'CONFIRM' }}"
|
||||
- conditions: "{{ wait.trigger.event.data.action == action_confirm }}"
|
||||
sequence: !input confirm_action
|
||||
- conditions: "{{ wait.trigger.event.data.action == 'DISMISS' }}"
|
||||
- conditions: "{{ wait.trigger.event.data.action == action_dismiss }}"
|
||||
sequence: !input dismiss_action
|
||||
|
|
|
@ -7,7 +7,8 @@ from unittest.mock import patch
|
|||
|
||||
from homeassistant.components import script
|
||||
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.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")
|
||||
context = Context()
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.mobile_app.device_action.async_call_action_from_config"
|
||||
) as mock_call_action:
|
||||
|
||||
# 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.
|
||||
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()
|
||||
|
||||
assert len(mock_call_action.mock_calls) == 1
|
||||
_hass, config, variables, _context = mock_call_action.mock_calls[0][1]
|
||||
|
||||
title_tpl = config.pop("title")
|
||||
message_tpl = config.pop("message")
|
||||
title_tpl.hass = hass
|
||||
message_tpl.hass = hass
|
||||
template.attach(hass, config)
|
||||
rendered_config = template.render_complex(config, variables)
|
||||
|
||||
assert config == {
|
||||
assert rendered_config == {
|
||||
"title": "Lord of the things",
|
||||
"message": "Throw ring in mountain?",
|
||||
"alias": "Send notification",
|
||||
"domain": "mobile_app",
|
||||
"type": "notify",
|
||||
"device_id": "frodo",
|
||||
"data": {
|
||||
"actions": [
|
||||
{"action": "CONFIRM", "title": "Confirm"},
|
||||
{"action": "DISMISS", "title": "Dismiss"},
|
||||
{"action": "CONFIRM_" + _context.id, "title": "Confirm"},
|
||||
{"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 turn_on_calls[0].data == {
|
||||
"entity_id": ["mount.doom"],
|
||||
|
|
Loading…
Reference in New Issue