From dbcaa676aa3cb82543b223addc80de78d7f7efc5 Mon Sep 17 00:00:00 2001 From: L3D Date: Tue, 22 Aug 2023 14:53:13 +0200 Subject: [PATCH] Update wlan script --- files/i3blocks/wlan.py | 78 ++++++++++++++++++++++++++++++++++++ files/i3blocks/wlan.sh | 70 -------------------------------- tasks/config.yml | 2 +- templates/i3blocks_config.j2 | 2 +- 4 files changed, 80 insertions(+), 72 deletions(-) create mode 100644 files/i3blocks/wlan.py delete mode 100644 files/i3blocks/wlan.sh diff --git a/files/i3blocks/wlan.py b/files/i3blocks/wlan.py new file mode 100644 index 0000000..a68e3d8 --- /dev/null +++ b/files/i3blocks/wlan.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import re +import subprocess +import sys +import argparse + +def find_interface(interfaces): + for iface in interfaces: + if os.path.exists(f"/sys/class/net/{iface}"): + return iface + return None + +def get_signal_quality(interface): + try: + iw_output = subprocess.check_output(["/sbin/iw", "dev", interface, "link"], text=True) + signal_match = re.search(r'(\-\d{2}) dBm', iw_output) + return int(signal_match.group(1)) if signal_match else None + except subprocess.CalledProcessError: + return None + +def get_ssid(interface): + try: + iw_output = subprocess.check_output(["/sbin/iw", "dev", interface, "link"], text=True) + ssid_match = re.search(r'SSID: (.+)', iw_output) + return ssid_match.group(1).encode("utf-8").decode("unicode-escape") if ssid_match else None + except subprocess.CalledProcessError: + return None + +def print_color(quality): + if quality >= 70: + print("#00FF00") + elif quality >= 50: + print("#CCFF00") + elif quality >= 30: + print("#FFFF00") + elif quality >= 10: + print("#FFAA00") + else: + print("#FF0000") + +def main(): + interfaces = ["wlp3s0", "wlan0"] + parser = argparse.ArgumentParser(description="Display WiFi signal strength and SSID") + parser.add_argument("--interface", help="Specify the wireless interface") + args = parser.parse_args() + + if args.interface: + interface = args.interface + else: + interface = find_interface(interfaces) + + if not interface: + sys.exit(0) + + signal_quality = get_signal_quality(interface) + ssid = get_ssid(interface) + + if signal_quality is None: + sys.exit(1) + + # quality = int((signal_quality / 70) * 100) # Adjust the calculation as needed + quality = int(signal_quality) # Adjust the calculation as needed + + emoji_signal = "📶" + + if ssid: + print(f"{emoji_signal} {ssid} {quality}dBm") + else: + print(f"{emoji_signal} {quality}dBm") + + print_color(quality) + +if __name__ == "__main__": + main() + diff --git a/files/i3blocks/wlan.sh b/files/i3blocks/wlan.sh deleted file mode 100644 index a51b6ae..0000000 --- a/files/i3blocks/wlan.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env bash -# -# i3blocks blocklet script to display wifi signal in dBm and IP address, if WIFI is available - -# try to detect interface name - -if [[ -z "$IFACE" ]] ; then - if ( ip a s dev wlp3s0 ) > /dev/null 2>& 1 ; then - IFACE="wlp3s0" - elif ( ip a s dev wlan0 ) > /dev/null 2>& 1 ; then - IFACE="wlan0" - else - # No wlan interface found - exit 0 - fi -fi - -USE_PERCENT=${USE_PERCENT:-0} - -IW=$(which iw || echo "/sbin/iw") - -if [[ ! -x $IW ]]; then - echo "No iw binary was found on the system." 1>2 - exit 1 -fi - -while getopts p opt; do - case "$opt" in - p) USE_PERCENT=1 ;; - esac -done - -if ( ip a s dev $IFACE | grep "state DOWN" ) > /dev/null 2>& 1 ; then - echo "📶 DOWN" -fi - -dbm=$($IW dev "$IFACE" link | grep 'dBm$' | grep -Eoe '-[0-9]{2}') -ssid=$($IW dev "$IFACE" link | grep 'SSID' | cut -d ':' -f2 | sed "s/\ //g") -ipv4=$(ip -o -4 addr list "$IFACE" | awk '{print $4}') -ipv6=$(ip -o -6 addr list "$IFACE" | awk '{print $4}') - -[[ -n "$dbm" ]] || exit 1 -[[ -n "$ssid" ]] || exit 1 - -if [[ $USE_PERCENT -eq 0 ]]; then - echo "📶($ssid) $dbm"dBm - echo "📶($ssid) $dbm"dBm -else - if [[ "$dbm" -le -100 ]]; then - quality=0 - elif [[ $dbm -ge -50 ]]; then - quality=100 - else - quality=$((2 * (dbm + 100))) - fi - echo "📶($ssid) $quality%" - echo "📶($ssid) $quality%" -fi - -if [[ $dbm -ge -55 ]]; then - echo "#00FF00" -elif [[ $dbm -ge -60 ]]; then - echo "#CCFF00" -elif [[ $dbm -ge -70 ]]; then - echo "#FFFF00" -elif [[ $dbm -ge -80 ]]; then - echo "#FFAA00" -else - echo "#FF0000" -fi diff --git a/tasks/config.yml b/tasks/config.yml index 2961b57..1ae8374 100644 --- a/tasks/config.yml +++ b/tasks/config.yml @@ -69,7 +69,7 @@ group: "{{ i3wm_usr.user }}" with_items: - 'volume.sh' - - 'wlan.sh' + - 'wlan.py' - 'battery.py' - name: Create rofi config folder diff --git a/templates/i3blocks_config.j2 b/templates/i3blocks_config.j2 index 9bc2286..1e56477 100644 --- a/templates/i3blocks_config.j2 +++ b/templates/i3blocks_config.j2 @@ -18,7 +18,7 @@ signal=10 {% if i3_i3blocks_options.wifisignal | bool %} [wlan] -command={{ i3wm_usr.home }}/.config/i3blocks/wlan.sh +command={{ i3wm_usr.home }}/.config/i3blocks/wlan.py interval=1 signal=10 {% endif %}