from hueclient.api import hue_api from hueclient.models.sensors import TapSwitch if __name__ == "__main__": def handle(resource, field, previous, current): print "Change from {} to {}".format(previous, current) hue_api.authenticate_interactive(app_name="Tap Test") # Ask the user which switch we want to monitor for tap in TapSwitch.objects.all(): print ("Switch {id}: {name}".format(**tap.as_dict())) # Get the switch switch_id = raw_input("Enter switch ID to monitor: ") switch = TapSwitch.objects.get(id=switch_id) # Setup the monitoring as follows: # - Any time the switch's state changes call handle() # - Check for a change every 0.2 seconds switch.monitor(lambda sw: sw.state.as_dict(), handle, poll_interval=0.2) # Start the monitoring print ("Starting monitoring. Control-c to exit.") hue_api.start_monitor_loop() # Execution will never pass this point
from pprint import pprint from hueclient.api import hue_api from hueclient.models.light import Light if __name__ == '__main__': hue_api.authenticate_interactive(app_name='List Lights Example') # Show a summary of all lights first for light in Light.objects.all(): print( "Light {id} is named '{name}' and is {onoff} (brightness: {brightness})".format( id=light.id, name=light.name, onoff='on' if light.state.on else 'off', brightness=light.state.brightness, ) ) while True: # Let's offer the user some more detailed information light_id = raw_input("Enter a light ID for more information (enter to exit): ") if not light_id: print('Bye!') exit() # Get the light using the specified ID light = Light.objects.get(id=light_id) # Get the information as a dictionary using as_dict() light_as_dict = light.as_dict() light_as_dict['state'] = light.state.as_dict()
from time import sleep from hueclient.api import hue_api from hueclient.models.groups import Group if __name__ == '__main__': hue_api.authenticate_interactive(app_name='Blink Group Example') group = Group.objects.get(id=1) print("Blinking group named '{name}', control-c to exit".format( name=group.name)) while True: group.action.on = not group.action.on group.action.save() sleep(2)
from time import sleep from hueclient.api import hue_api from hueclient.models.groups import Group if __name__ == '__main__': hue_api.authenticate_interactive(app_name='Blink Group Example') group = Group.objects.get(id=1) print("Blinking group named '{name}', control-c to exit".format( name=group.name )) while True: group.action.on = not group.action.on group.action.save() sleep(2)
from hueclient.api import hue_api from hueclient.models.sensors import TapSwitch if __name__ == '__main__': def handle(resource, field, previous, current): print "Change from {} to {}".format(previous, current) hue_api.authenticate_interactive(app_name='Tap Test') # Ask the user which switch we want to monitor for tap in TapSwitch.objects.all(): print("Switch {id}: {name}".format(**tap.as_dict())) # Get the switch switch_id = raw_input("Enter switch ID to monitor: ") switch = TapSwitch.objects.get(id=switch_id) # Setup the monitoring as follows: # - Any time the switch's state changes call handle() # - Check for a change every 0.2 seconds switch.monitor(lambda sw: sw.state.as_dict(), handle, poll_interval=0.2) # Start the monitoring print("Starting monitoring. Control-c to exit.") hue_api.start_monitor_loop() # Execution will never pass this point
from time import sleep from hueclient.api import hue_api from hueclient.models.light import Light if __name__ == '__main__': hue_api.authenticate_interactive(app_name='Blink Light Example') light = Light.objects.get(id=1) print("Blinking light named '{name}', control-c to exit".format( name=light.name)) while True: light.state.on = not light.state.on light.state.save() sleep(2)
from hueclient.api import hue_api from hueclient.models.light import Light from hueclient.models.scenes import SceneStateChange, Scene if __name__ == '__main__': hue_api.authenticate_interactive(app_name='List Lights Example') # SceneStateChange is write-only. We can therefore # not do the usual retrieve-then-edit process. # Therefore instantiate the resource here, make the changes, # then save it. change = SceneStateChange( scene=Scene.objects.get(id='e2eab6bf6-on-0'), light=Light.objects.get(id=1), ) change.on = True change.save()