From 2f472ab98080c9e4f55b400db5ec902c1e95c44d Mon Sep 17 00:00:00 2001 From: Glenn Carremans Date: Thu, 24 Oct 2024 22:01:47 +0200 Subject: [PATCH 1/2] Parse presets.json --- .editorconfig | 16 ++++++++++++++++ .gitignore | 2 ++ convert-v2.py | 39 +++++++++++++++++++++++++++++++++++++++ convert.py | 8 ++++---- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 convert-v2.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8d9614e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +max_line_length = 120 +tab_width = 4 + +[*.py] +indent_size = 2 + +[{*.yaml,*.yml}] +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..260867b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/scenes.json +/presets.json diff --git a/convert-v2.py b/convert-v2.py new file mode 100644 index 0000000..55d359a --- /dev/null +++ b/convert-v2.py @@ -0,0 +1,39 @@ +import json + +f = open("presets.json") +obj = json.load(f) + +d = {} +l = [] + +categories = obj["categories"] + +for preset in obj["presets"]: + set_name = None + for category in categories: + if preset["categoryId"] == category["id"] and category["id"] != "6e102277-814f-47c7-a054-f4a31cae30bf": + set_name = category["name"] + break + + if set_name is None: + print(f"Warning: No category found for preset {preset['name']}") + continue + + scene_name = preset["name"] + bri = preset["bri"] + lights = [[l["x"], l["y"]] for l in preset["lights"]] + s = {"c": lights, "b": round(bri / 255, 3), "s": set_name} + d[scene_name] = s + l.append(set_name + ": " + scene_name) + +sorted_json = {k: d[k] for k in sorted(d)} +with open('output.json', 'w') as f: + # Need to add Colorloop and Random manually + json.dump(sorted_json, f, separators=(',', ':'), ensure_ascii=False) + +l.sort() +l.append("Special: Colorloop") +l.append("Special: Random") + +for s in l: + print(f"- \"{s}\"") diff --git a/convert.py b/convert.py index 783cc66..f7367c5 100644 --- a/convert.py +++ b/convert.py @@ -11,14 +11,14 @@ for scene in scene_set["scenes"]: scene_name = scene["name"] bri = scene["bri"] - lights = [ [l["x"],l["y"]] for l in scene["lights"]] - s = { "c": lights, "b": round(bri/255, 3), "s": set_name } + lights = [[l["x"], l["y"]] for l in scene["lights"]] + s = {"c": lights, "b": round(bri / 255, 3), "s": set_name} d[scene_name] = s l.append(set_name + ": " + scene_name) -print(json.dumps(d, separators=(',', ':'))) +print(json.dumps(d, separators=(',', ':'), sort_keys=True)) l.sort() for s in l: - print(f"- \"{s}\"") \ No newline at end of file + print(f"- \"{s}\"") From a83d47230f8362a1518d5af9ebe4ab4b30565ff5 Mon Sep 17 00:00:00 2001 From: Glenn Carremans Date: Sun, 27 Oct 2024 20:26:08 +0100 Subject: [PATCH 2/2] Update script to latest json format --- .gitignore | 1 - convert-v2.py | 39 --------------------------------------- convert.py | 47 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 51 deletions(-) delete mode 100644 convert-v2.py diff --git a/.gitignore b/.gitignore index 260867b..0988cef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -/scenes.json /presets.json diff --git a/convert-v2.py b/convert-v2.py deleted file mode 100644 index 55d359a..0000000 --- a/convert-v2.py +++ /dev/null @@ -1,39 +0,0 @@ -import json - -f = open("presets.json") -obj = json.load(f) - -d = {} -l = [] - -categories = obj["categories"] - -for preset in obj["presets"]: - set_name = None - for category in categories: - if preset["categoryId"] == category["id"] and category["id"] != "6e102277-814f-47c7-a054-f4a31cae30bf": - set_name = category["name"] - break - - if set_name is None: - print(f"Warning: No category found for preset {preset['name']}") - continue - - scene_name = preset["name"] - bri = preset["bri"] - lights = [[l["x"], l["y"]] for l in preset["lights"]] - s = {"c": lights, "b": round(bri / 255, 3), "s": set_name} - d[scene_name] = s - l.append(set_name + ": " + scene_name) - -sorted_json = {k: d[k] for k in sorted(d)} -with open('output.json', 'w') as f: - # Need to add Colorloop and Random manually - json.dump(sorted_json, f, separators=(',', ':'), ensure_ascii=False) - -l.sort() -l.append("Special: Colorloop") -l.append("Special: Random") - -for s in l: - print(f"- \"{s}\"") diff --git a/convert.py b/convert.py index f7367c5..f0e7f07 100644 --- a/convert.py +++ b/convert.py @@ -1,24 +1,49 @@ import json -f = open("scenes.json") +f = open("presets.json") obj = json.load(f) d = {} l = [] -for scene_set in obj["sets"]: - set_name = scene_set["name"] - for scene in scene_set["scenes"]: - scene_name = scene["name"] - bri = scene["bri"] - lights = [[l["x"], l["y"]] for l in scene["lights"]] - s = {"c": lights, "b": round(bri / 255, 3), "s": set_name} - d[scene_name] = s - l.append(set_name + ": " + scene_name) +categories = obj["categories"] -print(json.dumps(d, separators=(',', ':'), sort_keys=True)) +for preset in obj["presets"]: + set_name = None + for category in categories: + if preset["categoryId"] == category["id"] and category["id"] != "6e102277-814f-47c7-a054-f4a31cae30bf": + set_name = category["name"] + break + + if set_name is None: + print(f"Warning: No category found for preset {preset['name']}") + continue + + scene_name = preset["name"] + preset_id = preset["id"] + bri = preset["bri"] + lights = [[l["x"], l["y"]] for l in preset["lights"]] + s = {"c": lights, "b": round(bri / 255, 3), "s": set_name, "n": scene_name} + d[preset_id] = s + l.append(set_name + ": " + scene_name) + +sorted_json = {k: d[k] for k in sorted(d)} +with open('output.json', 'w') as f: + for key, value in sorted_json.items(): + f.write(f'{json.dumps(value, separators=(',', ':'), ensure_ascii=False)},\n') + f.write("""{"colors":[ + [ range(hue_min|default(0),hue_max|default(360))|random,range(sat_min|default(99),sat_max|default(101))|random ], + [ range(hue_min|default(0),hue_max|default(360))|random,range(sat_min|default(99),sat_max|default(101))|random ], + [ range(hue_min|default(0),hue_max|default(360))|random,range(sat_min|default(99),sat_max|default(101))|random ], + [ range(hue_min|default(0),hue_max|default(360))|random,range(sat_min|default(99),sat_max|default(101))|random ], + [ range(hue_min|default(0),hue_max|default(360))|random,range(sat_min|default(99),sat_max|default(101))|random ], +],"s":"Special","n":"Random"}, +""") + f.write('{"colors":[],"s":"Special","n":"Colorloop"}') l.sort() +l.append("Special: Colorloop") +l.append("Special: Random") for s in l: print(f"- \"{s}\"")