Skip to content

Commit

Permalink
Work on #11
Browse files Browse the repository at this point in the history
  • Loading branch information
overload08 committed Jan 27, 2017
1 parent 3241056 commit 8e10097
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions bin/hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from phue import Bridge
import time
import os
import pprint

class HueManager(Plugin):

Expand All @@ -64,6 +65,7 @@ def __init__(self):
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.sensors_values = {}
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))
Expand Down Expand Up @@ -122,11 +124,15 @@ def get_status(self, device_id, lamp_id, bridge_ip):
except:
self.log.debug(u"Unable to get device information for id " + str(device_id))
data[self.sensors[device_id]['light']] = self.from_off_on_to_dt_switch(status)
self.sensors_values[self.sensors[device_id]['light']] = self.from_off_on_to_dt_switch(status)
if self.from_off_on_to_dt_switch(status) == 0 or self.from_off_on_to_dt_switch(reachable) == 0:
data[self.sensors[device_id]['brightness']] = 0
self.sensors_values[self.sensors[device_id]['brightness']] = 0
else:
data[self.sensors[device_id]['brightness']] = brightness
self.sensors_values[self.sensors[device_id]['brightness']] = brightness
data[self.sensors[device_id]['reachable']] = self.from_off_on_to_dt_switch(reachable)
self.sensors_values[self.sensors[device_id]['reachable']] = self.from_off_on_to_dt_switch(reachable)
try:
if (data != old_data) or (time.time() - previous_time >= interval):
self.log.info(u"==> Lamp '%s' state '%s', brightness '%s', reachable '%s'" % (lamp_id, status, brightness, reachable))
Expand Down Expand Up @@ -159,21 +165,23 @@ def on_mdp_request(self, msg):
device_name = self.device_list[device_id]["name"]
self.log.debug(u"==> Received MQ REQ command message: %s" % format(data))
command = list(self.commands[device_id].keys())[list(self.commands[device_id].values()).index(command_id)]
bridgecmd = {}
if command == "set_brightness":
if data['bri'] == 0:
sensors[self.sensors[device_id]['light']] = 0
else:
new_value = int(float(data['bri'])) * 254/100
if self.sensors_values[self.sensors[device_id]['light']] == 0:
sensors[self.sensors[device_id]['light']] = 1
bridgecmd = {'on' : True, 'bri' : new_value}
else:
bridgecmd = {'bri' : new_value}
sensors[self.sensors[device_id]['brightness']] = data['bri']
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))

new_value = int(float(data['bri'])) * 254/100
self.log.debug(u"Set brightness to '%s' light to '%s'" % (data['bri'], new_value))
set = self.bridge.set_light(self.device_list[device_id]['address'], 'bri', new_value)
set = self.bridge.set_light(self.device_list[device_id]['address'], bridgecmd)
if "success" in set:
if set.index("success") != -1:
status = True
Expand Down

0 comments on commit 8e10097

Please sign in to comment.