fanShimSet - tool for setting FanShim led etc

Here is a handy utility I wrote - the main intent is being able to set the FanShim LED colour from the command line.
You can then set colours from inside a long running script, maybe an overnight build, and signal results by the LED colour.

Just create a file ‘fanShimSet’ with this code and chmod 755

#!/usr/bin/env python3
#fanShimSet - a simple way to set the fan and LED colour on the Pimoroni FanShim
#(c)2019 Kym Farnik - free to use 
#requires the Pimoroni FanShim software to be installed as well as 'sudo pip3 install colour'
#  from https://github.com/pimoroni/fanshim-python
#useful in long running scripts to use the LED to report progress
#Color=US spelling
#Colour=the rest of the english speaking world
#
from fanshim import FanShim
import sys
import plasma
import argparse
import re
from colour import Color, COLOR_NAME_TO_RGB

def str2bool(val): #function to parse/normalaise the --fan -f setting to True/False
    if val.lower() in ('yes', 'true', 't', 'y', '1', 'on'):
        return True
    elif val.lower() in ('no', 'false', 'f', 'n', '0', 'off'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

parser = argparse.ArgumentParser(description='Sets the LED colour on the Pimoroni FanShim')
parser.add_argument('--listcolors', '-l', help="List all color words", action="store_true")
parser.add_argument('--color', '-c', default='notset', help="Color is a hex string or color name '#RRGGBB' eg. -c=#0000FF or -c White etc.")
parser.add_argument('--fan', '-f', type=str2bool, nargs='?', const=True, default=True, help="Fan On/Off (True/False) default True.")
parser.add_argument('--verbose', '-v', help="verbose actions", action="store_true")
args = parser.parse_args()

carg  = args.color.lower() #hex color #...... (or word)
match = re.search(r'^#(?:[0-9a-fA-F]{6})$', carg)
if match:
    r,g,b = (int(carg[i:i+2], 16) for i in (1, 3, 5))
elif carg in COLOR_NAME_TO_RGB:
    r,g,b = COLOR_NAME_TO_RGB[carg]
elif carg == 'notset':
    pass
else:
    print("Color is not valid, must be '#RRGGBB' format or name, eg. -c=#2257AC or --color '#aaff60' or -c Blue")
    sys.exit(1)
    
if args.listcolors:
    for cname in COLOR_NAME_TO_RGB.keys():
        rgb = COLOR_NAME_TO_RGB[cname]
        hc  = Color(cname)
        print(cname.capitalize(), "RGB:", rgb, "HEX:", hc.hex_l)

#now make it all happen...
fanshim = FanShim()
plasma.set_clear_on_exit(False) #this MUST be set False otherwise the led turns off on exit
if args.fan:
    fanshim.set_fan(True)       #fan on (keep it cool)
    if args.verbose:
        print("Fan is set on")
else:
    fanshim.set_fan(False)      #fan off
    if args.verbose:
        print('Warning: Fan is OFF, watch the temperature')
if carg != 'notset':
    fanshim.set_light(r, g, b)  #set color
    if args.verbose:
        chex = '#%02x%02x%02x' % (r, g, b)
        print("Color is set to RGB:", r, g, b, 'HEX:', chex)

sys.exit(0)