From b39f87253e31a40610c2529bd7455897e9ffce3d Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Fri, 16 Aug 2024 18:34:22 +0000 Subject: [PATCH 1/7] Several changes --- google_home_resume.yaml | 202 ++++++++++++++++++++-------------------- 1 file changed, 103 insertions(+), 99 deletions(-) diff --git a/google_home_resume.yaml b/google_home_resume.yaml index 76a1f9b..0fbbe60 100755 --- a/google_home_resume.yaml +++ b/google_home_resume.yaml @@ -1,4 +1,4 @@ -# Scripts and automation to resume streams on Google Home speakes after it has been interrupted by service calls +# Scripts and automation to resume streams on Google Home speakes after it has been interrupted by actions # Supports resuming of Spotify and online streams (like TuneIn) # Package contains 3 main scripts, the helper script to perform the resume and supporting template sensor and automation # for more details: @@ -54,6 +54,7 @@ homeassistant: seconds: 20 max_runtime: minutes: 10 + stop_before_action: true # settings for automation enable_automation: false dashboard_cast: true @@ -72,13 +73,13 @@ group: script: # The main Google Home Resume script google_home_resume: - alias: "00 - Google Home Resume" + alias: "Google Home Resume" description: Script for resuming Google Home speakers icon: mdi:cast-audio mode: parallel fields: target: - description: "Enter the targets in case they are not clear from the service calls" + description: "Enter the targets in case they are not clear from the actions" required: false selector: target: @@ -88,7 +89,7 @@ script: device: integration: cast action: - description: "Actions to be performed (only service calls will work)" + description: "Actions to be performed (only those using action: will work)" required: true selector: action: @@ -107,7 +108,7 @@ script: sequence: - alias: "Version number" variables: &version - version: &ver 2023.8.0 + version: &ver 2024.8.0b0 settings: "{{ settings | default({}, true) }}" - alias: "Failsafe to remove all groups if the script was not running" if: > @@ -127,35 +128,37 @@ script: for_each: "{{ groups_existing }}" sequence: - alias: "Remove one group" - service: group.remove + action: group.remove data: object_id: "{{ states[repeat.item].object_id }}" - alias: "Set based on other scripts" variables: event_script: "{{ event_script | default(false) }}" automation: "{{ automation | default(false) }}" - - alias: "Set variables for service calls" + - alias: "Set variables for actions" variables: &service_vars - service_calls: > + actions: > {% set action = action | default([]) %} - {{ iif(action is mapping,[ action ], action) | selectattr('service', 'defined') | list }} - no_service_count: > + {% set services = iif(action is mapping,[ action ], action) | selectattr('service', 'defined') | list %} + {% set actions = iif(action is mapping,[ action ], action) | selectattr('action', 'defined') | list %} + {{ services + actions }} + no_action_count: > {% set action = action | default([]) %} - {{ iif(action is mapping,[ action ], action) | count - service_calls | count }} + {{ iif(action is mapping,[ action ], action) | count - actions | count }} - alias: "Wrong actions provided" choose: - - conditions: "{{ not (event_script or automation) and service_calls | count == 0 }}" + - conditions: "{{ not (event_script or automation) and actions | count == 0 }}" sequence: - - stop: "There were no service calls defined, no actions are performed. The script script has been aborted." + - stop: "There were no actions defined, nothing is performed. The script script has been aborted." error: true - - conditions: "{{ not (event_script or automation) and no_service_count > 0 }}" + - conditions: "{{ not (event_script or automation) and no_action_count > 0 }}" sequence: - - service: system_log.write + - action: system_log.write data: level: "warning" logger: "{{ this.entity_id }}" message: > - {{ no_service_count }} out of {{ service_calls | count }} actions were not performed because they are not service calls. + {{ no_action_count }} out of {{ actions | count }} items were not performed because they are not actions. - alias: "Set variables to be used in the script" variables: &generic_vars start_time: "{{ start_time if start_time is defined else now() }}" @@ -186,7 +189,7 @@ script: if: "{{ spotify_entities | count > 0 }}" then: - alias: "Update Spotify entities" - service: homeassistant.update_entity + action: homeassistant.update_entity target: entity_id: "{{ spotify_entities }}" - alias: "Store data of media player entities and determine target_list" @@ -336,20 +339,23 @@ script: {%- set e = ((e | replace(' ', '')).split(',') if e is string else e) | select('in', cast_entities) | list %} {# add targets based on actions #} {# macro #} - {%- macro service_data(service_calls, type) -%} + {%- macro action_data(actions, type) -%} {%- macro to_string(item) -%} {{- (item if item is string else item | join(',')) | replace(' ', '') -}} {%- endmacro -%} {%- set ns = namespace(data=[]) -%} - {%- for call in service_calls -%} + {%- for action in actions -%} {%- set input = [ - call.get(type, []), - call.get('data', {}).get(type, []), - call.get('target', {}).get(type, []), - call.get('extra', {}).get(type, []), - call.get('data', {}).get('script_extra', {}).get(type, []) + action.get(type, []), + action.get('data', {}).get(type, []), + action.get('target', {}).get(type, []), + action.get('extra', {}).get(type, []), + action.get('data', {}).get('script_extra', {}).get(type, []) ] -%} + {%- if type == 'entity_id' and (action.get('action') or action.get('service')) == 'tts.speak' -%} + {%- set input = input + [action.get('data', {}).get('media_player_entity_id')] -%} + {%- endif -%} {%- for i in input -%} {%- set ns.data = ns.data + [to_string(i)] -%} {%- endfor -%} @@ -357,11 +363,11 @@ script: {{- (ns.data | select() | join(',')) -}} {%- endmacro -%} {# entities #} - {%- set entity_list = e + service_data(service_calls, 'entity_id').split(',') | unique | list %} + {%- set entity_list = e + action_data(actions, 'entity_id').split(',') | unique | list %} {# devices #} - {%- set device_list = d + service_data(service_calls, 'device_id').split(',') | unique | list %} + {%- set device_list = d + action_data(actions, 'device_id').split(',') | unique | list %} {# areas #} - {%- set area_list = a + service_data(service_calls, 'area_id').split(',') | unique | list %} + {%- set area_list = a + action_data(actions, 'area_id').split(',') | unique | list %} {# create entity list based on device/area input #} {# determine entities of areas #} {%- set area_list = area_list | map('area_entities') | sum(start=[]) | list %} @@ -429,7 +435,7 @@ script: - alias: "Send log message about wrong wrong primary_spotcast" if: "{{ not primary_spotcast_check }}" then: - - service: system_log.write + - action: system_log.write data: level: "warning" logger: "{{ this.entity_id }}" @@ -459,7 +465,7 @@ script: | list }} - alias: "Create group with entities for which the script is active" - service: group.set + action: group.set data: object_id: resume_script_active_ghresume_{{ context.id | lower }} name: "Entities currently active with Google Home Resume script" @@ -472,7 +478,7 @@ script: if: "{{ iif(resume_this_action is defined and resume_this_action,false,true) }}" then: - alias: "Create group entities with resume_this_action to false" - service: group.set + action: group.set data: object_id: resume_action_false_ghresume_{{ context.id | lower }} name: "Entities with actions which should not be resumed" @@ -486,7 +492,7 @@ script: {{ target_no_volume | count > 0 }} then: - alias: "Turn non playing entities on" - service: media_player.turn_on + action: media_player.turn_on continue_on_error: true target: entity_id: "{{ target_no_volume }}" @@ -523,7 +529,7 @@ script: | count > 0 }} then: - - service: ytube_music_player.call_method + - action: ytube_music_player.call_method data: entity_id: > {{ @@ -534,9 +540,9 @@ script: | list }} command: interrupt_start - - alias: "Perform service calls defined in action" + - alias: "Perform actions defined in action" repeat: - for_each: "{{ service_calls }}" + for_each: "{{ actions }}" sequence: - variables: service_call: "{{ repeat.item }}" @@ -561,7 +567,7 @@ script: {{ dict(extra, **script_extra) }} volume_set: "{{ iif(extra.get('volume', '')) }}" wait_set: "{{ extra.get('wait', false) }}" - tts: "{{ service_call.service.split('.')[0] == 'tts' }}" + tts: "{{ (service_call.get('action') or service_call.get('service')).split('.')[0] == 'tts' }}" target_entities: > {%- set e = target.get('entity_id', []) %} {%- set d = target.get('device_id', []) %} @@ -610,18 +616,20 @@ script: }} data: > {{ dict(service_call.get('data', {}).items() | rejectattr('0', 'in', ['entity_id', 'area_id', 'device_id', 'script_extra']) | list) }} - - alias: "Stop all targets which are playing" - service: media_player.media_stop - target: - entity_id: "{{ expand(media_entities) | selectattr('state', 'eq', 'playing') | map(attribute='entity_id') | list }}" - - alias: "Wait until they are all stopped" - wait_template: "{{ expand(media_entities) | selectattr('state', 'eq', 'playing') | list | count == 0 }}" + - if: "{{ settings.get('stop_before_action', false) }}" + then: + - alias: "Stop all targets which are playing" + action: media_player.media_stop + target: + entity_id: "{{ expand(media_entities) | selectattr('state', 'eq', 'playing') | map(attribute='entity_id') | list }}" + - alias: "Wait until they are all stopped" + wait_template: "{{ expand(media_entities) | selectattr('state', 'eq', 'playing') | list | count == 0 }}" - alias: "Set volume to TTS volume if set" choose: - conditions: "{{ volume_set }}" sequence: - alias: "Apply TTS volume" - service: media_player.volume_set + action: media_player.volume_set target: entity_id: > {{ @@ -639,7 +647,7 @@ script: - conditions: "{{ tts and screen }}" sequence: - alias: "Call TTS for screen script" - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume_helper data: @@ -651,7 +659,7 @@ script: picture_url: "{{ extra.screen_tts.get('picture_url', '') }}" action_type: tts continue_on_error: true - - service: "{{ service_call.service }}" + - action: "{{ service_call.get('action') or service_call.get('service') }}" target: > {%- if screen and tts %} {{ dict(entity_id = settings.dummy_player) }} @@ -691,7 +699,7 @@ script: variables: player: "{{ player_data | selectattr('entity_id', 'eq', repeat.item) | join }}" - alias: "Start perform resume script" - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume_helper data: @@ -725,14 +733,14 @@ script: for_each: "{{ groups_to_remove }}" sequence: - alias: "Remove group" - service: group.remove + action: group.remove data: object_id: > {{ states[repeat.item].object_id }} # The Google Home Resume Helpers script which actually performs the resume on the speaker google_home_resume_helper: - alias: "00 - Google Home Resume - Helper Script" + alias: "Google Home Resume - Helper Script" description: Helper script for the Google Home Resume script icon: mdi:cast-audio mode: parallel @@ -757,19 +765,15 @@ script: ) }} dashboard_cast: "{{ is_state_attr(player.entity_id, 'app_name', 'Home Assistant Lovelace') }}" + last_updated: "{{ states[player.entity_id].last_updated }}" - alias: "State changed since script started?" wait_template: > {{ event_script or ytube_resume - or ( - (states[player.entity_id].last_updated > as_datetime(start_time)) - and expand([player.entity_id] + player.members) - | selectattr('state', 'eq', 'playing') - | list | count > 0 - ) or image or dashboard_cast + or states[player.entity_id].last_updated > as_datetime(start_time) }} timeout: "00:00:30" - variables: @@ -777,7 +781,7 @@ script: current_volume: "{{ state_attr(player.entity_id, 'volume_level') | round(2, default=0.0) }}" data_volume: "{{ player.get('volume_level', 0.0) | round(2) }}" - alias: "Add entity to the restore active group" - service: group.set + action: group.set data: object_id: resume_active_ghresume_{{ context.id | lower }} name: "Entities which will be resumed by the Google Home Resume script" @@ -812,7 +816,7 @@ script: for_each: "{{ player.members if player.type == 'group' else [ player.entity_id ] }}" sequence: - alias: "Set volume back to old state" - service: media_player.volume_set + action: media_player.volume_set target: entity_id: "{{ repeat.item }}" data: @@ -826,7 +830,7 @@ script: if: "{{ player.type == 'screen' and not resume }}" then: - alias: "Turn Google Home off to return to idle mode (photo display)" - service: media_player.turn_off + action: media_player.turn_off target: entity_id: "{{ player.entity_id }}" - alias: "Resume needed?" @@ -856,13 +860,13 @@ script: conditions: "{{ bbc }}" sequence: - alias: "Play video" - service: media_player.play_media + action: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: media_content_type: cast media_content_id: '{{ dict(app_name="youtube", media_id=player.media_content_id) | to_json }}' - - service: media_player.play_media + - action: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -890,7 +894,7 @@ script: if: "{{ wait.completed }}" then: &seek - alias: "Seek" - service: media_player.media_seek + action: media_player.media_seek target: entity_id: "{{ player.entity_id }}" data: @@ -901,7 +905,7 @@ script: sequence: - variables: player_ytube: "{{ player.yt_player }}" - - service: ytube_music_player.call_method + - action: ytube_music_player.call_method data: entity_id: "{{ player_ytube }}" command: interrupt_resume @@ -916,7 +920,7 @@ script: conditions: "{{ player.type == 'screen' and youtube }}" sequence: - alias: "Play video" - service: media_player.play_media + action: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -942,7 +946,7 @@ script: account_data: account: "{{ player.spotcast }}" - alias: "Resume spotify" - service: spotcast.start + action: spotcast.start data: > {{ service_data if player.spotcast == 'primary_account' else dict(service_data, **account_data) }} - alias: "Wait until song is playing" @@ -954,7 +958,7 @@ script: then: *seek else: - alias: "Resume spotify" - service: spotcast.start + action: spotcast.start data: > {{ service_data if player.spotcast == 'primary_account' else dict(service_data, **account_data) }} - alias: "Wait until song is playing" @@ -977,7 +981,7 @@ script: images: - url: "{{ picture_url }}" - alias: "Resume stream" - service: media_player.play_media + action: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -1005,7 +1009,7 @@ script: images: - url: "{{ picture_url }}" - alias: "Resume stream" - service: media_player.play_media + action: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -1021,7 +1025,7 @@ script: if: "{{ wait.completed }}" then: - alias: "Play (avoids long delay)" - service: media_player.media_play + action: media_player.media_play target: entity_id: "{{ player.entity_id }}" - alias: "Check if resume_action_false group exists" @@ -1029,7 +1033,7 @@ script: {{ integration_entities('group') | select('search', 'resume_action_false_ghresume_' ~ context.id | lower) | list | count > 0 }} then: - alias: "Remove entity + members from group" - service: group.set + action: group.set data: object_id: resume_action_false_ghresume_{{ context.id | lower }} name: "Entities with actions which should not be resumed" @@ -1038,7 +1042,7 @@ script: {%- set current = state_attr('group.resume_action_false_ghresume' ~ context.id | lower, 'entity_id') %} {{ iif(current == none, [], current) | reject('in', [player.entity_id] + player.members) | list }} - alias: "Remove entity + members from group" - service: group.set + action: group.set data: object_id: resume_active_ghresume_{{ context.id | lower }} name: "Entities which will be resumed by the Google Home Resume script" @@ -1048,7 +1052,7 @@ script: {%- set current = state_attr(g, 'entity_id') %} {{ iif(current == none, [], current) | reject('in', [player.entity_id] + player.members) | list }} - alias: "Remove entity + members from group" - service: group.set + action: group.set data: object_id: resume_script_active_ghresume_{{ context.id | lower }} name: "Entities currently active with Google Home Resume script" @@ -1076,12 +1080,12 @@ script: event_type: call_service event_data: domain: media_player - service: play_media + action: play_media service_data: media_content_type: music entity_id: "{{ [ dummy_player ] }}" - alias: "Send TTS message with picture" - service: media_player.play_media + action: media_player.play_media target: "{{ target }}" data: media_content_id: "{{ wait.trigger.event.data.service_data.media_content_id }}" @@ -1098,7 +1102,7 @@ script: # The Google Home Voice script to send an action to a Google Home on which you just asked a question # for more details: https://community.home-assistant.io/t/script-to-send-tts-to-the-right-google-home-based-on-voice-commands/346885 google_home_voice: - alias: "00 - Google Home Voice" + alias: "Google Home Voice" icon: mdi:cast-audio mode: single max_exceeded: silent @@ -1113,7 +1117,7 @@ script: if: "{{ integration_entities('spotify') | count > 0 }}" then: - alias: "Update Spotify entities" - service: homeassistant.update_entity + action: homeassistant.update_entity target: entity_id: "{{ integration_entities('spotify') }}" - alias: "Set variables for service calls" @@ -1135,7 +1139,7 @@ script: | count > 0 }} then: - - service: ytube_music_player.call_method + - action: ytube_music_player.call_method data: entity_id: > {{ @@ -1173,30 +1177,30 @@ script: {{ [ target ] }} {%- endif %} - alias: "Stop ambient sound" - service: media_player.media_stop + action: media_player.media_stop target: entity_id: "{{ target }}" - alias: "Wrong actions provided" choose: - - conditions: "{{ service_calls | count == 0 }}" + - conditions: "{{ actions | count == 0 }}" sequence: - - stop: "There were no service calls defined, no actions are performed. The script script has been aborted." + - stop: "There were no actions defined, no items are performed. The script script has been aborted." error: true - - conditions: "{{ no_service_count > 0 }}" + - conditions: "{{ no_action_count > 0 }}" sequence: - - service: system_log.write + - action: system_log.write data: level: "warning" logger: "{{ this.entity_id }}" message: > - {{ no_service_count }} out of {{ service_calls | count }} actions were not performed because they are not service calls. + {{ no_action_count }} out of {{ actions | count }} actions were not performed because they are not service calls. - alias: "Wait until plaer is idle" wait_template: "{{ is_state(target, 'idle') }}" - alias: "Use resume script or perform actions" if: "{{ use_resume if use_resume is defined else true }}" then: - alias: "Send actions using Google Home Resume script" - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: @@ -1205,7 +1209,7 @@ script: player_data: "{{ player_data }}" action: > {%- set ns = namespace(action=[]) %} - {%- for item in service_calls %} + {%- for item in actions %} {%- set data = item.get('data', {}) %} {%- set extra = item.get('extra', {}) %} {%- set service = item.service %} @@ -1256,7 +1260,7 @@ script: - alias: "Variables for specific media_player" variables: player: "{{ player_data | selectattr('entity_id', 'eq', repeat.item) | join }}" - - service: script.turn_on + - action: script.turn_on target: entity_id: script.google_home_resume_helper data: @@ -1272,7 +1276,7 @@ script: else: - alias: "Perform service calls defined in action" repeat: - for_each: "{{ service_calls }}" + for_each: "{{ actions }}" sequence: - variables: data: > @@ -1292,14 +1296,14 @@ script: {%- else %} {{ data }} {%- endif %} - - service: "{{ repeat.item.service }}" + - action: "{{ repeat.item.service }}" target: "{{ repeat.item.get('target', { 'entity_id': target_list}) }}" data: "{{ data }}" - alias: "Volume defined" if: "{{ volume is defined and iif(volume) }}" then: - alias: "Apply volume" - service: media_player.volume_set + action: media_player.volume_set target: entity_id: "{{ target_list }}" data: @@ -1310,7 +1314,7 @@ script: # Example: you can stop the media_players when you leave home, and resume them when you return # for more details: https://github.com/TheFes/HA-configuration/blob/main/include/integrations/packages/google_cast/docs/google_home_event.md google_home_event: - alias: "00 - Google Home Event" + alias: "Google Home Event" description: Script for resuming Google Home speakers icon: mdi:cast-audio mode: parallel @@ -1422,7 +1426,7 @@ script: if: "{{ integration_entities('spotify') | count > 0 }}" then: - alias: "Update Spotify entities" - service: homeassistant.update_entity + action: homeassistant.update_entity target: entity_id: "{{ integration_entities('spotify') }}" - variables: @@ -1447,7 +1451,7 @@ script: | count > 0 }} then: - - service: ytube_music_player.call_method + - action: ytube_music_player.call_method data: entity_id: > {{ player_data @@ -1484,7 +1488,7 @@ script: {%- else %} [] {%- endif %} - - service: media_player.turn_off + - action: media_player.turn_off target: entity_id: "{{ (integration_entities('cast') | select('in', target_list) | list + also_stop_list) | unique | list }}" - conditions: "{{ resume if resume is defined else false }}" @@ -1508,7 +1512,7 @@ script: if: "{{ players_to_resume | count > 0 }}" then: - alias: "Resume players" - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: @@ -1541,7 +1545,7 @@ template: # Automation for automatic resume after specific service calls automation: - - alias: 00 - Google Home Automatic Resume + - alias: Google Home Automatic Resume id: 417395bc-bd64-40a3-b20a-9062d426a01f mode: parallel max_exceeded: silent @@ -1551,7 +1555,7 @@ automation: event_type: call_service event_data: domain: media_player - service: play_media + action: play_media variables: announce_volume: "{{ state_attr('script.google_home_resume', 'settings').get('announce_volume_automation') }}" id: play @@ -1560,7 +1564,7 @@ automation: event_type: call_service event_data: domain: cast - service: show_lovelace_view + action: show_lovelace_view id: cast variables: version: *ver @@ -1603,7 +1607,7 @@ automation: for_each: "{{ groups_existing }}" sequence: - alias: "Remove one group" - service: group.remove + action: group.remove data: object_id: "{{ states[repeat.item].object_id }}" - alias: "Check if target is already being resumed on in a group which is already being resumed" @@ -1629,7 +1633,7 @@ automation: if: "{{ integration_entities('spotify') | count > 0 }}" then: - alias: Update Spotify entities - service: homeassistant.update_entity + action: homeassistant.update_entity target: entity_id: "{{ integration_entities('spotify') }}" - alias: Set data_source and determine target list @@ -1647,7 +1651,7 @@ automation: if: > {{ player_data | selectattr('entity_id', 'in', target_list) | selectattr('yt_player', 'defined') | list | count > 0 }} then: - - service: ytube_music_player.call_method + - action: ytube_music_player.call_method data: entity_id: > {{ player_data | selectattr('entity_id', 'in', target_list) | selectattr('yt_player', 'defined') | map(attribute='yt_player') | list }} @@ -1656,7 +1660,7 @@ automation: then: - wait_template: "{{ is_state(entities|first, 'idle') }}" - alias: Set volume for announcement - service: media_player.volume_set + action: media_player.volume_set target: entity_id: "{{ entities }}" data: @@ -1667,7 +1671,7 @@ automation: condition: template value_template: *group_check - alias: Send actions using Google Home Resume script - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: From 3ab73bdfe769e1e3c36a82f87fe3a14a45301f7a Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Sat, 17 Aug 2024 08:42:39 +0000 Subject: [PATCH 2/7] changed action back to service for backwards compatability --- google_home_resume.yaml | 106 ++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/google_home_resume.yaml b/google_home_resume.yaml index 0fbbe60..aabaa6f 100755 --- a/google_home_resume.yaml +++ b/google_home_resume.yaml @@ -89,7 +89,7 @@ script: device: integration: cast action: - description: "Actions to be performed (only those using action: will work)" + description: "Actions to be performed (only those using action: or service: will work)" required: true selector: action: @@ -128,7 +128,7 @@ script: for_each: "{{ groups_existing }}" sequence: - alias: "Remove one group" - action: group.remove + service: group.remove data: object_id: "{{ states[repeat.item].object_id }}" - alias: "Set based on other scripts" @@ -153,7 +153,7 @@ script: error: true - conditions: "{{ not (event_script or automation) and no_action_count > 0 }}" sequence: - - action: system_log.write + - service: system_log.write data: level: "warning" logger: "{{ this.entity_id }}" @@ -189,7 +189,7 @@ script: if: "{{ spotify_entities | count > 0 }}" then: - alias: "Update Spotify entities" - action: homeassistant.update_entity + service: homeassistant.update_entity target: entity_id: "{{ spotify_entities }}" - alias: "Store data of media player entities and determine target_list" @@ -435,7 +435,7 @@ script: - alias: "Send log message about wrong wrong primary_spotcast" if: "{{ not primary_spotcast_check }}" then: - - action: system_log.write + - service: system_log.write data: level: "warning" logger: "{{ this.entity_id }}" @@ -465,7 +465,7 @@ script: | list }} - alias: "Create group with entities for which the script is active" - action: group.set + service: group.set data: object_id: resume_script_active_ghresume_{{ context.id | lower }} name: "Entities currently active with Google Home Resume script" @@ -478,7 +478,7 @@ script: if: "{{ iif(resume_this_action is defined and resume_this_action,false,true) }}" then: - alias: "Create group entities with resume_this_action to false" - action: group.set + service: group.set data: object_id: resume_action_false_ghresume_{{ context.id | lower }} name: "Entities with actions which should not be resumed" @@ -492,7 +492,7 @@ script: {{ target_no_volume | count > 0 }} then: - alias: "Turn non playing entities on" - action: media_player.turn_on + service: media_player.turn_on continue_on_error: true target: entity_id: "{{ target_no_volume }}" @@ -529,7 +529,7 @@ script: | count > 0 }} then: - - action: ytube_music_player.call_method + - service: ytube_music_player.call_method data: entity_id: > {{ @@ -619,7 +619,7 @@ script: - if: "{{ settings.get('stop_before_action', false) }}" then: - alias: "Stop all targets which are playing" - action: media_player.media_stop + service: media_player.media_stop target: entity_id: "{{ expand(media_entities) | selectattr('state', 'eq', 'playing') | map(attribute='entity_id') | list }}" - alias: "Wait until they are all stopped" @@ -629,7 +629,7 @@ script: - conditions: "{{ volume_set }}" sequence: - alias: "Apply TTS volume" - action: media_player.volume_set + service: media_player.volume_set target: entity_id: > {{ @@ -647,7 +647,7 @@ script: - conditions: "{{ tts and screen }}" sequence: - alias: "Call TTS for screen script" - action: script.turn_on + service: script.turn_on target: entity_id: script.google_home_resume_helper data: @@ -659,7 +659,7 @@ script: picture_url: "{{ extra.screen_tts.get('picture_url', '') }}" action_type: tts continue_on_error: true - - action: "{{ service_call.get('action') or service_call.get('service') }}" + - service: "{{ service_call.get('action') or service_call.get('service') }}" target: > {%- if screen and tts %} {{ dict(entity_id = settings.dummy_player) }} @@ -699,7 +699,7 @@ script: variables: player: "{{ player_data | selectattr('entity_id', 'eq', repeat.item) | join }}" - alias: "Start perform resume script" - action: script.turn_on + service: script.turn_on target: entity_id: script.google_home_resume_helper data: @@ -733,7 +733,7 @@ script: for_each: "{{ groups_to_remove }}" sequence: - alias: "Remove group" - action: group.remove + service: group.remove data: object_id: > {{ states[repeat.item].object_id }} @@ -781,7 +781,7 @@ script: current_volume: "{{ state_attr(player.entity_id, 'volume_level') | round(2, default=0.0) }}" data_volume: "{{ player.get('volume_level', 0.0) | round(2) }}" - alias: "Add entity to the restore active group" - action: group.set + service: group.set data: object_id: resume_active_ghresume_{{ context.id | lower }} name: "Entities which will be resumed by the Google Home Resume script" @@ -816,7 +816,7 @@ script: for_each: "{{ player.members if player.type == 'group' else [ player.entity_id ] }}" sequence: - alias: "Set volume back to old state" - action: media_player.volume_set + service: media_player.volume_set target: entity_id: "{{ repeat.item }}" data: @@ -830,7 +830,7 @@ script: if: "{{ player.type == 'screen' and not resume }}" then: - alias: "Turn Google Home off to return to idle mode (photo display)" - action: media_player.turn_off + service: media_player.turn_off target: entity_id: "{{ player.entity_id }}" - alias: "Resume needed?" @@ -860,13 +860,13 @@ script: conditions: "{{ bbc }}" sequence: - alias: "Play video" - action: media_player.play_media + service: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: media_content_type: cast media_content_id: '{{ dict(app_name="youtube", media_id=player.media_content_id) | to_json }}' - - action: media_player.play_media + - service: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -894,7 +894,7 @@ script: if: "{{ wait.completed }}" then: &seek - alias: "Seek" - action: media_player.media_seek + service: media_player.media_seek target: entity_id: "{{ player.entity_id }}" data: @@ -905,7 +905,7 @@ script: sequence: - variables: player_ytube: "{{ player.yt_player }}" - - action: ytube_music_player.call_method + - service: ytube_music_player.call_method data: entity_id: "{{ player_ytube }}" command: interrupt_resume @@ -920,7 +920,7 @@ script: conditions: "{{ player.type == 'screen' and youtube }}" sequence: - alias: "Play video" - action: media_player.play_media + service: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -946,7 +946,7 @@ script: account_data: account: "{{ player.spotcast }}" - alias: "Resume spotify" - action: spotcast.start + service: spotcast.start data: > {{ service_data if player.spotcast == 'primary_account' else dict(service_data, **account_data) }} - alias: "Wait until song is playing" @@ -958,7 +958,7 @@ script: then: *seek else: - alias: "Resume spotify" - action: spotcast.start + service: spotcast.start data: > {{ service_data if player.spotcast == 'primary_account' else dict(service_data, **account_data) }} - alias: "Wait until song is playing" @@ -981,7 +981,7 @@ script: images: - url: "{{ picture_url }}" - alias: "Resume stream" - action: media_player.play_media + service: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -1009,7 +1009,7 @@ script: images: - url: "{{ picture_url }}" - alias: "Resume stream" - action: media_player.play_media + service: media_player.play_media target: entity_id: "{{ player.entity_id }}" data: @@ -1025,7 +1025,7 @@ script: if: "{{ wait.completed }}" then: - alias: "Play (avoids long delay)" - action: media_player.media_play + service: media_player.media_play target: entity_id: "{{ player.entity_id }}" - alias: "Check if resume_action_false group exists" @@ -1033,7 +1033,7 @@ script: {{ integration_entities('group') | select('search', 'resume_action_false_ghresume_' ~ context.id | lower) | list | count > 0 }} then: - alias: "Remove entity + members from group" - action: group.set + service: group.set data: object_id: resume_action_false_ghresume_{{ context.id | lower }} name: "Entities with actions which should not be resumed" @@ -1042,7 +1042,7 @@ script: {%- set current = state_attr('group.resume_action_false_ghresume' ~ context.id | lower, 'entity_id') %} {{ iif(current == none, [], current) | reject('in', [player.entity_id] + player.members) | list }} - alias: "Remove entity + members from group" - action: group.set + service: group.set data: object_id: resume_active_ghresume_{{ context.id | lower }} name: "Entities which will be resumed by the Google Home Resume script" @@ -1052,7 +1052,7 @@ script: {%- set current = state_attr(g, 'entity_id') %} {{ iif(current == none, [], current) | reject('in', [player.entity_id] + player.members) | list }} - alias: "Remove entity + members from group" - action: group.set + service: group.set data: object_id: resume_script_active_ghresume_{{ context.id | lower }} name: "Entities currently active with Google Home Resume script" @@ -1080,12 +1080,12 @@ script: event_type: call_service event_data: domain: media_player - action: play_media + service: play_media service_data: media_content_type: music entity_id: "{{ [ dummy_player ] }}" - alias: "Send TTS message with picture" - action: media_player.play_media + service: media_player.play_media target: "{{ target }}" data: media_content_id: "{{ wait.trigger.event.data.service_data.media_content_id }}" @@ -1117,7 +1117,7 @@ script: if: "{{ integration_entities('spotify') | count > 0 }}" then: - alias: "Update Spotify entities" - action: homeassistant.update_entity + service: homeassistant.update_entity target: entity_id: "{{ integration_entities('spotify') }}" - alias: "Set variables for service calls" @@ -1139,7 +1139,7 @@ script: | count > 0 }} then: - - action: ytube_music_player.call_method + - service: ytube_music_player.call_method data: entity_id: > {{ @@ -1177,7 +1177,7 @@ script: {{ [ target ] }} {%- endif %} - alias: "Stop ambient sound" - action: media_player.media_stop + service: media_player.media_stop target: entity_id: "{{ target }}" - alias: "Wrong actions provided" @@ -1188,7 +1188,7 @@ script: error: true - conditions: "{{ no_action_count > 0 }}" sequence: - - action: system_log.write + - service: system_log.write data: level: "warning" logger: "{{ this.entity_id }}" @@ -1200,14 +1200,14 @@ script: if: "{{ use_resume if use_resume is defined else true }}" then: - alias: "Send actions using Google Home Resume script" - action: script.turn_on + service: script.turn_on target: entity_id: script.google_home_resume data: variables: target_list: "{{ target_list }}" player_data: "{{ player_data }}" - action: > + service: > {%- set ns = namespace(action=[]) %} {%- for item in actions %} {%- set data = item.get('data', {}) %} @@ -1260,7 +1260,7 @@ script: - alias: "Variables for specific media_player" variables: player: "{{ player_data | selectattr('entity_id', 'eq', repeat.item) | join }}" - - action: script.turn_on + - service: script.turn_on target: entity_id: script.google_home_resume_helper data: @@ -1296,14 +1296,14 @@ script: {%- else %} {{ data }} {%- endif %} - - action: "{{ repeat.item.service }}" + - service: "{{ repeat.item.get('action') or repeat.item.get('service') }}" target: "{{ repeat.item.get('target', { 'entity_id': target_list}) }}" data: "{{ data }}" - alias: "Volume defined" if: "{{ volume is defined and iif(volume) }}" then: - alias: "Apply volume" - action: media_player.volume_set + service: media_player.volume_set target: entity_id: "{{ target_list }}" data: @@ -1426,7 +1426,7 @@ script: if: "{{ integration_entities('spotify') | count > 0 }}" then: - alias: "Update Spotify entities" - action: homeassistant.update_entity + service: homeassistant.update_entity target: entity_id: "{{ integration_entities('spotify') }}" - variables: @@ -1451,7 +1451,7 @@ script: | count > 0 }} then: - - action: ytube_music_player.call_method + - service: ytube_music_player.call_method data: entity_id: > {{ player_data @@ -1488,7 +1488,7 @@ script: {%- else %} [] {%- endif %} - - action: media_player.turn_off + - service: media_player.turn_off target: entity_id: "{{ (integration_entities('cast') | select('in', target_list) | list + also_stop_list) | unique | list }}" - conditions: "{{ resume if resume is defined else false }}" @@ -1512,7 +1512,7 @@ script: if: "{{ players_to_resume | count > 0 }}" then: - alias: "Resume players" - action: script.turn_on + service: script.turn_on target: entity_id: script.google_home_resume data: @@ -1555,7 +1555,7 @@ automation: event_type: call_service event_data: domain: media_player - action: play_media + service: play_media variables: announce_volume: "{{ state_attr('script.google_home_resume', 'settings').get('announce_volume_automation') }}" id: play @@ -1564,7 +1564,7 @@ automation: event_type: call_service event_data: domain: cast - action: show_lovelace_view + service: show_lovelace_view id: cast variables: version: *ver @@ -1607,7 +1607,7 @@ automation: for_each: "{{ groups_existing }}" sequence: - alias: "Remove one group" - action: group.remove + service: group.remove data: object_id: "{{ states[repeat.item].object_id }}" - alias: "Check if target is already being resumed on in a group which is already being resumed" @@ -1633,7 +1633,7 @@ automation: if: "{{ integration_entities('spotify') | count > 0 }}" then: - alias: Update Spotify entities - action: homeassistant.update_entity + service: homeassistant.update_entity target: entity_id: "{{ integration_entities('spotify') }}" - alias: Set data_source and determine target list @@ -1651,7 +1651,7 @@ automation: if: > {{ player_data | selectattr('entity_id', 'in', target_list) | selectattr('yt_player', 'defined') | list | count > 0 }} then: - - action: ytube_music_player.call_method + - service: ytube_music_player.call_method data: entity_id: > {{ player_data | selectattr('entity_id', 'in', target_list) | selectattr('yt_player', 'defined') | map(attribute='yt_player') | list }} @@ -1660,7 +1660,7 @@ automation: then: - wait_template: "{{ is_state(entities|first, 'idle') }}" - alias: Set volume for announcement - action: media_player.volume_set + service: media_player.volume_set target: entity_id: "{{ entities }}" data: @@ -1671,7 +1671,7 @@ automation: condition: template value_template: *group_check - alias: Send actions using Google Home Resume script - action: script.turn_on + service: script.turn_on target: entity_id: script.google_home_resume data: From e7bd5eb1336c064109d92eefde4c115ad6282e5f Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Sat, 17 Aug 2024 08:54:24 +0000 Subject: [PATCH 3/7] update versioni number to b1 --- google_home_resume.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google_home_resume.yaml b/google_home_resume.yaml index aabaa6f..64de711 100755 --- a/google_home_resume.yaml +++ b/google_home_resume.yaml @@ -108,7 +108,7 @@ script: sequence: - alias: "Version number" variables: &version - version: &ver 2024.8.0b0 + version: &ver 2024.8.0b1 settings: "{{ settings | default({}, true) }}" - alias: "Failsafe to remove all groups if the script was not running" if: > From 6e15884ca9596e05d819d867631debaa0a72d568 Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Mon, 19 Aug 2024 14:44:42 +0000 Subject: [PATCH 4/7] Only stop media if there was something playing --- google_home_resume.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/google_home_resume.yaml b/google_home_resume.yaml index 64de711..8b413cb 100755 --- a/google_home_resume.yaml +++ b/google_home_resume.yaml @@ -616,7 +616,11 @@ script: }} data: > {{ dict(service_call.get('data', {}).items() | rejectattr('0', 'in', ['entity_id', 'area_id', 'device_id', 'script_extra']) | list) }} - - if: "{{ settings.get('stop_before_action', false) }}" + - if: > + {{ + settings.get('stop_before_action', false) + and expand(media_entities) | selectattr('state', 'eq', 'playing') | map(attribute='entity_id') | list | count > 0 + }} then: - alias: "Stop all targets which are playing" service: media_player.media_stop From f27d53f55cc7b2e182f0b1df4f48c7b66cfeb1ff Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Fri, 23 Aug 2024 14:14:13 +0200 Subject: [PATCH 5/7] update version number --- google_home_resume.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google_home_resume.yaml b/google_home_resume.yaml index 8b413cb..c0f7318 100755 --- a/google_home_resume.yaml +++ b/google_home_resume.yaml @@ -108,7 +108,7 @@ script: sequence: - alias: "Version number" variables: &version - version: &ver 2024.8.0b1 + version: &ver 2024.8.0 settings: "{{ settings | default({}, true) }}" - alias: "Failsafe to remove all groups if the script was not running" if: > From 0d5f982fd7d53ae816051c900851338060b4b13a Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Fri, 23 Aug 2024 12:17:32 +0000 Subject: [PATCH 6/7] Use action instead of service in examples --- docs/examples/examples_google_home_resume.md | 28 ++++++++++---------- docs/google_home_event.md | 4 +-- docs/google_home_voice.md | 4 +-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/examples/examples_google_home_resume.md b/docs/examples/examples_google_home_resume.md index 0cd2e18..e4c55f1 100644 --- a/docs/examples/examples_google_home_resume.md +++ b/docs/examples/examples_google_home_resume.md @@ -23,10 +23,10 @@ This example will play an mp3 on 2 targets, without additional data ```yaml alias: "Play sound when there is someone at the door" -service: script.google_home_resume +action: script.google_home_resume data: action: - - service: media_player.play_media + - action: media_player.play_media target: area_id: 'living_room' entity_id: @@ -45,7 +45,7 @@ In this example a script is started instead of a service call directly to the en ```yaml alias: "Play sound when there is someone at the door via script" -service: script.google_home_resume +action: script.google_home_resume data: target: area_id: 'living_room' @@ -53,7 +53,7 @@ data: - media_player.bedroom - media_player.guestroom action: - - service: script.play_sound + - action: script.play_sound data: file: "media-source://media_source/local/dingdong.mp3" ``` @@ -66,14 +66,14 @@ Enter the data for the TTS service call, and provide the volume under `extra` ```yaml - alias: "Send TTS using Google Home Resume script" - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: variables: action: - alias: "Send TTS message" - service: tts.google_cloud_say + action: tts.google_cloud_say target: entity_id: media_player.living_mini data: @@ -95,14 +95,14 @@ The `picture_url` should be a full url (starting with `http://` or `https://`, n ```yaml - alias: "Send TTS with picture and " - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: variables: action: - alias: "Send TTS message" - service: tts.google_cloud_say + action: tts.google_cloud_say target: entity_id: - media_player.living_mini @@ -123,14 +123,14 @@ ___ ```yaml - alias: "Send TTS with picture and " - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: variables: action: - alias: "Send TTS message" - service: tts.google_cloud_say + action: tts.google_cloud_say target: entity_id: - media_player.living_mini @@ -154,14 +154,14 @@ Don't put `wait: true` at the end of the last service_call, this will block the ```yaml - alias: "Send multiple TTS to the same target" - service: script.turn_on + action: script.turn_on target: entity_id: script.google_home_resume data: variables: action: - alias: "First TTS message" - service: tts.google_cloud_say + action: tts.google_cloud_say target: entity_id: media_player.living_mini data: @@ -169,7 +169,7 @@ Don't put `wait: true` at the end of the last service_call, this will block the extra: wait: true - alias: "Second message" - service: tts.google_cloud_say + action: tts.google_cloud_say target: entity_id: media_player.living_mini data: @@ -177,7 +177,7 @@ Don't put `wait: true` at the end of the last service_call, this will block the extra: wait: true - alias: "Third and last message" - service: tts.google_cloud_say + action: tts.google_cloud_say target: entity_id: media_player.living_mini data: diff --git a/docs/google_home_event.md b/docs/google_home_event.md index 8ed9f19..c821006 100644 --- a/docs/google_home_event.md +++ b/docs/google_home_event.md @@ -64,7 +64,7 @@ action: - choose: - conditions: "{{ trigger.id == 'leaving' }}" sequence: - - service: script.google_home_event + - action: script.google_home_event data: target: area_id: 'living_room' @@ -72,7 +72,7 @@ action: resume_id: resume_when_home - conditions: "{{ trigger.id == 'arriving' }}" sequence: - - service: script.google_home_event + - action: script.google_home_event data: target: area_id: 'living_room' diff --git a/docs/google_home_voice.md b/docs/google_home_voice.md index 1ecaecf..0ae14fa 100644 --- a/docs/google_home_voice.md +++ b/docs/google_home_voice.md @@ -90,14 +90,14 @@ eta_thefes: {%- set eta = (as_timestamp(now()) + 60 * states ('sensor.thefes_home') | float(0) | timestamp_custom('%H:%M') %} If TheFes leaves now, he will be home at {{ eta }}. - alias: "TTS for speaker voice command" - service: script.google_home_voice + action: script.google_home_voice data: target_conversion: media_player.kitchen_hub: media_player.livinghome_group use_resume: true action: - alias: "Send TTS message" - service: tts.google_cloud_say + action: tts.google_cloud_say data: message: "{{ message }}" volume: 35 From 4e05fd2ec794fd1fcfa667f58046ef3acb843fac Mon Sep 17 00:00:00 2001 From: Martijn van der Pol Date: Fri, 23 Aug 2024 12:23:58 +0000 Subject: [PATCH 7/7] add stop_before_action to readme and set to false --- google_home_resume.yaml | 2 +- readme.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/google_home_resume.yaml b/google_home_resume.yaml index c0f7318..2cee912 100755 --- a/google_home_resume.yaml +++ b/google_home_resume.yaml @@ -54,7 +54,7 @@ homeassistant: seconds: 20 max_runtime: minutes: 10 - stop_before_action: true + stop_before_action: false # settings for automation enable_automation: false dashboard_cast: true diff --git a/readme.md b/readme.md index d50a341..2d99ba0 100644 --- a/readme.md +++ b/readme.md @@ -147,7 +147,8 @@ There are no required settings, but if you use Google Home speaker groups you sh |dummy_player||`media_player.vlc_telnet`|The dummy media_player used for the TTS with picture and text feature |default_resume_delay|20 seconds|`20`|The delay after which the resume will started when it was interrupted by sending an image. Follows the syntax of [delay](https://www.home-assistant.io/docs/scripts/#wait-for-time-to-pass-delay), so also `"00:00:20"` or `seconds: 20` can be used. |max_runtime||"00:10:00"|The maximum duration the Resume script should run, if this time is reached the script will be stopped. This avoids entities being logged in the groups, which will stop them from being resumed on a new disruption. Follows the syntax of [delay](https://www.home-assistant.io/docs/scripts/#wait-for-time-to-pass-delay), so also `600` or `minutes: 10` can be used. -|automation_enabled|`true`|`true`|If the automation for automatic resume should be used or not +|stop_before_action|`false`|`true`|If media_players should be stopped before the actions provided in the script data are performed. +|automation_enabled|`false`|`true`|If the automation for automatic resume should be used or not |dashboard_cast|`false`|`false`|If the automation should be used in case a HA Dasboard is cast to the device |announce_volume_automation||`0.75`|The volume used for announcements in the automation, remove or leave empty to leave the volume as it is.