Skip to content

Commit

Permalink
Hotfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
overload08 committed Jan 22, 2017
1 parent d99f5a1 commit 6be177e
Show file tree
Hide file tree
Showing 7 changed files with 695 additions and 66 deletions.
21 changes: 12 additions & 9 deletions admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@


### package specific functions
def list_lights():
b=Bridge('10.0.0.129')
b.connect()
lights = b.get_light()
def list_lights(ip):
output = ""
for light in lights:
output += "Light ID : " + light[0] + "\n"
output += " Name : " + lights[light]["name"] + "\n"
output += "\n"
try:
b=Bridge(ip=ip,config_file_path="/var/lib/domogik/domogik_packages/plugin_hue/data/bridge.config")
b.connect()
lights = b.get_light()
for light in lights:
output += "Light ID : " + light[0] + "\n"
output += " Name : " + lights[light]["name"] + "\n"
output += "\n"
except:
output = "Error while retrieving Hue lamps... Have you push the bridge button?"
return output


Expand All @@ -45,7 +48,7 @@ def index(client_id):
client_detail = detail,
mactive="clients",
active = 'advanced',
hue = list_lights())
hue = list_lights(detail['data']['configuration'][1]['value']))

except TemplateNotFound:
abort(404)
Expand Down
98 changes: 61 additions & 37 deletions bin/hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Philips Hue Manager
@author: OverLoad <[email protected]>
@copyright: (C) 2007-2015 Domogik project
@copyright: (C) 2007-2017 Domogik project
@license: GPL(v3)
@organization: Domogik
"""
Expand All @@ -42,34 +42,31 @@
from phue import Bridge
import pprint
import time

def from_DT_Switch_to_off_on(x):
# 0 - 1 translated to off / on
if str(x) == "0":
return False
else:
return True

def from_off_on_to_DT_Switch(x):
# off - on translated to 0 - 1
if x == False:
return 0
else:
return 1
import os

class HueManager(Plugin):

def __init__(self):
""" Init plugin
"""
Plugin.__init__(self, name='hue')
if not os.path.exists(str(self.get_data_files_directory())):
self.log.info(u"Directory data not exist, trying create : %s", str(self.get_data_files_directory()))
try:
os.mkdir(str(self.get_data_files_directory()))
self.log.info(u"Hue data directory created : %s" % str(self.get_data_files_directory()))
except Exception as e:
self.log.error(e.message)
if not os.access(str(self.get_data_files_directory()), os.W_OK):
self.log.error("No write access on data directory : %s" % (str(self.get_data_files_directory())))
self.devices = self.get_device_list(quit_if_no_device=True)
self.commands = self.get_commands(self.devices)
self.sensors = self.get_sensors(self.devices)
self.ip_bridge = self.get_config("ip_bridge")
self.log.info(u"==> commands: %s" % format(self.commands))
self.log.info(u"==> sensors: %s" % format(self.sensors))
try:
b = Bridge(self.get_config("ip_bridge"))
b = Bridge(ip=self.ip_bridge,config_file_path="/var/lib/domogik/domogik_packages/plugin_hue/data/bridge.config")
b.connect()
except:
self.log.error(traceback.format_exc())
Expand All @@ -83,43 +80,55 @@ def __init__(self):
sensor_address = self.get_parameter(a_device, "Device")
self.device_list.update({device_id : {'name': device_name, 'address': sensor_address}})
thr_name = "dev_{0}".format(a_device['id'])
huethreads[thr_name] = threading.Thread(None,self.get_status,thr_name,(self.log,device_id,sensor_address,self.get_config("ip_bridge")),{})
self.log.info(u"Starting thread" + thr_name + " with paramerters : device_id=" + str(device_id) +", sensor_address=" + str(sensor_address) + ", ip_bridge=" + self.get_config("ip_bridge"))
huethreads[thr_name] = threading.Thread(None,self.get_status,thr_name,(self.log,device_id,sensor_address,self.ip_bridge),{})
self.log.info(u"Starting thread" + thr_name + " with paramerters : device_id=" + str(device_id) +", sensor_address=" + str(sensor_address) + ", ip_bridge=" + self.ip_bridge)
huethreads[thr_name].start()
self.register_thread(huethreads[thr_name])
# self.register_cb_update_devices(myHandleDeviceUpdate)
self.ready()

# def myHandleDeviceUpdate(self, devices):
# for hardDevice in self._myHardDevices:
# hardDevice.refreshAllDmgDevice(devices)
# self.log.info(u"All hard devives are updated from domogik devices")
def from_DT_Switch_to_off_on(self, x):
# 0 - 1 translated to off / on
if str(x) == "0":
return False
else:
return True

def from_off_on_to_DT_Switch(self, x):
# off - on translated to 0 - 1
if x == False:
return 0
else:
return 1

def get_status(self, log, device_id, address,bridge_ip):
while not self._stop.isSet():
b = Bridge(bridge_ip)
b.connect()
data = {}
status = b.get_light(address,'on')
brightness = b.get_light(address,'bri')/254.00*100.00
reachable = b.get_light(address,'reachable')
self.log.info(u"==> Device '%s' state '%s', brightness '%s', reachable '%s'" % (device_id, status, brightness, reachable))
data[self.sensors[device_id]['light']] = from_off_on_to_DT_Switch(status)
try:
b = Bridge(ip=bridge_ip,config_file_path="/var/lib/domogik/domogik_packages/plugin_hue/data/bridge.config")
b.connect()
status = b.get_light(address,'on')
brightness = b.get_light(address,'bri')/254.00*100.00
reachable = b.get_light(address,'reachable')
self.log.info(u"==> Device '%s' state '%s', brightness '%s', reachable '%s'" % (device_id, status, brightness, reachable))
self.log.debug(u"Trying to send data sensor...")
except:
self.log.debug(u"Unable to get device information for id " + str(device_id))
pass
data[self.sensors[device_id]['light']] = self.from_off_on_to_DT_Switch(status)
data[self.sensors[device_id]['brightness']] = brightness
data[self.sensors[device_id]['reachable']] = from_off_on_to_DT_Switch(reachable)
data[self.sensors[device_id]['reachable']] = self.from_off_on_to_DT_Switch(reachable)
try:
self.log.debug(u"Trying to send data sensor...")
self._pub.send_event('client.sensor', data)
except:
# We ignore the message if some values are not correct
self.log.debug(u"Bad MQ message to send. This may happen due to some invalid rainhour data. MQ data is : {0}".format(data))
pass
time.sleep(1)
time.sleep(1)

def on_mdp_request(self, msg):
self.log.error(u"Received MQ command, processing...")
Plugin.on_mdp_request(self, msg)
b = Bridge(self.get_config("ip_bridge"))
b = Bridge(self.ip_bridge)
b.connect()
sensors = {}
if msg.get_action() == "client.cmd":
Expand Down Expand Up @@ -166,15 +175,30 @@ def on_mdp_request(self, msg):
# We ignore the message if some values are not correct
self.log.debug(u"Bad MQ message to send. This may happen due to some invalid rainhour data. MQ data is : {0}".format(data))
pass
set = b.set_light(self.device_list[device_id]['address'], 'on', from_DT_Switch_to_off_on(sensors[self.sensors[device_id]['light']]))
set = b.set_light(self.device_list[device_id]['address'], 'on', self.from_DT_Switch_to_off_on(sensors[self.sensors[device_id]['light']]))
if ("success" in set):
if (set.index("success")) != -1:
status = True
else:
status = False

elif command == "send_alert":
self.log.debug(u"Sending alert on device " + self.device_list[device_id]['address'])
sensors[self.sensors[device_id]['light']] = data['current']
try:
self._pub.send_event('client.sensor', sensors)
except:
# We ignore the message if some values are not correct
self.log.debug(u"Bad MQ message to send. This may happen due to some invalid rainhour data. MQ data is : {0}".format(data))
pass
set = b.set_light(self.device_list[device_id]['address'], 'alert', 'lselect')
if ("success" in set):
if (set.index("success")) != -1:
status = True
else:
status = False

# Reply MQ REP (acq) to REQ command
self.send_rep_ack(status, reason, command_id, device_name) ;
self.send_rep_ack(status, reason, command_id, device_name)
return

def send_rep_ack(self, status, reason, cmd_id, dev_name):
Expand Down
73 changes: 53 additions & 20 deletions info.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"name": "White",
"commands": [
"set_brightness",
"set_on"
"set_on",
"send_alert"
],
"sensors": ["light", "brightness", "reachable"],
"parameters": [{
Expand All @@ -35,7 +36,25 @@
"description": "Device ID",
"type": "integer"
}]
}
},
"hue.rgb": {
"description": "RGB",
"id": "hue.rgb",
"name": "RGB",
"commands": [
"set_brightness",
"set_on",
"send_alert",
"set_hue"
],
"sensors": ["light", "brightness", "reachable", "hue"],
"parameters": [{
"key": "Device",
"xpl": false,
"description": "Device ID",
"type": "integer"
}]
}
},
"sensors": {
"light": {
Expand Down Expand Up @@ -79,6 +98,20 @@
"expire": 0,
"round_value": 0
}
},
"hue": {
"name": "Color",
"incremental": false,
"data_type": "DT_Number",
"conversion": "",
"timeout": 600,
"history": {
"store": true,
"duplicate": false,
"max": 0,
"expire": 0,
"round_value": 0
}
}
},
"xpl_stats": {},
Expand All @@ -101,24 +134,24 @@
"conversion": ""
}]
},
"get_light_status": {
"name": "Get light status",
"return_confirmation": true,
"parameters": [{
"key": "current",
"data_type": "DT_Switch",
"conversion": ""
}]
},
"get_brightness_value": {
"name": "Set ON",
"return_confirmation": true,
"parameters": [{
"key": "current",
"data_type": "DT_Scaling",
"conversion": "conv_100"
}]
}
"send_alert": {
"name": "Send alert",
"return_confirmation": true,
"parameters": [{
"key": "current",
"data_type": "DT_Trigger",
"conversion": ""
}]
},
"set_hue": {
"name": "Set HUE",
"return_confirmation": true,
"parameters": [{
"key": "current",
"data_type": "DT_Scaling",
"conversion": ""
}]
}
},
"xpl_commands": {}
}
Loading

0 comments on commit 6be177e

Please sign in to comment.