#!/usr/bin/env bash # # Status bar for Sway # Needs a .env.homeassistant with an API token in ~/.config/sway/.env.homeassistant # XXX is it possible to display a motd, but keep it longer than the rest in here? # TODO: # - HA optimize API calls: https://community.home-assistant.io/t/get-limited-number-of-states-entities-when-using-api-states/830323 # man 7 swaybar-protocol homeassistant_url="http://10.7.1.11:8123/api/states" common() { bg_ok="#66ac9f" # bg_ok_trans="#00000033" bg_ok_trans="#00000000" text_dark="#36454f" text_light="#c0c8c6" warn1="#facc15" warn2="#cc8d22" crit="#710f3d" . ~/.config/sway/.env.homeassistant echo -n "{" echo -n "\"separator_block_width\":0," echo -n "\"separator\":false," echo -n "\"color\":\"$fg\"," echo -n "\"background\":\"$bg\"," echo -n "\"name\":\"$name\"," echo -n "\"full_text\":\" $stat\"," echo -n "}," } temp_cpu() { local name="cpu" local cpu=$(expr $(cat /sys/class/thermal/thermal_zone2/temp) / 1000) if [[ $(echo "$cpu >= 60" | bc -l ) == "1" ]] && [[ $(echo "$cpu < 70" | bc -l ) == "1" ]]; then local bg=$warn1 local fg=$text_dark elif [[ $(echo "$cpu >= 70" | bc -l ) == "1" ]] && [[ $(echo "$cpu < 80" | bc -l ) == "1" ]]; then local bg=$warn2 local fg=$text_dark elif [[ $(echo "$cpu >= 80" | bc -l ) == "1" ]]; then local bg=$crit local fg=$text_light else local bg=$bg_ok_trans local fg=$text_light fi icon="󰍛" local stat=$icon\ $cpu\ °C" " common } mem(){ local name="mem" local memused=$(free | grep Mem | awk '{printf "%.1f\n", $3/$2 * 100.0}') local memproc=$(basename "$(ps --no-headers -A --sort -rss -o cmd | head -1 | awk {'print $1'})") if [[ $(echo "$memused >= 50" | bc -l ) == "1" ]] && [[ $(echo "$memused < 60" | bc -l ) == "1" ]]; then local bg=$warn1 local fg=$text_dark elif [[ $(echo "$memused >= 60" | bc -l ) == "1" ]] && [[ $(echo "$memused < 70" | bc -l ) == "1" ]]; then local bg=$warn2 local fg=$text_dark elif [[ $(echo "$memused >= 70" | bc -l ) == "1" ]]; then local bg=$crit local fg=$text_light else local bg=$bg_ok_trans local fg=$text_light fi icon="" local stat=$icon\ $memused\ %\ [$memproc]" " common } wifi() { local name="wifi" # orig = negative number local wifidbm_orig=$(iwctl station wlan0 show | awk '/[[:space:]]RSSI/{print $2}') local wifidbm=$((wifidbm_orig * -1)) if [[ $(echo "$wifidbm >= 50" | bc -l ) == "1" ]] && [[ $(echo "$wifidbm < 60" | bc -l ) == "1" ]]; then local bg=$warn1 local fg=$text_dark elif [[ $(echo "$wifidbm >= 60" | bc -l ) == "1" ]] && [[ $(echo "$wifidbm < 70" | bc -l ) == "1" ]]; then local bg=$warn2 local fg=$text_dark elif [[ $(echo "$wifidbm >= 70" | bc -l ) == "1" ]]; then local bg=$crit local fg=$text_light else local bg=$bg_ok_trans local fg=$text_light fi icon="" local stat=$icon\ -$wifidbm\ dBm" " common } mydate() { # local bg=$bg_ok_trans local bg=#404040b3 # local fg=$bg_ok local fg=$warn1 local name="id_time" local icon="" local stat=\ $icon\ $(date +'%A, %d %B %R %Z')\ common } loadavg() { local bg=$bg_ok_trans local fg=$text_light local name="loadavg" icon="󰍛" local stat=$icon\ $(awk {'print $1,$2,$3'} /proc/loadavg) common } temp_indoor() { local bg=$bg_ok_trans local fg=$text_light local name="temp_indoor" local ha_indoor=$(curl -s -H "Authorization: Bearer $ha_token" -H "Content-Type: application/json" $homeassistant_url/sensor.vindstyrka_oli_temperature | jq -r '.state') icon="" local stat=$icon\ $ha_indoor\ °C" " common } humid_indoor() { local bg=$bg_ok_trans local fg=$text_light local name="temp_indoor" local ha_indoor=$(curl -s -H "Authorization: Bearer $ha_token" -H "Content-Type: application/json" $homeassistant_url/sensor.vindstyrka_oli_humidity | jq -r '.state') icon="" local stat=$icon\ $ha_indoor\ %" " common } powerdraw() { local bg=$bg_ok_trans local fg=$text_light local name="temp_indoor" local ha_indoor=$(curl -s -H "Authorization: Bearer $ha_token" -H "Content-Type: application/json" $homeassistant_url/sensor.inspelning_oli_power | jq -r '.state') icon="" local stat=$icon\ $ha_indoor\ W" " common } audio_volume() { local name="audio" local volume=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk {'print $NF*100'}) if [[ $(echo "$volume == 0" | bc -l ) == "1" ]] ; then local bg=$text_light local fg=$bg_ok_trans elif [[ $(echo "$volume >= 50" | bc -l ) == "1" ]] && [[ $(echo "$volume < 60" | bc -l ) == "1" ]]; then local bg=$warn1 local fg=$text_dark elif [[ $(echo "$volume >= 60" | bc -l ) == "1" ]] && [[ $(echo "$volume < 70" | bc -l ) == "1" ]]; then local bg=$warn2 local fg=$text_dark elif [[ $(echo "$volume >= 70" | bc -l ) == "1" ]]; then local bg=$crit local fg=$text_light else local bg=$bg_ok_trans local fg=$text_light fi icon="" local stat=$icon\ $volume\ %" " common } # header echo '{ "version": 1, "click_events":false}' echo '[[],' while : do echo -n "[" mydate loadavg temp_indoor humid_indoor powerdraw temp_cpu mem wifi audio_volume echo -n "]," sleep 5 done