Since I remember I’ve created automations in separate files but now, I would like to move them all to web UI editor. It looks mature since my home assistant beginnings and I think I can get used to it
Is there any way to migrate all of them at once? Currently, I got 195 automations, and moving them one by one looks like a real pain
Since there is no answer, I’ve created a simple converter:
import os
from fnmatch import fnmatch
import sys
import re
import time
import random
def list_yaml_files(config_dir: str) -> list:
pattern = "*.yaml"
result = list()
for path, subdirs, files in os.walk(os.path.join(config_dir, 'automations')):
for name in files:
if fnmatch(name, pattern):
result.append(os.path.join(path, name))
return result
def get_automation(filepath: str) -> str:
with open(filepath, 'r') as fp:
return fp.read()
def generate_id_key() -> str:
return str(round(time.time() * 1000) - random.randint(100, 100000000))
if __name__ == '__main__':
if len(sys.argv) != 2:
print(f'Usage: python3 {sys.argv[0]} <path to config dir>')
exit(1)
config_dir = sys.argv[1]
list_yaml_files(config_dir)
all_automations = get_automation(os.path.join(config_dir, 'automations.yaml'))
if len(all_automations) > 0:
all_automations += '\n\n'
for filepath in list_yaml_files(config_dir):
automation = get_automation(filepath)
automation = re.sub('^id: .*\n', '', automation) # remove old id when it's at the beggining
automation = re.sub('\nid: .*\n', '\n', automation) # remove old id when it isn't at the beggining
automation = "- id: '{}'\n {}".format(generate_id_key(), re.sub('\n', '\n ', automation)) # add a new id and indent
all_automations += automation + '\n\n'
with open(os.path.join(config_dir, 'automations.yaml'), 'w') as writer:
writer.write(all_automations)