Skip to content

Examples

Tristan Possessky edited this page Feb 3, 2021 · 2 revisions

Code Examples

The following will provide an overview of the implementation of the GoveeController class into a Java project.

Initial Setup

Before beginning make sure you have an API key provided by Govee. Instructions for getting this API can be found in the readme.md.

Creating a New GoveeController Object

The GoveeController has a single constructor that accepts a String representing your API key. This value will be stored for all future API calls.

GoveeController controller = new GoveeController("API_KEY");

Current API Usage Rate

Govee sets a limit of 100 API calls per minute. To check your current usage you can call getRateLimit(). This will return a map containing useful information regarding your current usage of the API.

Map<String, List<String>> map = controller.getRateLimit();
for(Map.Entry<String, List<String>> entry : map.entrySet()
    System.out.println(entry.getKey() + "" + entry.getValue());

Choosing a Device to Control

Once your main controller object is instantiated, the first call you'll need to make is to the getConnectedDevices() method. This will return a JSONObject containing the response from Govee's API. It'll contain the MAC addresses and Device Models as well as the custom names of your devices.

JSONObject response = controller.getConnectedDevices();

You'll either need to read the JSON manually or scrape it for the Device and Model fields.

Getting Device Status

Govee allows for the retrieval of specific device information. This is implemented via the getDeviceState() method. The method can be called via the following

controller.getDeviceState(macAddress, model);

Turning a Device On/Off

The GoveeController class has an included enum called Device which contains the values ON and OFF. This is the first parameter in the turnDeviceOnOff() method.

controller.turnDeviceOnOff(Device.ON, macAddress, model);

Setting the Brightness of Your Device

To set the brightness of your device use the setBrightness() method. The brightness value must be an integer between 0-100.

controller.setBrightness(75, macAddress, model);

Setting the Color Temp

White lights often are described as having a "temperature". This setting is available through the API via the setColorTem() method. The temp must be an integer between 2000-9000

controller.setColorTem(5000, macAddress, model);

Setting the RGB Values

To set the RGB values of your lights, use the setRGB() method. Each RGB value must be between 0-255.

//All Red
controller.setRGB(255, 0, 0, macAddress, model);
//All Green
controller.setRGB(0, 255, 0, macAddress, model);
//All Blue
controller.setRGB(0, 0, 255, macAddress, model);

Changing Your API Key

If you for some reason have multiple API keys for different devices, the updateAPIKey() will allow you to change the API key used when making requests.

controller.updateAPIKey("NEW_API_KEY");