-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19fcad9
commit 9b796f6
Showing
1 changed file
with
58 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,61 @@ | ||
# list of media_player entities | ||
media_players = data.get('media_players') | ||
|
||
# the message to play | ||
message = data.get('message') | ||
|
||
# use caching mechanisms | ||
# optional - true/false, default: true | ||
cache = data.get('cache') or True | ||
|
||
# specify the language for the tts model | ||
# optional - default: en-US | ||
language = data.get('language') or 'en-US' | ||
|
||
# specify a volume to set to all media_players before playing the tts message | ||
# optional - min: 0, max: 1, step: 0.01, default: None | ||
volume = data.get('volume') or None | ||
|
||
# specify if the volume should be reset to the previous state after the tts | ||
# message | ||
# optional - true/false, default: false | ||
volume_reset = data.get('volume_reset') or False | ||
|
||
# store the volume level of all media_players before running the script if | ||
# specified | ||
if volume_reset: | ||
media_player_states_before_run = {} | ||
for entity_id in media_players: | ||
media_player_states_before_run[entity_id] = ( | ||
hass.states.get(entity_id).attributes['volume_level'] | ||
) | ||
|
||
# set volume to all media_players if specified | ||
if volume is not None: | ||
hass.services.call('media_player', 'volume_set', { | ||
'entity_id': media_players, | ||
'volume_level': volume, | ||
}) | ||
def log_error(logger, message): | ||
logger.error(f"Sync Announcement: {message}") | ||
|
||
|
||
try: | ||
# list of media_player entities | ||
media_players = data.get('media_players') | ||
|
||
# the message to play | ||
message = data.get('message') | ||
|
||
# use caching mechanisms | ||
# optional - true/false, default: true | ||
cache = data.get('cache') or True | ||
|
||
# specify the language for the tts model | ||
# optional - default: en-US | ||
language = data.get('language') or 'en-US' | ||
|
||
# send message through tts.cloud_say | ||
hass.services.call('tts', 'cloud_say', { | ||
'entity_id': media_players, | ||
'message': message, | ||
'cache': cache, | ||
'language': language, | ||
}) | ||
|
||
# set respective previous volume to all media_players if specified | ||
if volume_reset: | ||
for entity_id, previous_volume in media_player_states_before_run.items(): | ||
# specify a volume to set to all media_players before playing the message | ||
# optional - min: 0, max: 1, step: 0.01, default: None | ||
volume = data.get('volume') or None | ||
|
||
# specify if the volume should be reset to the previous state after the | ||
# tts message | ||
# optional - true/false, default: false | ||
volume_reset = data.get('volume_reset') or False | ||
|
||
# store the volume level of all media_players before running the script if | ||
# specified | ||
if volume_reset: | ||
media_player_states_before_run = {} | ||
for entity_id in media_players: | ||
media_player_states_before_run[entity_id] = ( | ||
hass.states.get(entity_id).attributes['volume_level'] | ||
) | ||
|
||
# set volume to all media_players if specified | ||
if volume is not None: | ||
hass.services.call('media_player', 'volume_set', { | ||
'entity_id': entity_id, | ||
'volume_level': previous_volume, | ||
'entity_id': media_players, | ||
'volume_level': volume, | ||
}) | ||
|
||
# send message through tts.cloud_say | ||
hass.services.call('tts', 'cloud_say', { | ||
'entity_id': media_players, | ||
'message': message, | ||
'cache': cache, | ||
'language': language, | ||
}) | ||
|
||
# set respective previous volume to all media_players if specified | ||
if volume_reset: | ||
for entity_id, prev_volume in media_player_states_before_run.items(): | ||
hass.services.call('media_player', 'volume_set', { | ||
'entity_id': entity_id, | ||
'volume_level': prev_volume, | ||
}) | ||
except Exception as e: | ||
log_error("**An unhandled error has occurred.**\n\n{}".format(e)) |