Skip to content

Commit

Permalink
add error logging (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjakobi authored Dec 2, 2023
1 parent 19fcad9 commit 9b796f6
Showing 1 changed file with 58 additions and 51 deletions.
109 changes: 58 additions & 51 deletions python_scripts/sync_announcement.py
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))

0 comments on commit 9b796f6

Please sign in to comment.