From ef999189ea47ed3aacb0678874837040135f1daa Mon Sep 17 00:00:00 2001 From: mischa Date: Fri, 27 Dec 2019 15:45:35 +0100 Subject: [PATCH] added 'state' function --- lightctl.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lightctl.py b/lightctl.py index 64a9281..52cf163 100755 --- a/lightctl.py +++ b/lightctl.py @@ -23,7 +23,7 @@ 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("-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") +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") parser.add_argument("-d", "--debug", action='store_true', help="debug") @@ -71,7 +71,11 @@ def get_state(id): json_data = json.loads(content) if debug: print (f"State for light id {id}:\n{json_data['state']}") if debug: print (f"Type for light id {id}: {json_data['config']['archetype']}") - return (json_data['state']) + if 'state' in json_data: + return (json_data['state']) + else: + print(f"id {id} doesn't exist") + return -1 def put_state(id, state): if not 'colormode' in state: @@ -100,4 +104,11 @@ def put_state(id, state): return(res) light_state = get_state(light) -put_state(light, light_state) +if not action == 'state': + put_state(light, light_state) +else: + if light_state['reachable']: + state = 'on' if light_state['on'] else 'off' + print(state) + else: + print("unreachable")