Skip to content

Instantly share code, notes, and snippets.

@joshland
Created March 31, 2021 17:47
Show Gist options
  • Save joshland/e8c5fc512c574ef48fa3b76dcdc735be to your computer and use it in GitHub Desktop.
Save joshland/e8c5fc512c574ef48fa3b76dcdc735be to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
##
## Poll xrandr to find brightness of connected monitors, set if above threshold
##
##
## Place this file in /usr/bin/dell_xps_15_brightness, set it to executable, and link it:
## ln -s /usr/bin/dell_xps_15_brightness /lib/systemd/system-sleep
##
## On Wakeup, your system should fix your brightness to the threshold below.
##
import re, sys, os
import subprocess
import logging
import logging.handlers
search_string = r'^(?P<monitor>[a-zA-Z0-9\-]+).connected.*Brightness:.(?P<level>[\d.]+)$'
msearch = re.compile(search_string, flags = re.M | re.S | re.I)
threshold = 0.6
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
if __name__ == "__main__":
if '-t' in sys.argv:
buffer = open('delltest.txt','r').read()
handler = logging.StreamHandler(sys.stdout)
else:
output = subprocess.run('/usr/bin/xrandr --verbose --current'.split(' '), capture_output=True)
buffer = output.stdout.decode(encoding='ASCII')
handler = logging.handlers.SysLogHandler(address = '/dev/log')
pass
formatter = logging.Formatter('%(module)s: %(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)
monitors = buffer.split('Screen')
log.debug(f'Found {len(monitors)} displays.')
for m in monitors:
for name, value in msearch.findall(m):
try:
value = float(value)
except:
continue
if value > threshold:
print(f'{name} brightness is {value}, too high, lowering to {threshold}')
subprocess.run(f'xrandr --output {name} --brightness {threshold}'.split(' '), capture_output=True)
continue
log.debug(f'Check display {name}, value within threshold: {value}.')
continue
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment