From 37fa0fac4c65803ac901b77c819b4fb2d8cb5b54 Mon Sep 17 00:00:00 2001 From: HNKNTA Date: Sat, 30 Dec 2023 20:22:58 +0200 Subject: [PATCH 1/5] Added: override ability for `notification_link` --- notifications.yaml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/notifications.yaml b/notifications.yaml index 99f99cf..60c5900 100644 --- a/notifications.yaml +++ b/notifications.yaml @@ -883,6 +883,14 @@ fields: selector: boolean: + field_notification_link: + name: "🔗 Notification Link" + description: "Override notification link" + example: !input notification_link + default: !input notification_link + selector: + text: + # [Unsupported due to `choose.sequence`] - Can't pass variables to choose.sequence. # field_timeout_action: # name: "⌛️ Timeout Action(s)" @@ -907,7 +915,8 @@ sequence: script_subtitle: !input subtitle subtitle: "{{ iif(field_subtitle is defined, field_subtitle, script_subtitle) }}" interruption_level: !input interruption_level - notification_link: !input notification_link + script_notification_link: !input notification_link + notification_link: "{{ iif(field_notification_link is defined, field_notification_link, script_notification_link) }}" visibility: !input visibility importance: !input importance custom_tag: !input tag # No need for manual definition anymore, treat existing definitions as custom tags. From f1d9b3885bf46259d1e1a5b74f138ce5fa455db8 Mon Sep 17 00:00:00 2001 From: Samuel Thng Date: Fri, 19 Jan 2024 14:04:55 +0800 Subject: [PATCH 2/5] Added: Return payload and response as variable. --- notifications.yaml | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/notifications.yaml b/notifications.yaml index 60c5900..1517f39 100644 --- a/notifications.yaml +++ b/notifications.yaml @@ -1178,24 +1178,44 @@ sequence: event_data: tag: "{{ tag }}" + ############################## + # Parse user response + ############################## + - alias: "Parse response into variable" + variables: + user_response: >- + {% set response = namespace(data={ 'payload': notification_data }) %} + {% if run_timeout_actions and wait.trigger == none %} + {% set response.data = dict(response.data, **{ 'result': "timeout" }) %} + {% elif run_timeout_actions and swipe_away_as_timeout and wait.trigger.event.event_type == 'mobile_app_notification_cleared' %} + {% set response.data = dict(response.data, **{ 'result': "notification_cleared" }) %} + {% elif wait.trigger.event.data.action == option_one['action'] %} + {% set response.data = dict(response.data, **{ 'result': "option_one" }) %} + {% elif wait.trigger.event.data.action == option_two['action'] %} + {% set response.data = dict(response.data, **{ 'result': "option_two" }) %} + {% elif wait.trigger.event.data.action == option_three['action'] %} + {% set response.data = dict(response.data, **{ 'result': "option_three" }) %} + {%- endif %} + {{ response.data }} + ############################## # Trigger Response Actions ############################## - choose: - alias: "Trigger: Timeout" - conditions: "{{ run_timeout_actions and wait.trigger == none }}" + conditions: "{{ user_response.result == 'timeout' }}" sequence: !input timeout_action # "{{ timeout_actions }}" - alias: "Trigger: Notification Cleared" - conditions: "{{ run_timeout_actions and swipe_away_as_timeout and wait.trigger.event.event_type == 'mobile_app_notification_cleared' }}" + conditions: "{{ user_response.result == 'notification_cleared' }}" sequence: !input timeout_action # "{{ timeout_actions }}" - alias: "Trigger: first_option" - conditions: "{{ wait.trigger.event.data.action == option_one['action'] }}" + conditions: "{{ user_response.result == 'option_one' }}" sequence: !input confirm_action # "{{ option_one_actions }}" - alias: "Trigger: second_option" - conditions: "{{ wait.trigger.event.data.action == option_two['action'] }}" + conditions: "{{ user_response.result == 'option_two' }}" sequence: !input dismiss_action # "{{ option_two_actions }}" - alias: "Trigger: third_option" - conditions: "{{ wait.trigger.event.data.action == option_three['action'] }}" + conditions: "{{ user_response.result == 'option_three' }}" sequence: !input option_three_action # "{{ option_three_actions }}" ############################## @@ -1216,3 +1236,9 @@ sequence: message: "clear_notification" data: tag: "{{ tag }}" + + ############################## + # Return user response + ############################## + - stop: "Script stopped." + response_variable: "user_response" From 1f214b6bc7765f0a692960319651807d12fd2c42 Mon Sep 17 00:00:00 2001 From: Samuel Thng Date: Fri, 19 Jan 2024 15:03:47 +0800 Subject: [PATCH 3/5] Fixed: Field Enable Timeout Override --- notifications.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/notifications.yaml b/notifications.yaml index 1517f39..373dbd8 100644 --- a/notifications.yaml +++ b/notifications.yaml @@ -923,7 +923,8 @@ sequence: first_option: "{{ 'FIRST_' ~ context.id }}" second_option: "{{ 'SECOND_' ~ context.id }}" third_option: "{{ 'THIRD_' ~ context.id }}" - enable_timeout: !input enable_timeout + script_enable_timeout: !input enable_timeout + enable_timeout: "{{ iif(field_enable_timeout is defined, field_enable_timeout, script_enable_timeout) }}" clear_on_timeout: !input clear_on_timeout persist: !input persist script_timeout: !input timeout # Load timeout into variable for use with templates. From 721098c6c6edf5f549ff826ca0b2c45dd61682dd Mon Sep 17 00:00:00 2001 From: Samuel Thng Date: Fri, 19 Jan 2024 15:04:07 +0800 Subject: [PATCH 4/5] Refactor: Do not wait if no response expected. --- notifications.yaml | 102 +++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/notifications.yaml b/notifications.yaml index 373dbd8..fd5ee09 100644 --- a/notifications.yaml +++ b/notifications.yaml @@ -1073,6 +1073,7 @@ sequence: {{ [option_one, option_two, option_three] | selectattr('enabled') | list }} tag: "{{ iif(custom_tag|length, custom_tag, this.entity_id ~ '-' ~ context.id) }}" group: !input group + expect_response: "{{ enable_timeout or option_one_enabled or option_two_enabled or option_three_enabled }}" # If there's no timeout nor options, then no point waiting. Stop script. # Build OS specific notification payloads. notification_data: >- {% set notifications = namespace(data=[])%} @@ -1134,50 +1135,55 @@ sequence: ############################## # Evaluate Response ############################## - - if: + - alias: Determine if script has to wait for response? + choose: + - alias: "No need for response" + conditions: "{{ expect_response == false }}" + sequence: [] - alias: "Should await response with timeout?" - condition: template - value_template: "{{ enable_timeout == true }}" - then: - - alias: "Awaiting response with timeout…" - wait_for_trigger: - - platform: event - event_type: mobile_app_notification_action - event_data: - action: "{{ first_option }}" - - platform: event - event_type: mobile_app_notification_action - event_data: - action: "{{ second_option }}" - - platform: event - event_type: mobile_app_notification_action - event_data: - action: "{{ third_option }}" - - platform: event - event_type: mobile_app_notification_cleared - event_data: - tag: "{{ tag }}" - timeout: "{{ timeout }}" - continue_on_timeout: true - else: - - alias: "Awaiting response indefinitely…" - wait_for_trigger: - - platform: event - event_type: mobile_app_notification_action - event_data: - action: "{{ first_option }}" - - platform: event - event_type: mobile_app_notification_action - event_data: - action: "{{ second_option }}" - - platform: event - event_type: mobile_app_notification_action - event_data: - action: "{{ third_option }}" - - platform: event - event_type: mobile_app_notification_cleared - event_data: - tag: "{{ tag }}" + conditions: "{{ enable_timeout }}" + sequence: + - alias: "Awaiting response with timeout…" + wait_for_trigger: + - platform: event + event_type: mobile_app_notification_action + event_data: + action: "{{ first_option }}" + - platform: event + event_type: mobile_app_notification_action + event_data: + action: "{{ second_option }}" + - platform: event + event_type: mobile_app_notification_action + event_data: + action: "{{ third_option }}" + - platform: event + event_type: mobile_app_notification_cleared + event_data: + tag: "{{ tag }}" + timeout: "{{ timeout }}" + continue_on_timeout: true + - alias: "Should await response indefinitely?" + conditions: "{{ enable_timeout == false }}" + sequence: + - alias: "Awaiting response indefinitely…" + wait_for_trigger: + - platform: event + event_type: mobile_app_notification_action + event_data: + action: "{{ first_option }}" + - platform: event + event_type: mobile_app_notification_action + event_data: + action: "{{ second_option }}" + - platform: event + event_type: mobile_app_notification_action + event_data: + action: "{{ third_option }}" + - platform: event + event_type: mobile_app_notification_cleared + event_data: + tag: "{{ tag }}" ############################## # Parse user response @@ -1186,7 +1192,9 @@ sequence: variables: user_response: >- {% set response = namespace(data={ 'payload': notification_data }) %} - {% if run_timeout_actions and wait.trigger == none %} + {% if not expect_response %} + {% set response.data = dict(response.data, **{ 'result': "no_response" }) %} + {% elif run_timeout_actions and wait.trigger == none %} {% set response.data = dict(response.data, **{ 'result': "timeout" }) %} {% elif run_timeout_actions and swipe_away_as_timeout and wait.trigger.event.event_type == 'mobile_app_notification_cleared' %} {% set response.data = dict(response.data, **{ 'result': "notification_cleared" }) %} @@ -1202,7 +1210,9 @@ sequence: ############################## # Trigger Response Actions ############################## - - choose: + - alias: "Run response actions" + default: [] + choose: - alias: "Trigger: Timeout" conditions: "{{ user_response.result == 'timeout' }}" sequence: !input timeout_action # "{{ timeout_actions }}" @@ -1229,7 +1239,7 @@ sequence: - if: - alias: "Should send clear notification command? (for iOS/macOS)?" condition: template - value_template: "{{ enable_timeout and clear_on_timeout and notification_data[repeat.index - 1].apple_device }}" + value_template: "{{ expect_response and enable_timeout and clear_on_timeout and notification_data[repeat.index - 1].apple_device }}" then: - alias: "Send clear notification command to iOS device" service: "{{ repeat.item }}" From 0dbdb2b73253f4f62f6e3065908c0c11a9da2da5 Mon Sep 17 00:00:00 2001 From: Samuel Thng Date: Fri, 19 Jan 2024 23:37:41 +0800 Subject: [PATCH 5/5] Updated: Changelog --- notifications.yaml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/notifications.yaml b/notifications.yaml index fd5ee09..629bb98 100644 --- a/notifications.yaml +++ b/notifications.yaml @@ -1,6 +1,6 @@ mode: restart blueprint: - name: 🔔 Notifications (Version 2.0.1 - Multi-device) + name: 🔔 Notifications (Version 2.0.2 Beta - Multi-device) source_url: https://github.com/samuelthng/t-house-blueprints/blob/multidevice/notifications.yaml homeassistant: min_version: 2023.11.0 @@ -9,7 +9,7 @@ blueprint: description: >-

