From 910ed70e03a35fe7101ae3d2a9cec4c82accade6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 11 Jan 2017 12:43:17 +0300 Subject: [PATCH] Lightbulbstate class extracted --- src/lightbulbstate.py | 64 +++++++++++++++++++++++++++++++++++++++++++ src/main.py | 63 ++---------------------------------------- 2 files changed, 66 insertions(+), 61 deletions(-) create mode 100644 src/lightbulbstate.py diff --git a/src/lightbulbstate.py b/src/lightbulbstate.py new file mode 100644 index 0000000..1d976a4 --- /dev/null +++ b/src/lightbulbstate.py @@ -0,0 +1,64 @@ +import pyyeelight +import os +import time +import threading + +class LightBulbState: + bright = 0 + color_temperature = 0 + status = "off" + rgb = 0 + + ip = "" + name = "" + yeelight = None # yeelight object + + def __init__(self, ip, yeelightObj): + self.yeelight = yeelightObj + self.name = yeelightObj.__name__ + self.ip = ip + + def update_properties(self, force = False): + if (force): + self.yeelight.refresh_property() + prop = self.yeelight.get_all_properties() + # print(prop) + self.bright = prop["bright"] + self.color_temperature = prop["ct"] + self.status = prop["power"] + self.rgb = prop["rgb"] + + def hash(self): + return str(self.bright) + ":" + str(self.color_temperature) + ":" + str(self.status) + ":" + str(self.rgb) + + def is_int(self, x): + try: + tmp = int(x) + return True + except Exception as e: + return False + + def process_command(self, param, value): + try: + if (param == 'status'): + if (value == "on"): + print("Turning on bulb", self.name) + self.yeelight.turn_on() + if (value == "off"): + print("Turning off bulb", self.name) + self.yeelight.turn_off() + if (param == 'bright' and self.is_int(value)): + print("Setting brightness of bulb", self.name, 'to', value) + self.yeelight.set_brightness(int(value)) + if (param == 'ct' and self.is_int(value)): + print("Setting temperature of bulb", self.name, 'to', value) + self.yeelight.set_color_temperature(int(value)) + if (param == 'rgb' and self.is_int(value)): + intval = int(value) + Blue = intval & 255 + Green = (intval >> 8) & 255 + Red = (intval >> 16) & 255 + print("Setting rgb of bulb", self.name, 'to', value) + self.yeelight.set_rgb_color(Red, Green, Blue) + except Exception as e: + print ('Error while set value of bulb', self.name , ' error:', e) \ No newline at end of file diff --git a/src/main.py b/src/main.py index e273d57..df7e12a 100644 --- a/src/main.py +++ b/src/main.py @@ -3,72 +3,13 @@ import paho.mqtt.client as mqtt import os import time import threading - -class LightBulbState: - bright = 0 - color_temperature = 0 - status = "off" - rgb = 0 - - ip = "" - name = "" - yeelight = None # yeelight object - - def __init__(self, ip, yeelightObj): - self.yeelight = yeelightObj - self.name = yeelightObj.__name__ - self.ip = ip - - def update_properties(self, force = False): - if (force): - self.yeelight.refresh_property() - prop = self.yeelight.get_all_properties() - # print(prop) - self.bright = prop["bright"] - self.color_temperature = prop["ct"] - self.status = prop["power"] - self.rgb = prop["rgb"] - - def hash(self): - return str(self.bright) + ":" + str(self.color_temperature) + ":" + str(self.status) + ":" + str(self.rgb) - - def is_int(self, x): - try: - tmp = int(x) - return True - except Exception as e: - return False - - def process_command(self, param, value): - try: - if (param == 'status'): - if (value == "on"): - print("Turning on bulb", self.name) - self.yeelight.turn_on() - if (value == "off"): - print("Turning off bulb", self.name) - self.yeelight.turn_off() - if (param == 'bright' and self.is_int(value)): - print("Setting brightness of bulb", self.name, 'to', value) - self.yeelight.set_brightness(int(value)) - if (param == 'ct' and self.is_int(value)): - print("Setting temperature of bulb", self.name, 'to', value) - self.yeelight.set_color_temperature(int(value)) - if (param == 'rgb' and self.is_int(value)): - intval = int(value) - Blue = intval & 255 - Green = (intval >> 8) & 255 - Red = (intval >> 16) & 255 - print("Setting rgb of bulb", self.name, 'to', value) - self.yeelight.set_rgb_color(Red, Green, Blue) - except Exception as e: - print ('Error while set value of bulb', self.name , ' error:', e) +from lightbulbstate import LightBulbState MQTT_SERVER = os.getenv('MQTT_SERVER', "") MQTT_PORT = os.getenv('MQTT_PORT', 1883) MQTT_USER = os.getenv('MQTT_USER', "") MQTT_PASS = os.getenv('MQTT_PASS', "") -QUERY_TIME = 3 +QUERY_TIME = os.getenv('QUERY_TIME', 3) ips = dict({ '192.168.1.51':'main-mono',