GoveeLights.Api (Govee Lights v0.2.0)

View Source

Elixir client for the Govee Developer API.

This module is the main entry point for interacting with Govee devices. It provides functions to:

  • list devices associated with your account
  • retrieve the current state of a device
  • send control commands (power, brightness, color, temperature)

All data returned by this module is converted into domain structs (GoveeLights.Device and GoveeLights.Device.State) instead of raw API maps.


Authentication

You must provide a Govee API key, either as an environment variable:

export GOVEE_API_KEY="your_api_key"

or via application configuration:

config :govee_lights, api_key: "your_api_key"

You can request an API key from Govee at: https://developer.govee.com/reference/apply-you-govee-api-key


Devices

Retrieve all devices linked to your account:

iex> {:ok, devices} = GoveeLights.Api.devices()
iex> Enum.all?(devices, &match?(%GoveeLights.Device{}, &1))
true

Each device contains basic metadata and a normalized state field.


Device state

Fetch the current state of a device by its identifier and model:

iex> {:ok, state} =
...>   GoveeLights.Api.device_state("AA:BB:CC:DD:EE:FF:11:22", "H6008")
iex> state.on
true
iex> state.brightness
10
iex> state.color
%{r: 36, g: 242, b: 156}

The returned value is a %GoveeLights.Device.State{} struct with normalized fields. Missing information is represented as :unknown.


Device control

Send commands to a device using its identifier and model.

Turn a device on or off:

iex> GoveeLights.Api.turn_on("AA:BB:CC:DD:EE:FF:11:22", "H6008")
{:ok, :updated}

iex> GoveeLights.Api.turn_off("AA:BB:CC:DD:EE:FF:11:22", "H6008")
{:ok, :updated}

Set brightness (0–100):

iex> GoveeLights.Api.set_brightness("AA:BB:CC:DD:EE:FF:11:22", "H6008", 25)
{:ok, :updated}

Set color temperature (Kelvin):

iex> GoveeLights.Api.set_temperature("AA:BB:CC:DD:EE:FF:11:22", "H6008", 2700)
{:ok, :updated}

Set RGB color:

iex> GoveeLights.Api.set_color(
...>   "AA:BB:CC:DD:EE:FF:11:22",
...>   "H6008",
...>   %{r: 255, g: 0, b: 0}
...> )
{:ok, :updated}

Bang functions

For convenience, most functions also have a bang (!) variant that returns the successful value directly or raises GoveeLights.Api.Error on failure.

Example:

iex> state =
...>   GoveeLights.Api.device_state!(
...>     "AA:BB:CC:DD:EE:FF:11:22",
...>     "H6008"
...>   )
iex> state.on
true

Errors

Non-bang functions return structured error tuples:

  • {:http_error, reason} – network or HTTP client failure
  • {:decode_error, data} – unexpected API response shape
  • {:device_error, raw, reason} – invalid device payload
  • {:state_error, raw, reason} – invalid state payload
  • {:command_failed, command, value, code, message} – API rejected a command
  • {:invalid_command, command, value} – invalid command arguments

Bang functions raise GoveeLights.Api.Error, which contains the original error tuple in exception.reason.


HTTP client configuration

By default, this module uses GoveeLights.HTTPClient (based on Req). You can provide a custom HTTP client by configuring:

config :govee_lights, http_client: MyHTTPClient

The client module must implement the GoveeLights.HTTPClient behaviour.


This library is unofficial and not affiliated with Govee.

Summary

Functions

Retrieve the current state of a device as %State{}.

Retrieve all devices associated with your Govee account as %Device{} structs.

Same as devices/0, but raises GoveeLights.Api.Error on failure.

Set the brightness (0..100).

Set the color of a device using RGB.

Set the color temperature (Kelvin).

Turn off a device.

Turn on a device.

Types

command_result()

@type command_result() :: :updated

error_reason()

@type error_reason() ::
  {:config_error, String.t()}
  | {:http_error, term()}
  | {:decode_error, term()}
  | {:device_error, map(), GoveeLights.Device.new_error()}
  | {:state_error, map(), GoveeLights.Device.State.new_error()}
  | {:command_failed, String.t(), term(), term(), term()}
  | {:invalid_command, String.t(), term()}

