Last active
December 10, 2020 16:20
-
-
Save nampereira/5601342095328e4a8df06410056b5d03 to your computer and use it in GitHub Desktop.
Simple light app example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import os | |
import json | |
import arena # arena python library | |
from pylutron_wrapper.smartbridge import SmartBridgeWrapper # wrapper to https://github.com/gurumitts/pylutron-caseta | |
def light_toggle(light_sw_obj, light_sw_state, device_id): | |
"""toggles the lutron device""" | |
if light_sw_state == 0: | |
SmartBridgeWrapper.turn_on_light(device_id) | |
light_sw_obj.update(color=(0, 255, 0)) | |
light_sw_state = 1 | |
else: | |
SmartBridgeWrapper.turn_off_light(device_id) | |
light_sw_obj.update(color=(255, 0, 0)) | |
light_sw_state = 0 | |
return light_sw_state | |
def light_select(light_sw_obj, light_sw_state): | |
"""change the brightness on mouse hover events""" | |
if light_sw_state == 1: | |
light_sw_obj.update(color=(0, 255, 0)) | |
else: | |
light_sw_obj.update(color=(255, 0, 0)) | |
# This function is called to help with changing the brightness on mouse hover events | |
def light_unselect(light_sw_obj, light_sw_state): | |
"""change the brightness on mouse leave events""" | |
if light_sw_state == 1: | |
light_sw_obj.update(color=(0, 150, 0)) | |
else: | |
light_sw_obj.update(color=(150, 0, 0)) | |
def scene_callback(msg): | |
"""MQTT message callback function for the scene""" | |
# we will access this global state | |
global light_sw_state # state of the light (on/off) | |
global light_sw_obj # arena object for the switch (sphere) | |
global device_id # lutron device id for the light | |
jsonMsg = json.loads(msg) | |
if jsonMsg['action'] != 'clientEvent': | |
return | |
# Check for mouse events | |
if jsonMsg['type'] == 'mouseenter': | |
light_select(light_sw_obj, light_sw_state) | |
if jsonMsg['type'] == 'mouseleave': | |
light_unselect(light_sw_obj, light_sw_state) | |
if jsonMsg['type'] == 'mousedown': | |
light_sw_state = light_toggle(light_sw_obj, light_sw_state, | |
device_id) | |
if __name__ == '__main__': | |
# we will access this global state | |
global light_sw_state | |
global device_id | |
global light_sw_obj | |
# init light | |
light_sw_state = 0 | |
# program parameters are received in environment variables | |
host = os.environ.get('MQTTH', 'arena.andrew.cmu.edu') | |
realm = os.environ.get('REALM', 'realm') | |
scene = os.environ.get('SCENE', 'kitchen') # default to scene 'kitchen' | |
sw_name = os.environ.get('SW_NAME', None) | |
sw_location_str = os.environ.get('SW_LOC', '2.5, 2.3, -0.6') | |
device_id = os.environ.get('DEV_ID', None) | |
# convert string to x, y, z tuple | |
print(sw_location_str.split(', ')) | |
sw_location = tuple(map(float, sw_location_str.split(', '))) | |
# init Lutron Caseta MQTT wrapper | |
SmartBridgeWrapper.init_light_system() | |
# connect to arena scene | |
arena.init(host, realm, scene, scene_callback) | |
print('setting up objects') | |
# create a sphere to represent the light switch | |
light_sw_obj = arena.Object( | |
objType=arena.Shape.sphere, | |
objName=sw_name, | |
location=sw_location, | |
color=(150, 0, 0), | |
scale=(0.1, 0.1, 0.1), | |
data='{"material": {"transparent":true,"opacity": 0.5}}', | |
clickable=True, | |
persist=True, | |
) | |
print('starting main loop') | |
arena.handle_events() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment