added 'state' function

This commit is contained in:
mischa 2019-12-27 15:45:35 +01:00
parent b455762fa2
commit ef999189ea
1 changed files with 14 additions and 3 deletions

View File

@ -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")