From 932c0817a5eb15b2d51820c9cbe929e4aa8a21c2 Mon Sep 17 00:00:00 2001 From: L3D Date: Mon, 22 Jul 2024 14:36:03 +0200 Subject: [PATCH] Create waybar config --- defaults/main.yml | 1 + tasks/main.yml | 9 ++ tasks/waybar.yml | 36 ++++++++ templates/waybar.json.j2 | 13 ++- templates/wttr.py.j2 | 194 +++++++++++++++++++++++++++++++++++++++ vars/main.yml | 2 +- 6 files changed, 251 insertions(+), 4 deletions(-) create mode 100644 tasks/waybar.yml create mode 100644 templates/wttr.py.j2 diff --git a/defaults/main.yml b/defaults/main.yml index f42340e..2cf4e72 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -18,6 +18,7 @@ sway__keyboard_settings: true sway__keyboard_lang: 'de' sway__lock: 'swaylock --color 000000 --image /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png' sway__waybar: true +sway__waybar_modules_right: ["idle_inhibitor", "custom/weather", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "keyboard-state", "sway/language", "battery", "clock", "tray"] sway__keybindings: - keybinding: diff --git a/tasks/main.yml b/tasks/main.yml index cb73b58..3173e87 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -17,6 +17,15 @@ ansible.builtin.import_tasks: file: "config.yml" +- name: Create waybar config + ansible.builtin.include_tasks: + file: "waybar.yml" + loop: "{{ sway__user_list | flatten(1) }}" + loop_control: + loop_var: swayusr + label: "{{ swayusr.home }}/.config/waybar/" + when: sway__waybar | bool + # - name: Ensure i3 packages are installed [Archlinux} # ansible.builtin.include_tasks: # file: setup-Archlinux.yml diff --git a/tasks/waybar.yml b/tasks/waybar.yml new file mode 100644 index 0000000..7d797d3 --- /dev/null +++ b/tasks/waybar.yml @@ -0,0 +1,36 @@ +--- +- name: Create waybar config folder + become: true + ansible.builtin.file: + path: "{{ swayusr.home }}/.config/waybr/" + state: directory + mode: '0750' + owner: "{{ swayusr.user }}" + group: "{{ swayusr.user }}" + +- name: Deploy waybar weather script + become: true + ansible.builtin.template: + src: 'templates/wttr.py.j2' + dest: "{{ swayusr.home }}/.config/waybar/wttr.py" + mode: '0750' + owner: "{{ swayusr.user }}" + group: "{{ swayusr.user }}" + +- name: Deploy waybar stylesheet + become: true + ansible.builtin.template: + src: 'templates/waybar.css.j2' + dest: "{{ swayusr.home }}/.config/waybar/style.css" + mode: '0640' + owner: "{{ swayusr.user }}" + group: "{{ swayusr.user }}" + +- name: Deploy waybar config + become: true + ansible.builtin.template: + src: 'templates/waybar.json.j2' + dest: "{{ swayusr.home }}/.config/waybar/config" + mode: '0640' + owner: "{{ swayusr.user }}" + group: "{{ swayusr.user }}" diff --git a/templates/waybar.json.j2 b/templates/waybar.json.j2 index f722560..8a76f66 100644 --- a/templates/waybar.json.j2 +++ b/templates/waybar.json.j2 @@ -6,9 +6,9 @@ // "width": 1280, // Waybar width "spacing": 4, // Gaps between modules (4px) // Choose the order of the modules - "modules-left": ["sway/workspaces", "sway/mode", "sway/scratchpad", "custom/media"], + "modules-left": ["sway/workspaces", "sway/mode", "sway/scratchpad"], "modules-center": ["sway/window"], - "modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "keyboard-state", "sway/language", "battery", "clock", "tray"], + "modules-right": {{ sway__waybar_modules_right | tojson }}, // Modules configuration "sway/workspaces": { "disable-scroll": false, @@ -151,6 +151,13 @@ "escape": true, "exec": "$HOME/.config/waybar/mediaplayer.py 2> /dev/null" // Script in resources folder // "exec": "$HOME/.config/waybar/mediaplayer.py --player spotify 2> /dev/null" // Filter player based on name - } + }, + "custom/weather": { + "format": "{}", + "tooltip": true, + "interval": 900, + "exec": "{{ swayusr.home }}/.config/waybar/wttr.py", + "return-type": "json" + }, } diff --git a/templates/wttr.py.j2 b/templates/wttr.py.j2 new file mode 100644 index 0000000..9965af0 --- /dev/null +++ b/templates/wttr.py.j2 @@ -0,0 +1,194 @@ +#!/usr/bin/env python +""" +wttr.in waybar widget +{{ ansible_managed }} +""" +import json +from datetime import datetime +import requests + +localization = { + "en": { + "feels_like": "Feels like", + "wind": "Wind", + "humidity": "Humidity", + "today": "Today", + "tomorrow": "Tomorrow", + "weatherDesc": "weatherDesc", + "chanceoffog": "Fog", + "chanceoffrost": "Frost", + "chanceofovercast": "Overcast", + "chanceofrain": "Rain", + "chanceofsnow": "Snow", + "chanceofsunshine": "Sunshine", + "chanceofthunder": "Thunder", + "chanceofwindy": "Wind", + "weatherfor": "Weather for" + }, + "de": { + "feels_like": "Gefühlt wie", + "wind": "Wind", + "humidity": "Luftfeuchtigkeit", + "today": "Heute", + "tomorrow": "Morgen", + "weatherDesc": "lang_de", + "chanceoffog": "Nebel", + "chanceoffrost": "Frost", + "chanceofovercast": "Bewölkt", + "chanceofrain": "Regen", + "chanceofsnow": "Schnee", + "chanceofsunshine": "Sonne", + "chanceofthunder": "Gewitter", + "chanceofwindy": "Wind", + "weatherfor": "Wetter für" + }, + "el": { + "feels_like": "Πραγματική αίσθηση", + "wind": "Άνεμος", + "humidity": "Υγρασία", + "today": "Σήμερα", + "tomorrow": "Αύριο", + "day_after_tomorrow": "Μεθαύριο", + "weatherDesc": "lang_el", + "chanceoffog": "Ομίχλη", + "chanceoffrost": "Παγετός", + "chanceofovercast": "Πυκνή νέφωση", + "chanceofrain": "Βροχή", + "chanceofsnow": "Χιόνι", + "chanceofsunshine": "Λιακάδα", + "chanceofthunder": "Κεραυνοί", + "chanceofwindy": "Άνεμος", + "weatherfor": "Καιρός για" + } +} + +LANG = "de" +text = localization[LANG] + +WEATHER_CODES = { + '113': '☀️', + '116': '⛅️', + '119': '☁️', + '122': '☁️', + '143': '🌫', + '176': '🌦', + '179': '🌧', + '182': '🌧', + '185': '🌧', + '200': '⛈', + '227': '🌨', + '230': '❄️', + '248': '🌫', + '260': '🌫', + '263': '🌦', + '266': '🌦', + '281': '🌧', + '284': '🌧', + '293': '🌦', + '296': '🌦', + '299': '🌧', + '302': '🌧', + '305': '🌧', + '308': '🌧', + '311': '🌧', + '314': '🌧', + '317': '🌧', + '320': '🌨', + '323': '🌨', + '326': '🌨', + '329': '❄️', + '332': '❄️', + '335': '❄️', + '338': '❄️', + '350': '🌧', + '353': '🌦', + '356': '🌧', + '359': '🌧', + '362': '🌧', + '365': '🌧', + '368': '🌨', + '371': '❄️', + '374': '🌧', + '377': '🌧', + '386': '⛈', + '389': '🌩', + '392': '⛈', + '395': '❄️' +} + +data = {} + + +weather = requests.get(f"https://{LANG}.wttr.in/?format=j1", timeout=10).json() + + +def format_time(time): + """ + Make sure times get properly formated... + """ + return time.replace("00", "").zfill(2) + + +def format_temp(temp): + """ + Make sure temperature get properly formated... + """ + return (f"{temp}°").ljust(3) + +def format_chances(format_hour): + """ + Make sure weather wil be properly ranslated + """ + chances = [ + "chanceoffog", + "chanceoffrost", + "chanceofovercast", + "chanceofrain", + "chanceofsnow", + "chanceofsunshine", + "chanceofthunder", + "chanceofwindy" + ] + + probs = {text[e]: int(prb) for e, prb in format_hour.items() if e in chances and int(prb) > 0} + sorted_probs = {e: probs[e] for e in sorted(probs, key=probs.get, reverse=True)} + conditions = [f"{event} {prb}%" for event, prb in sorted_probs.items()] + return ", ".join(conditions) + +data['text'] = f"{weather['nearest_area'][0]['areaName'][0]['value']}: " +data['text'] += f"{WEATHER_CODES[weather['current_condition'][0]['weatherCode']]} " +data['text'] += f"{weather['current_condition'][0]['FeelsLikeC']}°C" + +WEATHER_DESC = text['weatherDesc'] +data['tooltip'] = f"{text['weatherfor']} " +data['tooltip'] += f"{weather['nearest_area'][0]['areaName'][0]['value']}, " +data['tooltip'] += f"{weather['nearest_area'][0]['country'][0]['value']}:\n\n" +data['tooltip'] += f"{weather['current_condition'][0][WEATHER_DESC][0]['value']}" +data['tooltip'] += f"{weather['current_condition'][0]['temp_C']}°\n" +data['tooltip'] += f"{text['feels_like']}: " +data['tooltip'] += f"{weather['current_condition'][0]['FeelsLikeC']}°\n" +data['tooltip'] += f"{text['wind']}: " +data['tooltip'] += f"{weather['current_condition'][0]['windspeedKmph']}Km/h\n" +data['tooltip'] += f"{text['humidity']}: {weather['current_condition'][0]['humidity']}%\n" +for i, day in enumerate(weather['weather']): + data['tooltip'] += "\n" + if i == 0: + data['tooltip'] += f"{text['today']}, " + if i == 1: + data['tooltip'] += f"{text['tomorrow']}, " + if i == 2 and 'day_after_tomorrow' in text: + data['tooltip'] += f"{text['day_after_tomorrow']}, " + data['tooltip'] += f"{day['date']}\n" + data['tooltip'] += f"⬆️ {day['maxtempC']}° ⬇️ {day['mintempC']}° " + data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n" + for hour in day['hourly']: + if i == 0: + if int(format_time(hour['time'])) < datetime.now().hour-2: + continue + data['tooltip'] += f"{format_time(hour['time'])} " + data['tooltip'] += f"{WEATHER_CODES[hour['weatherCode']]} " + data['tooltip'] += f"{format_temp(hour['FeelsLikeC'])} " + data['tooltip'] += f"{hour[WEATHER_DESC][0]['value']}, {format_chances(hour)}\n" + + +print(json.dumps(data)) diff --git a/vars/main.yml b/vars/main.yml index 53f6fb1..8990c3c 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,3 @@ --- -playbook_version_number: 4 # should be int +playbook_version_number: 5 # should be int playbook_version_path: 'role-roles-ansible.sway.version'