Giter Club home page Giter Club logo

lifx-library's Introduction

Lifx-Library

C# .NET Standard Library to control the Lifx smart LED bulbs over the cloud

This project is a wrapper library that contains functionalities from the official Lifx API https://api.developer.lifx.com/reference/introduction

Use

Toggle Methods

Create an instance from the LifxBulbs class by providing the TOKENAPIKEY and the label name of the LED bulb as parameters.

using LifxLibrary;

LifxBulbs bedroom = new LifxBulbs("tokenKey", "Bedroom");

//synchronous
bedroom.LightToggle(3); //toggle with 3 seconds duration

//async
await bedroom.LightToggleAsync(3); //async toggle with 3 seconds duration

NOTE

It is not obligatory to specify a label name in the class constructor when using the SweepToggle methods. These methods will execute a general toggle operation across all connected devices.

using LifxLibrary;

LifxBulbs devices = new LifxBulbs("tokenKey");

//synchronous
devices.SweepToggle();

//async
await devices.SweepToggleAsync();

Power Methods

The PutPower methods allow you to change the state of the bulbs, setting them to either on or off, and providing an optional duration time parameter ranging from 0 to 100 seconds.

using LifxLibrary;

LifxBulbs bedroom = new LifxBulbs("tokenKey", "Bedroom");

//synchronous
bedroom.PutPower("on");

bedroom.PutPower("off");

bedroom.PutPower("on", 5); //with 5 seconds duration time

//async
await bedroom.PutPowerAsync("off");

Brightness Methods

The PutBrightness methods takes an integer parameter ranging from 0 to 100 to set the intensity of the brightness.

using LifxLibrary;

LifxBulbs bedroom = new LifxBulbs("tokenKey", "Bedroom");

//synchronous
bedroom.PutBrightness(50); //set the brightness at 50%

//async
await bedroom.PutBrightnessAsync(50);

Color Methods

The PutColor methods can receive a series of string values as parameter to define the color, brightness, saturation, and other attributes.

using LifxLibrary;

LifxBulbs bedroom = new LifxBulbs("tokenKey", "Bedroom");

//hexadecimal
bedroom.PutColor("#0000FF"); //blue color

//RGB
bedroom.PutColor("rgb:255,0,0"); //red color

//plain text
bedroom.PutColor("white"); //white color

//async
await bedroom.PutColorAsync("hue:120 saturation:1.0 brightness:0.5"); //Deep green 50% brightness

For a more detailed guide on defining colors, please visit the official Lifx API documentation at https://api.developer.lifx.com/reference/colors.

MultiUse Methods

The MultiUse methods accept a set of optional parameters to carry out diverse actions on the LED bulbs, such as changing the color, brightness, power, duration time, and activating fast mode.

NOTE

The fast mode execute the query fast, without initial state checks and wait for no results.

using LifxLibrary;

LifxBulbs bedroom = new LifxBulbs("tokenKey", "Bedroom");

//synchronous
bedroom.MultiUse("on", "blue", 50, 6, true); //power on, color blue, 50% brightness, 6 seconds duration with fast mode activated

//other ways to use it
bedroom.MultiUse(power:"on", color:"blue", brightness:100, duration:6, fast:true);

//duration time is 0 by default and fast mode is false by default
bedroom.MultiUse(power:"on", color:"white", brightness:80); 

//async
await bedroom.MultiUseAsync("off");

await bedroom.MultiUseAsync("on", "orange");

The LightSearcher class

LightSearcher Methods

The LightSearcher class is a static class that contains static async methods that helps to discover the LED bulbs and retrieve their properties such as LED bulb name, power status, connection status, brightness level, saturation level and more.

ShowConnectedDevicesAsync()

The ShowConnectedDevicesAsync method returns a list of string object with the label names of all connected devices.

using LifxLibrary;

//set the tokenkey
LightSearcher.SetTokenKey("tokenkey");

var devices = await LightSearcher.ShowConnectedDevicesAsync();

foreach(var device in devices)
{
    Console.WriteLine(device);
}

GetNamesAsync()

The GetNamesAsync method returns a list of string object with the label names of all devices linked to your account, regardless of their connection status or power status.

using LifxLibrary;

//set the tokenkey
LightSearcher.SetTokenKey("tokenkey");

var devices = await LightSearcher.GetNamesAsync();

foreach(var device in devices)
{
    Console.WriteLine(device);
}

ShowStateAsync()

The ShowStateAsync method returns a BulbState object with the power status, connection status, LED label name, brightness level, saturation level, UUID, ID, and HUE level.

using LifxLibrary;

//set the tokenkey
LightSearcher.SetTokenKey("tokenkey");

BulbState bedroom = await LightSearcher.ShowStateAsync("Bedroom");

Console.WriteLine(bedroom.Power); //the response would be off or on

Console.WriteLine(bedroom.Connected); //the response would be true or false

Console.WriteLine(bedroom.Brightness); //brightness level

Console.WriteLine(bedroom.Saturation); //saturation level

Console.WriteLine(bedroom.Id); //bulb ID

The LightGroup class

The LightGroup class contains methods to control a set of LED bulbs that belong in a specific group.

NOTE

Async and Synchronous methods available in the LightGroup class.

Toggle methods Brightness methods Color methods MultiUse methods

using LifxLibrary;

LightGroup bulbs = new LightGroup("tokenKey", "Kitchen"); //tokenKey and group name

bulbs.SweepToggle();

await bulbs.PutColorAsync("blue");

Please report any bugs [email protected]

lifx-library's People

Contributors

stanlyrod avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

lifx-library's Issues

JSON crash when trying to get ShowConnectedDevicesAsync

Trying to get the list of connected devices.
LightSearcher.SetTokenKey(tokenKey); var devices = await LightSearcher.ShowConnectedDevicesAsync()
when I get this JSON error:

System.Text.Json.JsonException: 'The JSON value could not be converted to System.Int32. Path: $[2].color.hue | LineNumber: 91 | BytePositionInLine: 18.'
Inner exception:
FormatException: Either the JSON value is not in a supported format, or is out of bounds for an Int32.

I have a few A19 bulbs, but then I also have three original LIFX bulbs from the very first Kickstarter. Not sure, but could they be the cause? But I can control them when going directly to them.
(Model: LIFX Original, Firmware: v2.1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.