Set initial startup background brightness depending on daytime
Basic idea:
Save configuration file
/opt/.time_brightness_values
:{ "5,7": 0.25, "8,16": 0.50, "17,22": 0.75 }
Pay attention to quoted values and commas. First column with quoted values represents hour range in 24 hour format, the second column is percentage. For example, if you booted from 5 to 7 o'clock your brightness would be set to 25 percent. If you booted from 8 to 16 o'clock, it would be 50 percent.
Save the script below as
/opt/set_timed_brightness.py
#!/usr/bin/env python3 from __future__ import print_function from collections import OrderedDict import json import time import sys import os def read_config(): dir = '/opt' filename = '.time_brightness_values' conf_file = os.path.join(dir,filename) brightness = None with open(conf_file) as f: try: data = json.load(f) data = OrderedDict(sorted(data.items())) except Exception as e: print(e) sys.exit(1) else: keys = [ key.split(',') for key,value in data.items()] keys.sort() hour = time.localtime().tm_hour for key in keys: if int(key[0]) <= hour and int(key[1]) >= hour: brightness = data[','.join(key)] return brightness def set_percentage(pcent): dir = os.listdir('/sys/class/backlight') dev = os.path.join('/sys/class/backlight',dir[0]) max = None with open(os.path.join(dev,'max_brightness')) as f: max = f.readline().strip() new = int(float(max)*pcent) new = str(new) with open(os.path.join(dev,'brightness'),'w') as f: f.write(new) def main(): percentage = read_config() if percentage: set_percentage(percentage) if __name__ == '__main__': main()
Set
greeter-setup-script=/opt/set_timed_brightness.py
in the/etc/lightdm/lightdm.conf
file. The greeter setup script runs as root and once your system gets up to the login screen, the script will run and set the brightness you need.As far as waking up from suspend goes, create
/etc/pm/sleep.d/set_timed_brightness.sh
:#!/bin/bash case "${1}" in resume|thaw) python3 /opt/set_timed_brightness.py ;; esac
Remember: all the standard rules apply, the scripts must be made executable with chmod +x
, naming and calls to each file have to be consistent.