commit 496ad2f3f639e6328ee92aebc0cf0bee292202ab from: mischa date: Thu May 07 14:13:45 2020 UTC added config file support, hue.conf commit - e7e27decb5dc8a763656c279cde23a1aa8514347 commit + 496ad2f3f639e6328ee92aebc0cf0bee292202ab blob - a0c06edd5e70a98b983ee245be00d6c4e9a173a8 blob + 3645eecfe1915135322e334f257be390ee63e585 --- .gitignore +++ .gitignore @@ -1,3 +1,4 @@ hue-bridge hue-email hue-token +_* blob - 6377fc0ab36c8ce44977b2c2b095d404fd6b43a5 blob + 94e556b4d9b799dd45da0f417042590ac73e6cce --- add-newdeveloper.py +++ 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() blob - 49935e0a799195fe965f8ce6d0454a9cf57cb088 blob + 632f6c6ea2376ef32d088f0a3c82f3332483849e --- daylight-trigger.py +++ daylight-trigger.py @@ -1,15 +1,19 @@ #!/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_tru 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 blob - 848a8c17c82b160b20e7f1dcc99f27e0c6d5d8d7 blob + 9a9ba6dc4ad4e39693c7ad20a081bfef644a6fa4 --- get-groups.py +++ 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 blob - 7b8fac9fd2d909fc78f541dcb555dfddd10329ad blob + b58744f75ab93d032de0d0424a5f3bdccee60e53 --- get-id.py +++ 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 blob - f60d94350e737616dc8d361d763e0c59142b0570 blob + bd2862cde63555b43dfe9a020826a43442e359ff --- get-lights.py +++ 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 blob - a65e8341a03dc49719dbc913b9f6a3dbc20420b1 blob + 2beef18025048774008f7d94a04ada1af6ee976b --- get-sensors.py +++ 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 blob - 4d89913b9e32a5381c32b452687c43ca6219b00f blob + 59a4eb4e4e132e204586ec4673dcdab1cd36b622 --- groupctl.py +++ 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_tru 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 blob - 52cf1637cd35df690424ceb606881dd3e88e985e blob + a75c136e6eff032e9a8f05fff25b92e331779250 --- lightctl.py +++ 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_tru 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 blob - /dev/null blob + ff0fd6aabf69f4fef30e40fd53b6cc1d20399346 (mode 644) --- /dev/null +++ hue.conf @@ -0,0 +1,6 @@ +[bridge1] +ip = 192.168.100.101 +token = bridge1token +[bridge2] +ip = 192.168.100.102 +token = bridge2token blob - 9026d575a89b80d7688d6b41d412a897968e1601 blob + d4cacb44173efafd8126554f43a65ac0f047ad54 --- temperature.py +++ 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 blob - e0e673d6611cf23a0627b441a16478fa4a5a62cd (mode 755) blob + /dev/null --- 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 blob - d1ef08f212a87a18d300668b1ac11c1a0ae6c79e blob + 7fc15b5382e8f483f1884cfa39d9a086a107c4b7 --- wrapper-sensors-battery.sh +++ 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