From 496ad2f3f639e6328ee92aebc0cf0bee292202ab Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 7 May 2020 16:13:45 +0200 Subject: [PATCH] added config file support, hue.conf --- .gitignore | 1 + add-newdeveloper.py | 6 +++--- daylight-trigger.py | 22 ++++++++++++++++------ get-groups.py | 19 +++++++++++++------ get-id.py | 19 +++++++++++++------ get-lights.py | 19 +++++++++++++------ get-sensors.py | 19 +++++++++++++------ groupctl.py | 19 +++++++++++++------ hue.conf | 6 ++++++ lightctl.py | 19 +++++++++++++------ temperature.py | 19 +++++++++++++------ wrapper-daylight-trigger.sh | 11 ----------- wrapper-sensors-battery.sh | 8 ++++---- 13 files changed, 121 insertions(+), 66 deletions(-) create mode 100644 hue.conf delete mode 100755 wrapper-daylight-trigger.sh diff --git a/.gitignore b/.gitignore index a0c06ed..3645eec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ hue-bridge hue-email hue-token +_* diff --git a/add-newdeveloper.py b/add-newdeveloper.py index 6377fc0..94e556b 100755 --- a/add-newdeveloper.py +++ b/add-newdeveloper.py @@ -6,7 +6,7 @@ # Create a new user on the bridge # # For example: -# $ create-new.py +# $ add-newdeveloper.py # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -19,8 +19,8 @@ import ssl import urllib.request import json -parser = argparse.ArgumentParser(description="Control light") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") +parser = argparse.ArgumentParser(description="Create a new developer token on the Hue Bridge") +parser.add_argument("bridge", type=str, help="Hue Bridge IP address") try: args = parser.parse_args() diff --git a/daylight-trigger.py b/daylight-trigger.py index 49935e0..632f6c6 100755 --- a/daylight-trigger.py +++ b/daylight-trigger.py @@ -1,14 +1,18 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191030 +# Version 1.1 - 20200507 - added config file support # # Control a light based on sensor information # Get ['dark'] from sensor ID and switch on/off light ID # depending where your sensor is located you can use ['daylight'] # # For example: -# $ daylight-trigger.py -s 50 -l 24 +# $ daylight-trigger.py -s 50 -l 24 +# +# Add the following to crontab: +# */5 * * * * //daylight-trigger.py -s 50 -l 24 -a dimmed # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -20,10 +24,11 @@ import argparse import ssl import urllib.request import json +import os +import configparser parser = argparse.ArgumentParser(description="Control light based on light sensor") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-s", "--sensor", type=int, required=True, help="sensor id#") parser.add_argument("-l", "--light", type=int, required=True, help="light id#") parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight") @@ -32,8 +37,7 @@ parser.add_argument("-d", "--debug", action='store_true', help="debug") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename sensor = args.sensor light = args.light action = args.action @@ -43,6 +47,12 @@ try: except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/get-groups.py b/get-groups.py index 848a8c1..9a9ba6d 100755 --- a/get-groups.py +++ b/get-groups.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191028 +# Version 1.1 - 20200507 - added config file support # # Get all light IDs # # For example: -# $ get-lights.py +# $ get-lights.py # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -18,19 +19,25 @@ import argparse import ssl import urllib.request import json +import os +import configparser parser = argparse.ArgumentParser(description="Get all group ids from Hue Bridge") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/get-id.py b/get-id.py index 7b8fac9..b58744f 100755 --- a/get-id.py +++ b/get-id.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191103 +# Version 1.1 - 20200507 - added config file support # # Collect all information of given id # # For example: -# $ get-id.py -t sensors -i 6 +# $ get-id.py -t sensors -i 6 # # Requires: # - Python 3.x @@ -15,18 +16,18 @@ import argparse import ssl import urllib.request import json +import os +import configparser parser = argparse.ArgumentParser(description="Get id information") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-i", "--id", type=int, default='1', help="id#") parser.add_argument("-t", "--type", type=str, default='lights', help="lights|sensors|groups|rules") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename id = args.id type = args.type verbose = args.verbose @@ -34,6 +35,12 @@ try: except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/get-lights.py b/get-lights.py index f60d943..bd2862c 100755 --- a/get-lights.py +++ b/get-lights.py @@ -1,13 +1,14 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191028 # Version 1.1 - 20191103 - added ['state']['on'] +# Version 1.2 - 20200507 - added config file support # # Get all light ids and state # # For example: -# $ get-lights.py +# $ get-lights.py # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -19,21 +20,27 @@ import argparse import ssl import urllib.request import json +import os +import configparser parser = argparse.ArgumentParser(description="Get all light ids from Hue Bridge") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-i", "--id", type=int, help="light id#") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename id = args.id except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/get-sensors.py b/get-sensors.py index a65e834..2beef18 100755 --- a/get-sensors.py +++ b/get-sensors.py @@ -1,14 +1,15 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191028 # Version 1.1 - 20191106 - added battery status +# Version 1.2 - 20200507 - added config file support # # Get all sensor IDs (ZLLPresence, ZLLLightLevel and ZLLTemperature) # grouped by ZLLPresence name # # For example: -# $ get-sensors.py +# $ get-sensors.py # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -22,23 +23,29 @@ import urllib.request import json import re import collections +import os +import configparser parser = argparse.ArgumentParser(description="Get all sensor ids from Hue Bridge") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-b", "--battery", type=int, help="battery check only, threshold, default 20") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename battery = args.battery verbose = args.verbose except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/groupctl.py b/groupctl.py index 4d89913..59a4eb4 100755 --- a/groupctl.py +++ b/groupctl.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191102 +# Version 1.1 - 20200507 - added config file support # # Control a group of lights (room) # # For example: -# $ groupctl.py -g 4 -a on +# $ groupctl.py -g 4 -a on # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -18,10 +19,11 @@ import argparse import ssl import urllib.request import json +import os +import configparser parser = argparse.ArgumentParser(description="Control group of lights (room)") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-g", "--group", type=int, required=True, help="group id#") parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") @@ -29,8 +31,7 @@ parser.add_argument("-d", "--debug", action='store_true', help="debug") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename group = args.group action = args.action verbose = args.verbose @@ -39,6 +40,12 @@ try: except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/hue.conf b/hue.conf new file mode 100644 index 0000000..ff0fd6a --- /dev/null +++ b/hue.conf @@ -0,0 +1,6 @@ +[bridge1] +ip = 192.168.100.101 +token = bridge1token +[bridge2] +ip = 192.168.100.102 +token = bridge2token diff --git a/lightctl.py b/lightctl.py index 52cf163..a75c136 100755 --- a/lightctl.py +++ b/lightctl.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191102 +# Version 1.1 - 20200507 - added config file support # # Control a light or plug # # For example: -# $ lightctl.py -l 24 -a relax +# $ lightctl.py -l 24 -a relax # # Follow the steps at the Hue Developer site to get the username/token # https://developers.meethue.com/develop/get-started-2/ @@ -18,10 +19,11 @@ import argparse import ssl import urllib.request import json +import os +import configparser parser = argparse.ArgumentParser(description="Control light") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-l", "--light", type=int, required=True, help="light id#") parser.add_argument("-a", "--action", type=str, default='on', help="on|off|relax|bright|dimmed|nightlight|state") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") @@ -29,8 +31,7 @@ parser.add_argument("-d", "--debug", action='store_true', help="debug") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename light = args.light action = args.action verbose = args.verbose @@ -39,6 +40,12 @@ try: except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/temperature.py b/temperature.py index 9026d57..d4cacb4 100755 --- a/temperature.py +++ b/temperature.py @@ -1,12 +1,13 @@ #!/usr/bin/env python3 # -# Copyright 2019, Mischa Peters , High5!. +# Copyright 2019-2020, Mischa Peters , High5!. # Version 1.0 - 20191103 +# Version 1.1 - 20200507 - added config file support # # Get temperaure from all sensors # # For exmaple: -# $ temperature.py +# $ temperature.py # # Requires: # - Python 3.x @@ -18,23 +19,29 @@ import json import re import collections import math +import os +import configparser parser = argparse.ArgumentParser(description="Get temperature from Hue Bridge") -parser.add_argument("bridge", type=str, help="Hue Bridge IP") -parser.add_argument("token", type=str, help="Hue API Token") +parser.add_argument("bridgename", type=str, help="Hue Bridge name in specified in hue.conf") parser.add_argument("-v", "--verbose", action='store_true', help="verbose") parser.add_argument("-d", "--debug", action='store_true', help="debug") try: args = parser.parse_args() - bridge = args.bridge - token = args.token + bridgename = args.bridgename verbose = args.verbose debug = args.debug except argparse.ArgumentError as e: print(str(e)) +config_files = ['./hue.conf', './.hue.conf', '/etc/hue.conf', '/etc/hue/hue.conf', os.path.expanduser('~/.hue.conf'), os.path.expanduser('~/hue.conf')] +config = configparser.RawConfigParser() +config.read(config_files) +bridge = config.get(bridgename, 'ip') +token = config.get(bridgename, 'token') + no_cert_check = ssl.create_default_context() no_cert_check.check_hostname=False no_cert_check.verify_mode=ssl.CERT_NONE diff --git a/wrapper-daylight-trigger.sh b/wrapper-daylight-trigger.sh deleted file mode 100755 index e0e673d..0000000 --- a/wrapper-daylight-trigger.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -PATH=$PATH:/usr/local/bin -# -# Easy invocation of python script -# Add the following to crontab: -# */5 * * * * //wrapper-daylight-trigger.sh -# -# ambient light sensor 43 to check -# light 7 to control -# scene dimmed -/home/mischa/hue/daylight-trigger.py $(cat /home/mischa/hue-bridge2) $(cat /home/mischa/hue-token2) -s 43 -l 7 -a dimmed diff --git a/wrapper-sensors-battery.sh b/wrapper-sensors-battery.sh index d1ef08f..7fc15b5 100755 --- a/wrapper-sensors-battery.sh +++ b/wrapper-sensors-battery.sh @@ -5,12 +5,12 @@ PATH=$PATH:/usr/local/bin # Add the following to crontab: # @daily //wrapper-sensors-battery.sh # -result=$(/home/mischa/hue/get-sensors.py $(cat /home/mischa/hue-bridge) $(cat /home/mischa/hue-token) -b 20) -result2=$(/home/mischa/hue/get-sensors.py $(cat /home/mischa/hue-bridge2) $(cat /home/mischa/hue-token2) -b 20) +result=$(/home/mischa/hue/get-sensors.py bridge1 -b 20) +result2=$(/home/mischa/hue/get-sensors.py bridge2 -b 20) if [[ -n "$result" ]]; then - echo "${result}" | mail -s "Hue Battery Status $(cat /home/mischa/hue-bridge)" $(cat /home/mischa/hue-email) + echo "${result}" | mail -s "Hue Battery Status bridge1" $(cat /home/mischa/hue-email) fi if [[ -n "$result2" ]]; then - echo "${result2}" | mail -s "Hue Battery Status $(cat /home/mischa/hue-bridge2)" $(cat /home/mischa/hue-email) + echo "${result2}" | mail -s "Hue Battery Status bridge2" $(cat /home/mischa/hue-email) fi