def connect_to_device(): device_id = os.getenv('DEVICE_ID') ip_address = os.getenv('IP_ADDRESS') locale_key = os.getenv('LOCALE_KEY') device = tinytuya.BulbDevice(device_id, ip_address, locale_key) device.set_version(3.3) return device
def setDim(self, command): DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) ivr_one = 'percent' percent = int(command.get('value')) def set_percent(self, command): percent = int(command.get('value') * 10) if percent < 0 or percent > 100: LOGGER.error('Invalid Level {}'.format(percent)) else: d.set_brightness_percentage( percent) #d.set_brightness_percentage(percent) self.setDriver('GV3', percent) LOGGER.info('Dimmer Setpoint = ' + str(percent) + 'Level')
def setDim(self, command): DEVICEID = "ebfd4f4263bb769d99zjkq" DEVICEIP = "192.168.1.149" DEVICEKEY = "ec0b2b581a246eab" DEVICEVERS = "us" # Check for environmental variables and always use those if available DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Switch Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) ivr_one = 'percent' percent = int(command.get('value')) def set_percent(self, command): percent = int(command.get('value') * 10) if percent < 0 or percent > 100: LOGGER.error('Invalid Level {}'.format(percent)) else: d.set_brightness_percentage(percent) self.setDriver('GV3', percent)
def start(self): DEVICEID = "ebfd4f4263bb769d99zjkq" DEVICEIP = "192.168.1.149" DEVICEKEY = "ec0b2b581a246eab" DEVICEVERS = "3.3" LOGGER.info(DEVICEID) LOGGER.info(DEVICEIP) LOGGER.info(DEVICEKEY) #LOGGER.info('us') # Check for environmental variables and always use those if available DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TinyTuya - Smart Bulb RGB Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) # Connect to Tuya BulbDevice d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True)
def start(self): DEVICEID = "ebfc16d57ed374932cjqfk" DEVICEIP = "192.168.1.150" DEVICEKEY = "805217605357161b" DEVICEVERS = "3.3" LOGGER.info(DEVICEID) LOGGER.info(DEVICEIP) LOGGER.info(DEVICEKEY) #LOGGER.info('us') # Check for environmental variables and always use those if available DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TinyTuya - Smart Bulb RGB Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) # Connect to Tuya BulbDevice d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True)
def modeOn(self, command): DEVICEID = "ebfc16d57ed374932cjqfk" DEVICEIP = "192.168.1.150" DEVICEKEY = "805217605357161b" DEVICEVERS = "us" # Check for environmental variables and always use those if available DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Switch Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) self.modeOn = int(command.get('value')) self.setDriver('GV4', self.modeOn) if self.modeOn == 0: d.set_mode('colour') LOGGER.info('Colour') elif self.modeOn == 1: d.set_mode('scene') LOGGER.info('Scene') elif self.modeOn == 2: d.set_mode('music') LOGGER.info('Music') elif self.modeOn == 3: d.set_mode('white') LOGGER.info('White')
def setDim(self, command): DEVICEID = "ebfc16d57ed374932cjqfk" DEVICEIP = "192.168.1.150" DEVICEKEY = "805217605357161b" DEVICEVERS = "us" # Check for environmental variables and always use those if available DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Switch Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) ivr_one = 'percent' percent = int(command.get('value')) def set_percent(self, command): percent = int(command.get('value') * 10) if percent < 0 or percent > 100: LOGGER.error('Invalid Level {}'.format(percent)) else: d.set_brightness_percentage( percent) #d.set_brightness_percentage(percent) self.setDriver('GV3', percent) LOGGER.info('Dimmer Setpoint = ' + str(percent) + 'Level')
def modeOn(self, command): DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) self.modeOn = int(command.get('value')) self.setDriver('GV4', self.modeOn) if self.modeOn == 0: d.set_mode('colour') LOGGER.info('Colour') elif self.modeOn == 1: d.set_mode('scene') LOGGER.info('Scene') elif self.modeOn == 2: d.set_mode('music') LOGGER.info('Music') elif self.modeOn == 3: d.set_mode('white') LOGGER.info('White')
def bed_lights(name, *num): global counter for item in jdata["devices"]: if item["name"] == name: break d = tinytuya.BulbDevice(item["id"], item["ip"], item["key"]) d.set_version(float(item["ver"])) d.set_socketPersistent(True) data = d.status() for n in num: if n == 2: d.turn_off() if n == 1: if (data['dps']['1'] == True): ##print("its on Turning off") d.turn_off() elif (data['dps']['1'] == False): ##print("its off Turing on") d.turn_on() if n == 4: # print(counter) d.set_brightness_percentage(counter) d.set_colourtemp_percentage(counter) if counter == 100: counter = 0
def test_set_white(self): d = tinytuya.BulbDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY) d.set_version(3.1) d._send_receive = MagicMock(side_effect=mock_send_receive_set_white) result = d.set_white(255, 255) result = result.decode(mock_byte_encoding) result = json.loads(result) self.assertEqual(result['test_result'], "SUCCESS")
def setSwOff(self, command): DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) LOGGER.info(' Turn Lamp 1 Off') d.turn_off() self.setDriver('GV2', 0)
def colorOn(self, command): DEVICEID = "ebfc16d57ed374932cjqfk" DEVICEIP = "192.168.1.150" DEVICEKEY = "805217605357161b" DEVICEVERS = "us" # Check for environmental variables and always use those if available DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Switch Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) self.colorOn = int(command.get('value')) self.setDriver('GV5', self.colorOn) if self.colorOn == 0: d.set_colour(255, 0, 0) LOGGER.info('Red') elif self.colorOn == 1: d.set_colour(255, 127, 0) LOGGER.info('Orange') elif self.colorOn == 2: d.set_colour(255, 200, 0) LOGGER.info('Yellow') elif self.colorOn == 3: d.set_colour(0, 255, 0) LOGGER.info('Green') elif self.colorOn == 4: d.set_colour(0, 0, 255) LOGGER.info('Blue') elif self.colorOn == 5: d.set_colour(46, 43, 95) LOGGER.info('Indigo') elif self.colorOn == 6: d.set_colour(139, 0, 255) LOGGER.info('Violet') elif self.colorOn == 7: d.set_colour(255, 255, 255) LOGGER.info('White')
def execute(r, g, b): lightObjs = [] # light objects lights = get_all_smart_lights() for light in lights: lightId = light["id"] lightKey = light["key"] lightIp = light["ip"] d = tinytuya.BulbDevice(lightId, lightIp, lightKey) d.set_version(3.3) data = d.status() lightObjs.append(d) for d in lightObjs: # color d.turn_on() d.set_colour(r, g, b)
def setSwOn(self, command): #DEVICEID = "ebfd4f4263bb769d99zjkq" #DEVICEIP = "192.168.1.146" #DEVICEKEY = "ec0b2b581a246eab" #DEVICEVERS = "us" DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) LOGGER.info(' Turn Lamp 1 On') d.turn_on() self.setDriver('GV2', 1)
def setSwOn(self, command): #DEVICEID = "ebfc16d57ed374932cjqfk" #DEVICEIP = "192.168.1.147" #DEVICEKEY = "805217605357161b" #DEVICEVERS = "us" # Check for environmental variables and always use those if available DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) LOGGER.info(' Turn Lamp 1 On') d.turn_on() self.setDriver('GV2', 1)
def colorOn(self, command): DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) self.colorOn = int(command.get('value')) self.setDriver('GV5', self.colorOn) if self.colorOn == 0: d.set_colour(255, 0, 0) LOGGER.info('Red') elif self.colorOn == 1: d.set_colour(255, 127, 0) LOGGER.info('Orange') elif self.colorOn == 2: d.set_colour(255, 200, 0) LOGGER.info('Yellow') elif self.colorOn == 3: d.set_colour(0, 255, 0) LOGGER.info('Green') elif self.colorOn == 4: d.set_colour(0, 0, 255) LOGGER.info('Blue') elif self.colorOn == 5: d.set_colour(46, 43, 95) LOGGER.info('Indigo') elif self.colorOn == 6: d.set_colour(139, 0, 255) LOGGER.info('Violet') elif self.colorOn == 7: d.set_colour(255, 255, 255) LOGGER.info('White')
def setSwOff(self, command): DEVICEID = "ebfc16d57ed374932cjqfk" DEVICEIP = "192.168.1.150" DEVICEKEY = "805217605357161b" DEVICEVERS = "us" # Check for environmental variables and always use those if available DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Lamp Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) LOGGER.info(' Turn Lamp 1 Off') d.turn_off() self.setDriver('GV2', 0)
def setSwOn(self, command): DEVICEID = "ebfd4f4263bb769d99zjkq" DEVICEIP = "192.168.1.149" DEVICEKEY = "ec0b2b581a246eab" DEVICEVERS = "us" # Check for environmental variables and always use those if available DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Lamp Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) LOGGER.info(' Turn Lamp 1 On') d.turn_on() self.setDriver('GV2', 1)
DEVICEIP = "192.168.1.150" #"192.168.1.140" #"192.168.1.142" DEVICEKEY = "805217605357161b" #"cb02297ceb692149" #"805217605357161b" DEVICEVERS = "3.3" # Check for environmental variables and always use those if available DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) print("TinyTuya - Smart Bulb RGB Test [%s]\n" % tinytuya.__version__) print('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) # Connect to Tuya BulbDevice d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) if(DEVICEVERS == '3.3'): # IMPORTANT to always set version d.set_version(3.3) #else: # d.set_version(3.1) # Keep socket connection open between commands d.set_socketPersistent(True) # Show status of device data = d.status() print('\nCurrent Status of Bulb: %r' % data) # Set to full brightness warm white print('\nWarm White Test') d.set_white() time.sleep(2)
def turn_on(name): ip = fg.ip_address(table, name) device_ip = tiny.BulbDevice(ip) device_ip.turn_on()
def turn_off(name): ip = fg.ip_address(table, name) device_ip = tiny.BulbDevice(dev_id="180062414c11ae14f7f9", address=ip) device_ip.turn_off()
def setclrflip(self, command): DEVICEID = "ebfc16d57ed374932cjqfk" DEVICEIP = "192.168.1.150" DEVICEKEY = "805217605357161b" DEVICEVERS = "us" # Check for environmental variables and always use those if available DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) LOGGER.info("TreatLife - Smart Switch Test [%s]\n" % tinytuya.__version__) LOGGER.info('TESTING: Device %s at %s with key %s version %s' % (DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) LOGGER.info('TESTING: Device %s' % (DEVICEIP)) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) # Turn on d.turn_on() self.setDriver('GV2', 1) time.sleep(1) # Dimmer Test LOGGER.info('\nDimmer Control Test') for level in range(11): LOGGER.info(' Level: %d%%' % (level * 10)) d.set_brightness_percentage(level * 10) time.sleep(1) # Colortemp Test LOGGER.info('\nColortemp Control Test (Warm to Cool)') for level in range(11): LOGGER.info(' Level: %d%%' % (level * 10)) d.set_colourtemp_percentage(level * 10) time.sleep(1) # Flip through colors of rainbow - set_colour(r, g, b): LOGGER.info('\nColor Test - Cycle through rainbow') rainbow = { "red": [255, 0, 0], "orange": [255, 127, 0], "yellow": [255, 200, 0], "green": [0, 255, 0], "blue": [0, 0, 255], "indigo": [46, 43, 95], "violet": [139, 0, 255] } for x in range(2): for i in rainbow: r = rainbow[i][0] g = rainbow[i][1] b = rainbow[i][2] LOGGER.info(' %s (%d,%d,%d)' % (i, r, g, b)) d.set_colour(r, g, b) time.sleep(2) LOGGER.info('') # Turn off d.turn_off() time.sleep(1) # Random Color Test d.turn_on() LOGGER.info('\nRandom Color Test') for x in range(10): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) LOGGER.info(' RGB (%d,%d,%d)' % (r, g, b)) d.set_colour(r, g, b) time.sleep(2) # Test Modes LOGGER.info('\nTesting Bulb Modes') LOGGER.info(' Colour') d.set_mode('colour') time.sleep(2) LOGGER.info(' Scene') d.set_mode('scene') time.sleep(2) LOGGER.info(' Music') d.set_mode('music') time.sleep(2) LOGGER.info(' White') d.set_mode('white') time.sleep(2) # Turn off d.turn_off() self.setDriver('GV2', 0) time.sleep(1) LOGGER.info('\nDone')
def setclrflip(self, command): DEVICEID = self.DEVICEID DEVICEIP = self.DEVICEIP DEVICEKEY = self.DEVICEKEY DEVICEVERS = self.DEVICEVERS DDEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) d = tinytuya.BulbDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) d.set_socketPersistent(True) # Turn on d.turn_on() self.setDriver('GV2', 1) time.sleep(1) # Dimmer Test LOGGER.info('\nDimmer Control Test') for level in range(11): LOGGER.info(' Level: %d%%' % (level * 10)) d.set_brightness_percentage(level * 10) time.sleep(1) # Colortemp Test LOGGER.info('\nColortemp Control Test (Warm to Cool)') for level in range(11): LOGGER.info(' Level: %d%%' % (level * 10)) d.set_colourtemp_percentage(level * 10) time.sleep(1) # Flip through colors of rainbow - set_colour(r, g, b): LOGGER.info('\nColor Test - Cycle through rainbow') rainbow = { "red": [255, 0, 0], "orange": [255, 127, 0], "yellow": [255, 200, 0], "green": [0, 255, 0], "blue": [0, 0, 255], "indigo": [46, 43, 95], "violet": [139, 0, 255] } for x in range(2): for i in rainbow: r = rainbow[i][0] g = rainbow[i][1] b = rainbow[i][2] LOGGER.info(' %s (%d,%d,%d)' % (i, r, g, b)) d.set_colour(r, g, b) time.sleep(2) LOGGER.info('') # Turn off d.turn_off() time.sleep(1) # Random Color Test d.turn_on() LOGGER.info('\nRandom Color Test') for x in range(10): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) LOGGER.info(' RGB (%d,%d,%d)' % (r, g, b)) d.set_colour(r, g, b) time.sleep(2) # Test Modes LOGGER.info('\nTesting Bulb Modes') LOGGER.info(' Colour') d.set_mode('colour') time.sleep(2) LOGGER.info(' Scene') d.set_mode('scene') time.sleep(2) LOGGER.info(' Music') d.set_mode('music') time.sleep(2) LOGGER.info(' White') d.set_mode('white') time.sleep(2) # Turn off d.turn_off() self.setDriver('GV2', 0) time.sleep(1) LOGGER.info('\nDone')
def wizard(color=True): ########################################################################################### CUSTOM PARAMETERS ################################################################################################################## ####################################### Loads as custom parameters in Node Server ############################### config = {} config['apiKey'] = "txejpdfda9iwmn5cg2es" config['apiSecret'] = "46d6072ffd724e0ba5ebeb5cc6b9dce9" config['apiRegion'] = 'us' config['apiDeviceID'] = "017743508caab5f0973e" needconfigs = True print('') print('TreatLife Device Light Discovery') print('') print('Authentication' + ' [%s]' % (tinytuya.version)) ##print## 2 if (config['apiKey'] != '' and config['apiSecret'] != '' and config['apiRegion'] != '' and config['apiDeviceID'] != ''): needconfigs = False answer = 'Y' #input(subbold + ' Use existing credentials ' + # normal + '(Y/n): ') if ('Y'[0:1].lower() == 'n'): needconfigs = True KEY = config['apiKey'] SECRET = config['apiSecret'] DEVICEID = config['apiDeviceID'] REGION = config['apiRegion'] # us, eu, cn, in LANG = 'us' # en or zh # Get Oauth Token from tuyaPlatform uri = 'token?grant_type=1' response_dict = tuyaPlatform(REGION, KEY, SECRET, uri) token = response_dict['result']['access_token'] # Get UID from sample Device ID uri = 'devices/%s' % DEVICEID response_dict = tuyaPlatform(REGION, KEY, SECRET, uri, token) uid = response_dict['result']['uid'] # Use UID to get list of all Devices for User uri = 'users/%s/devices' % uid json_data = tuyaPlatform(REGION, KEY, SECRET, uri, token) # Filter to only Name, ID and Key tuyadevices = [] for i in json_data['result']: item = {} item['name'] = i['name'].strip() item['id'] = i['id'] item['key'] = i['local_key'] item['ip'] = i['ip'] ####Added IP tuyadevices.append(item) # Display device list #print("\n\n" + "Device Listing\n") output = json.dumps(tuyadevices, indent=4) # sort_keys=True) ##print("\n\n" + "Hello Here's are the Devices with INTERNET ACTIVE IP ADDRESSES \n\n " + output) ######################### Device Switch Poller ################################### # # # Using devId to poll Switches with lights or should i use key length??????#### # # # NEED TO GRAB SWITCH DEVICES AND MAKE THEM SEPARATE NODES FOR EACH SWITCH/LIGHT # # # ######################### By-Pass Data Input ##################################### if ('Y'[0:1].lower() != 'n'): # Scan network for devices and provide polling data ###print(normal + "\nScanning local network for Tuya devices...") devices = tinytuya.deviceScan(False, 20) #### changed 20 to 1 #print(" %s%s local devices discovered%s" % # ( len(devices))) print("") def getIP(d, gwid): for ip in d: if (gwid == d[ip]['gwId']): return (ip, d[ip]['version']) return (0, 0) polling = [] print("Polling TreatLife Light Devices...\n") for i in tuyadevices: item = {} name = i['name'] (ip, ver) = getIP(devices, i['id']) ## 'id' item['name'] = name item['ip'] = ip item['ver'] = ver item['id'] = i['id'] item['key'] = i['key'] if (ip == 0): #print(" %s[%s] - %s%s - %sError: No IP found%s" % # (name, ip, alert, normal)) pass else: try: d = tinytuya.OutletDevice(i['id'], ip, i['key']) if ver == "3.3": d.set_version(3.3) data = d.status() if 'dps' in data: item['devId'] = data #state = alertdim + "Off" + dim try: if '20' in data['dps'] or '20' in data[ 'devId']: # if '1' to '20' for all Devices #state = "On" #print(" %s[%s] - %s%s - %s - DPS: %r" % # (name, ip, state, data['dps'])) print( "\nEACH TREATLIFE LIGHT TO NODE WITH ADDNODE FROM HERE!!!" ) ########################## addNode HERE!! ###################################################################### print("%-35.35s %-24s %-16s %-17s %-5s" % (item["name"], item["id"], item["ip"], item["key"], item["ver"])) else: pass except: pass else: pass except: pass polling.append(item) # for loop ###################################################### JSON STATUS ################################################### # Save polling data snapsot current = { 'timestamp': time.time(), 'devices': polling } #current = {'timestamp' : time.time(), 'devices' : polling} output = json.dumps( current, indent=4, ) #output = json.dumps(current, indent=4) print("") ##print("Hello Here's the JSON \n " + output) #Prints output.json print("") ####################################################################################### NODE SWITCH CLASS START HERE ########################################################################################################## #class SwitchNodes(polyinterface.Node): #def __init__(self, controller, primary, address, name): #, ip, id1, key1 # super(SwitchNodes, self).__init__(controller, primary, address, name) ######################## SEPERATE OUT THE SWITCHES THEY HAVE "devId" IN BETWEEN 'DPS' or should I use length???? ############## ######################## Passed to Nodes ############################# print("Currently Passed Name:", item["name"]) DEVICENAME = item["name"] DEVICEID = item["id"] #"017743508caab5f0973e" print("Currently Passed ID:", DEVICEID) DEVICEIP = item["ip"] #"192.168.1.146" print("Currently Passed IP:", DEVICEIP) DEVICEKEY = item["key"] #"e779c96c964f71b2" print("Currently Passed KEY:", DEVICEKEY + "\n") DEVICEVERS = "us" ########################################################################################### NODE SERVER NODES ################################################################################################################# ###################### Node Light by DEVICE Example ######################################### # Check for environmental variables and always use those if available DEVICENAME = os.getenv("DEVICEID", DEVICEID) DEVICEID = os.getenv("DEVICEID", DEVICEID) DEVICEIP = os.getenv("DEVICEIP", DEVICEIP) DEVICEKEY = os.getenv("DEVICEKEY", DEVICEKEY) DEVICEVERS = os.getenv("DEVICEVERS", DEVICEVERS) if data["dps"] == '20': #print("\nTreatLife - Smart Light Test [%s]\n" % tinytuya.__version__) print( '\n\nTESTING NODE Light by DEVICE: Device ', item["name"], '%s at %s with key %s version %s' % (DEVICENAME, DEVICEID, DEVICEIP, DEVICEKEY, DEVICEVERS)) # Connect to the device - replace with real values d = tinytuya.OutletDevice(DEVICEID, DEVICEIP, DEVICEKEY) d.set_version(3.3) # Payload to Cycle Light payload1 = d.generate_payload(tinytuya.CONTROL, {'20': False, '2': 50}) payload2 = d.generate_payload(tinytuya.CONTROL, {'20': True, '2': 50}) if data["dps"] != '1': #if data["dps"] or item["dps"] != '1': TRY WHEN BACK print("\nTest Cycle Light by DEVICE ON") d._send_receive(payload1) print('\nCurrent Status of', item["name"], 'Light: %r' % data) time.sleep(2) print("\n\nTest Cycle Light by DEVICE OFF\n") d._send_receive(payload2) print('\nCurrent Status of', item["name"], 'Light: %r' % data) ###################### Node Light by NAME Example ######################################### # Turn on a device by name #def turn_on(name): # if data["dps"] == '20': # find the right item that matches name # for data['dps'] in item['dps']: # if item["name"] == name: # break print("\nTurning On: %s" % item["name"]) d = tinytuya.BulbDevice(item["id"], item["ip"], item["key"]) d.set_version(float(item["ver"])) d.set_socketPersistent(True) d.turn_off() # Show status of device #data = d.status() print('\nCurrent Status of Bulb: %r' % data) # Turn off a device by name #def turn_off(name): # if data["dps"] == '20': # # find the right item that matches name # for data['dps'] in item['dps']: # if item["name"] == name: # break print("\nTurning Off: %s" % item["name"]) d = tinytuya.BulbDevice(item["id"], item["ip"], item["key"]) d.set_version(float(item["ver"])) d.set_socketPersistent(True) d.turn_on() # Dimmer Test print('\nDimmer Control Test') for level in range(11): print(' Level: %d%%' % (level * 10)) d.set_brightness_percentage(level * 10) time.sleep(1) # Colortemp Test print('\nColortemp Control Test (Warm to Cool)') for level in range(11): print(' Level: %d%%' % (level * 10)) d.set_colourtemp_percentage(level * 10) time.sleep(1) # Flip through colors of rainbow - set_colour(r, g, b): print('\nColor Test - Cycle through rainbow') rainbow = { "red": [255, 0, 0], "orange": [255, 127, 0], "yellow": [255, 200, 0], "green": [0, 255, 0], "blue": [0, 0, 255], "indigo": [46, 43, 95], "violet": [139, 0, 255] } for x in range(2): for i in rainbow: r = rainbow[i][0] g = rainbow[i][1] b = rainbow[i][2] print(' %s (%d,%d,%d)' % (i, r, g, b)) d.set_colour(r, g, b) time.sleep(2) print('') # Turn off d.turn_off() time.sleep(1) # Random Color Test d.turn_on() print('\nRandom Color Test') for x in range(10): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) print(' RGB (%d,%d,%d)' % (r, g, b)) d.set_colour(r, g, b) time.sleep(2) # Test Modes print('\nTesting Bulb Modes') print(' White') d.set_mode('white') time.sleep(2) print(' Colour') d.set_mode('colour') time.sleep(2) print(' Scene') d.set_mode('scene') time.sleep(2) print(' Music') d.set_mode('music') time.sleep(2) d.set_mode(b) #d.set_white() print('\nDimmer Control Test') for level in range(1): print(' Level: %d%%' % (level * 1)) # Dimmer Test print('\nDimmer Control Test') for level in range(5): print(' Level: %d%%' % (level * 1)) d.set_brightness_percentage(level * 1) time.sleep(1) # Power Control Test #print('\nPower Control Test') #print(' Turn off lamp') #d.turn_off() #time.sleep(2) #print(' Turn on lamp') #d.turn_on() #time.sleep(2) # Done print('\nDone wWith Light Color Test') # Who was passed print("\n" + item["name"]) print(item["id"]) print(item["ip"]) print(item["key"] + "\n") print("\nDone.\n") return