Skip to content

Commit

Permalink
fix reset action + code comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ledge74 committed Nov 12, 2015
1 parent 8b2c33b commit 28471dc
Showing 1 changed file with 70 additions and 38 deletions.
108 changes: 70 additions & 38 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def Start():
ObjectContainer.art = R(ART)
ValidatePrefs()


####################################################################################################
# Main menu
####################################################################################################
Expand Down Expand Up @@ -183,7 +182,7 @@ def ConnectBridge():
thumb = R('hellohue.png'),
)
####################################################################################################
# Returns object to signin to Hue Bridge
# Callback to signin to Hue Bridge
####################################################################################################
def ConnectBridgeCallback():
Log("Trying to connect")
Expand Down Expand Up @@ -216,7 +215,7 @@ def EnableHelloHue():
)

####################################################################################################
# Return object to enable the Channel
# Callback to enable the Channel
####################################################################################################
def EnableHelloHueCallback():
Log("Trying to enable thread")
Expand All @@ -237,7 +236,7 @@ def DisableHelloHue():
)

####################################################################################################
# Return object to disable the Channel
# Callback to disable the Channel
####################################################################################################
def DisableHelloHueCallback():
Log("Trying to disable thread")
Expand Down Expand Up @@ -274,7 +273,7 @@ def ValidatePrefs():
return MainMenu(header=NAME)

####################################################################################################
# Philips Hue Commands
# Philips Hue Check Commands
####################################################################################################

class HueCheck:
Expand Down Expand Up @@ -319,6 +318,10 @@ def connect_to_bridge(self):
else:
return e

####################################################################################################
# Philips Hue Commands
####################################################################################################

class Hue:
def __init__(self):
Log("Initializing Hue class")
Expand All @@ -340,6 +343,8 @@ def get_hue_light_initial_state(self, client_name):
line = {}
line['on'] = B.get_light(light, 'on')
line['bri'] = B.get_light(light, 'bri')
line['hue'] = B.get_light(light, 'hue')
line['sat'] = B.get_light(light, 'sat')
dico[light] = line
LIGHT_GROUPS_INITIAL_STATE[client_name] = dico
Log(LIGHT_GROUPS_INITIAL_STATE[client_name])
Expand All @@ -360,8 +365,10 @@ def update_light_state(self, powered, brightness, lights):
def reset_lights_state(self, client_name):
Log("--Reset lights")
lights = LIGHT_GROUPS_INITIAL_STATE[client_name]
Log("lights : %s" % lights)
for light in lights:
command = {'on': lights[light]['on'], 'bri': lights[light]['bri']}
Log("light : %s" % light)
command = {'on': lights[light]['on'], 'bri': lights[light]['bri'], 'hue':lights[light]['hue'], 'sat': lights[light]['sat']}
B.set_light(light, command)

####################################################################################################
Expand Down Expand Up @@ -404,7 +411,7 @@ def get_plex_status(self):
return e

####################################################################################################
# PlexHue Commands
# Compile rooms in list/dictionary on plugin start or pref change
####################################################################################################

def CompileRooms():
Expand All @@ -425,6 +432,10 @@ def CompileRooms():
j += 1
Log("Room check done")

####################################################################################################
# Put all available clients status to '' on plugin start or pref change
####################################################################################################

def InitiateCurrentStatus():
Log("Initiating current status")
global CURRENT_STATUS, LIGHT_GROUPS_INITIAL_STATE
Expand All @@ -433,12 +444,20 @@ def InitiateCurrentStatus():
for client in ReturnClients():
CURRENT_STATUS[client] = ''

####################################################################################################
# Return all configured clients from preferences
####################################################################################################

def ReturnClients():
client_list = []
for clients in rooms:
client_list.append(clients['client'])
return client_list

####################################################################################################
# Return all lights attached a specific client
####################################################################################################

def ReturnLightsFromClient(client):
lights_list = []
for clients in rooms:
Expand All @@ -447,6 +466,10 @@ def ReturnLightsFromClient(client):
lights_list.append(light)
return lights_list

####################################################################################################
# Return the list of all lights
####################################################################################################

def ReturnAllLights():
lights_list = []
for clients in rooms:
Expand All @@ -455,6 +478,10 @@ def ReturnAllLights():
lights_list.append(light)
return lights_list

####################################################################################################
# Return a list of authorized users for a specific client
####################################################################################################

def ReturnUsersFromClient(client):
users_list = []
for clients in rooms:
Expand All @@ -463,6 +490,10 @@ def ReturnUsersFromClient(client):
users_list.append(light)
return users_list

