Skip to content

Instantly share code, notes, and snippets.

@Romanmir
Forked from mdegat01/auto_check_config.yaml
Created October 19, 2022 22:20
Show Gist options
  • Save Romanmir/b2375e57ffdaf6283541005374891904 to your computer and use it in GitHub Desktop.
Save Romanmir/b2375e57ffdaf6283541005374891904 to your computer and use it in GitHub Desktop.
Auto Check Config package
automation:
id: cb7d0e035b94403db6b08f7263584b51_config_check_update
alias: Run config check on change
description: Run a config check when config is changed and reload automations when blueprints change
max_exceeded: silent
trigger:
- id: file_change
platform: event
event_type: folder_watcher
- id: reload
platform: event
event_type: notification_action
event_data:
data:
tag: invalid_config
variables:
_anchor_valid_config_data: &valid-config-data
title: HA configuration valid
data:
tag: invalid_config
notification_icon: mdi:check-bold
channel: Info
group: Info
url: /developer-tools/yaml
ttl: 0
actions:
- action: reload-automations
title: πŸ”„ Automations
authenticationRequired: true
- action: reload-scripts
title: πŸ”„ Scripts
authenticationRequired: true
- action: reload-templates
title: πŸ”„ Templates
authenticationRequired: true
action:
choose:
- alias: Reload action selected
conditions: "{{ trigger.id == 'reload' }}"
sequence:
- alias: Update notification to acknowledge action
service: &notify-service notify.mobile_app_mikes_phone
data:
<<: *valid-config-data
message: "{{ trigger.event.data.data.action[7:] | capitalize }} reloading"
- choose:
- alias: πŸ”„ automations
conditions: "{{ trigger.event.data.data.action == 'reload-automations' }}"
sequence:
service: automation.reload
- alias: πŸ”„ scripts
conditions: "{{ trigger.event.data.data.action == 'reload-scripts' }}"
sequence:
service: script.reload
- alias: πŸ”„ templates
conditions: "{{ trigger.event.data.data.action == 'reload-templates' }}"
sequence:
service: template.reload
- alias: A YAML file changed
conditions:
- "{{ trigger.id == 'file_change' }}"
- "{{ trigger.event.data.path is search('[.]ya?ml$') }}"
sequence:
- alias: Clear invalid config notification if present
if: &invalid-config >-
{{ not not states['persistent_notification.invalid_config'] }}
then:
service: *notify-service
data:
message: clear_notification
data: &error-notification-tag
tag: invalid_config
- alias: Was a blueprint updated
if: "{{ trigger.event.data.path is search('^/config/blueprints/') }}"
then:
choose:
alias: Reload automations for automation blueprint updated
conditions: "{{ trigger.event.data.path is search('^/config/blueprints/automation/') }}"
sequence:
- service: automation.reload
- alias: Can only get here if reload failed, notify mike about error
service: *notify-service
data:
title: Automations failed to reload after blueprint update
message: *error-message >-
Trigger was {{ trigger.event.data.path }} being {{ trigger.event.data.event_type }}. Check logs for details.
data: &error-notification-data
<<: *error-notifiation-tag
icon: mdi:alert
channel: Errors
group: Errors
ttl: 0
url: /config/logs
default:
alias: Notify mike a manual config check is required
service: *notify-service
data:
title: An unexpected type of blueprint was changed
message: "{{ trigger.event.data.path }} was {{ trigger.event.data.event_type }}. A manual config check is required."
data:
tag: ha_blueprint_check
icon: mdi:alert
channel: Alerts
group: Alerts
url: /a0d7b954_vscode
ttl: 0
else:
- service: homeassistant.check_config
- alias: Did config check fail
if: *invalid-config
then:
service: *notify-service
data:
title: HA configuration invalid
message: *error-message
data: *error-notification-data
else:
alias: Config check passed, notify all
service: *notify-service
data:
<<: *valid-config-data
message: "Trigger was {{ trigger.event.data.path }} being {{ trigger.event.data.event_type }}"
folder_watcher:
- folder: /config/packages
- folder: /config/blueprints
- folder: /config/common
homeassistant:
allowlist_external_dirs:
- /config/packages
- /config/blueprints
- /config/common
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment