def set_light_name(ip, light_model, light_id): print("Name not defined for Yeelight {}, ID: {}".format(light_model, light_id)) # Identify light for user try: bulb = Bulb(ip) bulb.turn_off() bulb.effect = "sudden" bulb.turn_on() bulb.set_brightness(100) bulb.set_rgb(0, 0, 255) bulb.effect = "smooth" bulb.duration = 10000 bulb.set_rgb(255, 0, 0) except: print( "Communication failed with Yeelight {}, ID: {}".format( light_model, light_id ) ) return "" # Get user input for light name print("This device will change color from blue to red over 10 seconds.") print("Enter name for this device or press enter to skip it:") input_char = input() if input_char == "": try: bulb.turn_off() except: print( "Communication failed with Yeelight {}, ID: {}".format( light_model, light_id ) ) return "" return "" # Set light name device_name = input_char try: bulb.set_name(device_name) except: print( "Communication failed with Yeelight {}, ID: {}".format( light_model, light_id ) ) return "" return device_name
def __init__(self): self.bulbs = list() for bulb in discover_bulbs(): found_bulb = Bulb(ip=bulb['ip'], port=bulb['port'], model=bulb['capabilities']['model']) if bulb['capabilities']['name'] == '': name = bulb['ip'] found_bulb.set_name(name) else: name = bulb['capabilities']['name'] bulb_name = name self.bulbs.append({ 'bulb': found_bulb, 'name': bulb_name, 'ip': bulb['ip'] })
def init_yeelight(): # Inicializamos la lista de bombillas bulb_list = {} # Buscamos las bombillas print('Looking for bulbs') discovered_bulbs = discover_bulbs() discovered_len = len(discovered_bulbs) if discovered_len <= 0: # Si no se encuentran bombillas informamos al usuario print('No bulbs found') else: #Si se encuentran bombillas, decimos cuantas y las parseamos print('%s bulb%s found.' % (discovered_len,('s' if discovered_len > 1 else ''))) for bulb in discovered_bulbs: ip = bulb["ip"] support = bulb["capabilities"]["support"] model = bulb["capabilities"]["model"] power = bulb["capabilities"]["power"] #Comprobamos que la bombilla tiene nombre if bulb['capabilities']['name']: name = bulb['capabilities']['name'].lower() else: print('------------------------------') print('Bulb IP is: %s' % ip) print('This Yeelight %s has no name' % model) #Si no lo tine asignamos un nombre a la bombilla target_bulb = Bulb(ip) name = input('Enter name of the bulb: ').lower() target_bulb.set_name(name) #Añadimos la bombilla al diciconario bulb_list[name] = YeelightBulb(name,ip,support,model,power) return bulb_list
import paho.mqtt.client as mqtt from yeelight import Bulb from pynput import keyboard import time import ssl ### Setting the lamps lamp_1 = Bulb("192.168.1.16") lamp_2 = Bulb("192.168.1.17") lamp_1.set_name("Lamp 1") lamp_2.set_name("Lamp 2") lamp_1_properties = lamp_1.get_properties( requested_properties=["power", "bright", "rgb", "name"]) lamp_2_properties = lamp_2.get_properties( requested_properties=["power", "bright", "rgb", "name"]) ### Logging function def on_log(client, userdata, level, buf): print("log : " + buf) ### Information about connection ### ==> OK or KO with returned code def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected OK") else: print("Bad connection, returned code : " + str(rc))
#! python import sys, json ip = str(sys.argv[1]) name = str(sys.argv[2]) from yeelight import Bulb from random import random bulb = Bulb(ip) bulb.set_name(name)
class LedLightYeeLight(LedLight): def __init__(self, name, ipAddrOrName=None): if ipAddrOrName is None: ipAddrOrName = name self.bulb = Bulb(ipAddrOrName) self.name = name try: self.bulb.set_name(name) except Exception as exc: print "Caught exception socket.error : %s" % exc # Send an RGB message def sendRGB(self, red, green, blue): try: self.bulb.set_rgb(red, green, blue) except Exception as exc: print "Caught exception socket.error : %s" % exc # Send a white message def sendWhite(self, white): try: self.bulb.set_color_temp(5000) self.bulb.set_brightness(white) except Exception as exc: print "Caught exception socket.error : %s" % exc def turnOff(self): try: return self.bulb.turn_off() except Exception as exc: print "Caught exception socket.error : %s" % exc def turnOn(self): try: self.bulb.turn_on() except Exception as exc: print "Caught exception socket.error : %s" % exc def isOn(self): try: return self.bulb.get_properties()['power'] == 'on' except Exception as exc: print "Caught exception socket.error : %s" % exc def getWhite(self): try: properties = self.bulb.get_properties() if (properties['color_mode'] == '2'): return int(properties['bright']) else: return -1 except Exception as exc: print "Caught exception socket.error : %s" % exc # comment def getRGB(self): try: properties = self.bulb.get_properties() if (properties['color_mode'] == '1'): rgb = int(properties['rgb']) return [(rgb >> 16) & 255, (rgb >> 8) & 255, rgb & 255] else: return -1 except Exception as exc: print "Caught exception socket.error : %s" % exc
#The YeeLight names didn't appear for some reason, so manually added here #Will add a step that makes user register a name with the bulb via UI #bulb = Bulb(ip="192.168.0.102",effect="smooth") #bulb.set_name("livingroom_biglight") #bulb = Bulb(ip="192.168.0.104", effect="smooth") #bulb.set_name("livingroom_biglight") #bulb = Bulb(ip="192.168.0.100",effect="smooth") #bulb.set_name("livingroom_littlelight") #bulb = Bulb("192.168.0.101") #bulb.set_name("bedroom_lucy") #bulb = Bulb("192.168.0.105") #bulb.set_name("office") bulb = Bulb("192.168.0.105") bulb.set_name("office_lamp") #bulb = Bulb("192.168.0.112") #bulb.set_name("bedroom_martin") #id=0x00000000035aba47, ip=192.168.0.102 => Thai lamp #id=0x0000000002be3385, ip=192.168.0.104 => BigLight #id=0x0000000000c9f231, ip=192.168.0.100 => Corner Lamp ##id=0x00000000035abeb6, ip=192.168.0.101 => LucyLamp #id=0x00000000035a0e5d ip=192.168.0.105 => Office #id=0x00000000035a0f28, ip=192.168.0.103 => Bedroom