####################################################################################################
# Listen to Plex Media Server websocket
####################################################################################################

def run_websocket_watcher():
global ws
Log('Starting websocket listener')
Expand All @@ -475,21 +506,35 @@ def run_websocket_watcher():
Log("Up and running, listening")
ws.run_forever()

####################################################################################################
# If websocket detected, trigger PMS sessions status analyze
####################################################################################################

def on_message(ws, message):
json_object = json.loads(message)
#Log(json_object)
if json_object['type'] == 'playing':
plex_status = plex.get_plex_status()
is_plex_playing(plex_status)

def on_close(ws):
Log("### closed ###")

####################################################################################################
# Parse PMS sessions status
####################################################################################################

def is_plex_playing(plex_status):
configuredclients = ReturnClients()
i = 0
j = 0
ACTIVE_CLIENTS = []
for item in plex_status.findall('Video'):
for client_name in configuredclients:
if item.find('Player').get('title') == client_name:
if not client_name in ACTIVE_CLIENTS:
ACTIVE_CLIENTS.append(client_name)
i += 1
configuredusers = ReturnUsersFromClient(client_name)
for username in configuredusers:
if item.find('User').get('title') == username:
j += 1
if item.find('Player').get('state') == 'playing' and CURRENT_STATUS[client_name] != item.find('Player').get('state'):
if CURRENT_STATUS[client_name] == '':
Log(time.strftime("%I:%M:%S") + " - New Playback (saving initial state): - %s %s %s - %s on %s."% (item.find('User').get('title'), CURRENT_STATUS[client_name], item.get('grandparentTitle'), item.get('title'), client_name))
Expand All @@ -508,30 +553,18 @@ def is_plex_playing(plex_status):
if isitdark() is True:
choose_action(CURRENT_STATUS[client_name], ReturnLightsFromClient(client_name), client_name)
return False
#else:
# return False
#if i == 0 and Prefs['HUE_ADVANCED'] is False:
# Log("No authorized clients are playing - skipping")
# return False

#if j == 0 and Prefs['HUE_ADVANCED'] is False:
# Log("No authorized users are playing - skipping")
# return False

#for client in CURRENT_STATUS:
# if CURRENT_STATUS[client] == 'stopped':
# Log(time.strftime("%I:%M:%S") + " - Waiting for new playback on %s"%client)
# CURRENT_STATUS[client] = ''
# return False

for client in CURRENT_STATUS:
if not client in ACTIVE_CLIENTS:
if not CURRENT_STATUS[client] == 'stopped' and not CURRENT_STATUS[client] == '':
CURRENT_STATUS[client] = ''
Log(time.strftime("%I:%M:%S") + " - Playback stopped on %s - Waiting for new playback" % client);
if isitdark() is True:
choose_action(CURRENT_STATUS[client], ReturnLightsFromClient[client])
Log(CURRENT_STATUS[client])
choose_action("stopped", ReturnLightsFromClient(client), client)

####################################################################################################
# Choose action based on playback status and preferences
####################################################################################################

def choose_action(state, lights, client_name):
Log("Selecting action")
Expand All @@ -547,12 +580,17 @@ def choose_action(state, lights, client_name):
elif Prefs['HUE_ACTION_' + state.upper()] == "Nothing":
return
elif Prefs['HUE_ACTION_' + state.upper()] == "Reset":
Log("will reset")
reset_lights(client_name)
return
else:
Log("No matching action found")
return

####################################################################################################
# Calculate if it's dark outside at user's location
####################################################################################################

def isitdark():
if Prefs['HUE_DARK'] is False:
Log("Dark pref set to false: triggering")
Expand All @@ -571,6 +609,10 @@ def isitdark():
Log("It's dark outside: triggering")
return True

####################################################################################################
# Execute lights actions
####################################################################################################

def reset_lights(client_name):
Log("Reseting lights")
hue.reset_lights_state(client_name)
Expand All @@ -591,13 +633,3 @@ def dim_lights(lights):
dim_value = int(float(Prefs['HUE_DIM']))
hue.update_light_state(powered=True, brightness=dim_value, lights=lights)
pass

def on_message(ws, message):
json_object = json.loads(message)
#Log(json_object)
if json_object['type'] == 'playing':
plex_status = plex.get_plex_status()
is_plex_playing(plex_status)

def on_close(ws):
Log("### closed ###")

0 comments on commit 28471dc

Please sign in to comment.