-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d99f5a1
commit 6be177e
Showing
7 changed files
with
695 additions
and
66 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
""" | ||
|
@@ -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()) | ||
|
@@ -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": | ||
|
@@ -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): | ||
|
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
Oops, something went wrong.