🔔 Notifications

- Version 2.0.1 - Multi-device | 🔗 Github Link | 💬 Community Post + Version 2.0.2 Beta - Multi-device | 🔗 Github Link | 💬 Community Post

@@ -42,13 +42,14 @@ blueprint:
Expand/collapse changelog - ### Version 2.0.1 Multiple Device - *20 Dec 2023* - - Fixed: `Custom Tag` and `Groups` not being set properly. + ### Version 2.0.2 Beta Multiple Device - *19 Jan 2023* + - Added: Field for `notification_link`. [#26](https://github.com/samuelthng/t-house-blueprints/pull/26) (Thanks to [HNKNTA](https://github.com/HNKNTA)) + - Fixed: Field `enable_timeout`. + - Added: `response_variable` with payload and response. - ### Version 2 Multiple Device - *12 Dec 2023* [🔗](https://community.home-assistant.io/t/notifications-actionable-mobile-notifications-script-with-optional-timeout-feature-and-camera-snapshots-works-with-ios-android/551552/109) + ### Version 2.0.1 Multiple Device - *20 Dec 2023* [🔗](https://community.home-assistant.io/t/notifications-actionable-mobile-notifications-script-with-optional-timeout-feature-and-camera-snapshots-works-with-ios-android/551552/111?u=samuelthng) - ### Version 1.6.1 Multiple Device - *31 Oct 2023* - - Added: Experimental Multiple Device Support + ### Version 2 Multiple Device - *12 Dec 2023* [🔗](https://community.home-assistant.io/t/notifications-actionable-mobile-notifications-script-with-optional-timeout-feature-and-camera-snapshots-works-with-ios-android/551552/109) ### Version 1.6.1 - *26 Oct 2023* [🔗](https://community.home-assistant.io/t/notifications-actionable-mobile-notifications-script-with-optional-timeout-feature-and-camera-snapshots-works-with-ios-android/551552/66)