Why?
I recently setup some Govee smart LED strips around my desk and they've been amazing! I have been controlling the lights primarily through my Alexa, which has worked well so far.
However, lately I have been trying to focus on using the keyboard for as much as possible to become more efficient. When I discovered that Govee has a public API, I thought it would be nice to build a command line application to control the lights. In the process, I created a Node.js wrapper for the API and decided to publish it!
Installing
You can install the package using:
npm install govee-ts
Getting an API key
Before you get started, you must request an API key using the Govee Home app. Once you download it:
- Go to your profile tab
- Go to the "About Us" section, then tap "Apply for API key"
- Enter your name and a reason that you wish to use the API. It can be a brief description saying you want to control your lights programmatically.
- Shortly after, check your email. There should be an email from Govee containing your API Key and the API documentation.
Create the client
Let's take a look at how we can instantiate a client:
// If using ES6 import syntax
import Govee from 'govee-ts';
// If using CommonJS require() syntax
const Govee = require('govee-ts').default;
const client = new Govee('replace-with-your-api-key');
Getting a light
My main goal was to make the API simple to use. The Govee API uses the MAC address and model number to identify the light to control, I wanted to provide an easier way for users to specify the desired device.
First, we need to get a list of all devices:
const devices = await client.getDevices();
Then, we can use the device name to access a specific smart light:
const device = client.getDevice('deviceName');
Controlling a light
Once you have a device object, you can use its methods to turn it on/off, change the color, change the brightness, or change the color temperature:
// Turn the device on
await device.turnOn();
// Turn the device on
await device.turnOff();
// Set the device's brightness
await device.setBrightness(50); // from 1-100
// Set the color
await device.setHexColor('#a02020');
// or using RGB
await device.setRGBColor({ r: 160, g: 32, b: 32 });
// Set the device's color temperature
await device.setColorTemperature(6000);
// Get device state
await device.getState();
Make sure the command is supported by the device you are trying to control. You can see the supported commands using device.supportCmds
.
How does it work?
Behind the scenes, the library uses Axios to send requests to the API. That's about it! Quite simple honestly.
Source
Take a look at the GitHub if you'd like to see the source code for the project!