Skip to content

Commit

Permalink
Update for headsetcontrol v3
Browse files Browse the repository at this point in the history
Removes deprecated cli arg and uses json parsing instead.
  • Loading branch information
Psykar committed Apr 30, 2024
1 parent b37af6f commit 6c768fe
Showing 1 changed file with 33 additions and 51 deletions.
84 changes: 33 additions & 51 deletions headset-charge-indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# startup of the graphical desktop

import argparse
import json
from shutil import which
from sys import argv, exit
from subprocess import check_output, CalledProcessError
Expand All @@ -34,9 +35,7 @@
global SWITCHSOUND_BINARY
SWITCHSOUND_BINARY = None

OPTION_CAPABILITIES = '-?'
OPTION_BATTERY = '-b'
OPTION_SILENT = '-c'
OPTION_OUTPUT = "-oJSON"
OPTION_CHATMIX = '-m'
OPTION_SIDETONE = '-s'
OPTION_LED = '-l'
Expand Down Expand Up @@ -81,61 +80,49 @@ def change_icon():
ind.set_icon_full("monitor", "Monitor")
prevSwitch = 5

def fetch_capabilities():
try:
# ask HeadsetControl for the available capabilities for the current headset
output = check_output([HEADSETCONTROL_BINARY, OPTION_CAPABILITIES, OPTION_SILENT])
if args.verbose:
print('Cap: ' + str(output, 'utf-8'))

return output
except CalledProcessError as e:
print(e)
return "all"


def change_label():
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_BATTERY, OPTION_SILENT])
if args.verbose:
print('Bat: ' + str(output, 'utf-8'))

# -1 indicates "Battery is charging"
if int(output) == -1:
text = 'Chg'
# -2 indicates "Battery is unavailable"
elif int(output) == -2:
text = 'Off'
elif int(output) < 100:
text = str(output, 'utf-8') + '%'
else:
text = str(output, 'utf-8') + '%'
output = check_output([HEADSETCONTROL_BINARY, OPTION_OUTPUT])
except CalledProcessError as e:
print(e)
text = 'N/A'
else:
if args.verbose:
print('Bat: ' + str(output, 'utf-8'))

ind.set_label(text, '999%')
charge.get_child().set_text('Charge: ' + text)

j = json.loads(output)
if len(j["devices"]) >= 1:
bat = j["devices"][0]["battery"]
level = bat.get("level")
status = bat.get("status")
if level >=0:
text = f"{level}%"
else:
if status == "BATTERY_AVAILABLE":
text = "Ava"
elif status == "BATTERY_UNAVAILABLE":
text = "Off"
elif status == "BATTERY_CHARGING":
text = "Chg"
else:
text = "Unknown"
cm = j["devices"][0].get("chatmix")
if cm:
chatmix.get_child().set_text(f"ChatMix: {cm}")
else:
chatmix.get_child().set_text(f"ChatMix: N/A")

def change_chatmix():
global chatmix

try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_CHATMIX, OPTION_SILENT])
if args.verbose:
print("ChatMix: " + str(output, 'utf-8'))
chatmix.get_child().set_text('ChatMix: ' + str(output, 'utf-8'))
except CalledProcessError as e:
print(e)
chatmix.get_child().set_text('ChatMix: N/A')
ind.set_label(text, '999%')
charge.get_child().set_text('Charge: ' + text)


def set_sidetone(dummy, level):
if args.verbose:
print("Set sidetone to: " + str(level))
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_SIDETONE, str(level), OPTION_SILENT])
output = check_output([HEADSETCONTROL_BINARY, OPTION_SIDETONE, str(level), OPTION_OUTPUT])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
Expand All @@ -148,7 +135,7 @@ def set_inactive_time(dummy, level):
if args.verbose:
print("Set inactive-time to: " + str(level))
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_INACTIVE_TIME, str(level), OPTION_SILENT])
output = check_output([HEADSETCONTROL_BINARY, OPTION_INACTIVE_TIME, str(level), OPTION_OUTPUT])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
Expand All @@ -161,7 +148,7 @@ def set_led(dummy, level):
if args.verbose:
print("Set LED to: " + str(level))
try:
output = check_output([HEADSETCONTROL_BINARY, OPTION_LED, str(level), OPTION_SILENT])
output = check_output([HEADSETCONTROL_BINARY, OPTION_LED, str(level), OPTION_OUTPUT])
if args.verbose:
print("Result: " + str(output, 'utf-8'))
except CalledProcessError as e:
Expand Down Expand Up @@ -313,13 +300,8 @@ def switch_menu():


def refresh(dummy):
cap = fetch_capabilities()

change_icon()
if "all" == cap or b'b' in cap:
change_label()
if "all" == cap or b'm' in cap:
change_chatmix()
change_label()

# return True to keep the timer running
return True
Expand Down

0 comments on commit 6c768fe

Please sign in to comment.