Functions

device_state(device_id, model)

@spec device_state(String.t(), String.t()) ::
  {:ok, GoveeLights.Device.State.t()} | {:error, error_reason()}

Retrieve the current state of a device as %State{}.

This endpoint returns a somewhat awkward "properties" list from Govee; this function normalizes it into your State struct.

Examples

iex> {:ok, state} =
...>   GoveeLights.Api.device_state("AA:BB:CC:DD:EE:FF:11:22", "H6008")
iex> match?(%GoveeLights.Device.State{}, state)
true

device_state!(device_id, model)

@spec device_state!(String.t(), String.t()) :: GoveeLights.Device.State.t()

Same as device_state/2, but raises GoveeLights.Api.Error on failure.

devices()

@spec devices() :: {:ok, [GoveeLights.Device.t()]} | {:error, error_reason()}

Retrieve all devices associated with your Govee account as %Device{} structs.

Examples

iex> {:ok, devices} = GoveeLights.Api.devices()
iex> Enum.all?(devices, &match?(%GoveeLights.Device{}, &1))
true

devices!()

@spec devices!() :: [GoveeLights.Device.t()]

Same as devices/0, but raises GoveeLights.Api.Error on failure.

set_brightness(device_id, model, value)

@spec set_brightness(String.t(), String.t(), integer()) ::
  {:ok, command_result()} | {:error, error_reason()}

Set the brightness (0..100).

Examples

iex> GoveeLights.Api.set_brightness("AA:BB:CC:DD:EE:FF:11:22", "H6008", 10)
{:ok, :updated}

set_brightness!(device_id, model, value)

@spec set_brightness!(String.t(), String.t(), integer()) :: command_result()

Bang variant of set_brightness/3.

set_color(device_id, model, rgb)

@spec set_color(String.t(), String.t(), map()) ::
  {:ok, command_result()} | {:error, error_reason()}

Set the color of a device using RGB.

The RGB map must contain integer :r, :g, :b values in 0..255.

Examples

iex> GoveeLights.Api.set_color("AA:BB:CC:DD:EE:FF:11:22", "H6008", %{r: 255, g: 0, b: 0})
{:ok, :updated}

iex> GoveeLights.Api.set_color("AA:BB:CC:DD:EE:FF:11:22", "H6008", %{r: 999, g: 0, b: 0})
{:error, {:invalid_command, "color", %{r: 999, g: 0, b: 0}}}

set_color!(device_id, model, rgb)

@spec set_color!(String.t(), String.t(), map()) :: command_result()

Bang variant of set_color/3.

set_temperature(device_id, model, value)

@spec set_temperature(String.t(), String.t(), integer()) ::
  {:ok, command_result()} | {:error, error_reason()}

Set the color temperature (Kelvin).

Govee devices support different ranges. You can inspect the range in the device properties returned by devices/0.

Examples

iex> GoveeLights.Api.set_temperature("AA:BB:CC:DD:EE:FF:11:22", "H6008", 2700)
{:ok, :updated}

set_temperature!(device_id, model, value)

@spec set_temperature!(String.t(), String.t(), integer()) :: command_result()

Bang variant of set_temperature/3.

turn_off(device_id, model)

@spec turn_off(String.t(), String.t()) ::
  {:ok, command_result()} | {:error, error_reason()}

Turn off a device.

Examples

iex> GoveeLights.Api.turn_off("AA:BB:CC:DD:EE:FF:11:22", "H6008")
{:ok, :updated}

turn_off!(device_id, model)

@spec turn_off!(String.t(), String.t()) :: command_result()

Bang variant of turn_off/2.

turn_on(device_id, model)

@spec turn_on(String.t(), String.t()) ::
  {:ok, command_result()} | {:error, error_reason()}

Turn on a device.

Examples

iex> GoveeLights.Api.turn_on("AA:BB:CC:DD:EE:FF:11:22", "H6008")
{:ok, :updated}

turn_on!(device_id, model)

@spec turn_on!(String.t(), String.t()) :: command_result()

Bang variant of turn_on/2.