constconnectedDevices=newSet<Device>();/* Function will be called several times with overlapping devices, * but yeelight will return already existing ones if you'll try to connect them again. * The 0st element is always the newest discovered. */constadd=(data: DiscoveryData[],yeelight: Yeelight): void=>{constdevice=yeelight.connectDiscovered(data[0]);connectedDevices.add(device);console.log('Connected devices:');for(constdeviceofconnectedDevices){console.log(`${device.ip}`);}};conststart=()=>{constyeelight=newYeelight();yeelight.on('discovery',(data)=>add(data,yeelight));};start();
Cycle through all basic colors
constip='192.168.88.22';consteffect=(device: Device)=>{device.command(CommandLibrary.powerOn(PowerOnMode.RGB));// adjustColor will switch the light to one of basic predefined colors.// 5000 is tansition duration in mssetInterval(()=>device.command(CommandLibrary.adjustColor(5000)),5000);};constprintColor=(data: DeviceState): void=>{constcolor=data.rgb?.toString(16).padStart(6,'0');console.log(`Color: #${color}`);};conststart=()=>{constyeelight=newYeelight();constdevice=yeelight.connectOne(ip);device.on('connect',()=>effect(device)).on('update',printColor);};start();
Do some random stuff
constip='192.168.88.22';constyeelight=newYeelight({disableDiscovery: true});constdevice=yeelight.connectOne(ip);// do nothing for ms millisecondsconstwait=async(ms: number)=>newPromise((resolve)=>setTimeout(resolve,ms));// do some random stuffconstsequence=async()=>{device.command(CommandLibrary.powerOn());awaitwait(500);device.command(CommandLibrary.setBright(100));device.command(CommandLibrary.setRgb(0x0099ff));awaitwait(1000);device.command(CommandLibrary.setBright(50));device.command(CommandLibrary.setRgb(0xff9900));awaitwait(1000);device.command(CommandLibrary.powerOff);awaitwait(50);device.disconnect();awaitwait(5000);device.connect();};device.on('connect',sequence);device.on('connect',()=>console.log('connected'));device.on('disconnect',()=>console.log('disconnected'));