GoveeLights.Device (Govee Lights v0.2.0)
View SourceRepresents a single Govee light device.
This module defines a typed struct and a smart constructor new/1 that
validates and normalizes input coming from Govee's API responses or internal
code.
Fields
:id– required. Govee device identifier (MAC-like string).:model– required. Govee model identifier (e.g."H6008").:name– optional, user-friendly name for the device (ornil).:state– normalized device state asGoveeLights.Device.State.t(). When no state map is provided, a default state with:unknownvalues is used.:properties– map for additional device properties (defaults to%{}).:controllable– boolean flag indicating whether the device can be controlled by the API (defaults tofalse).
The new/1 function accepts either a map or a keyword list, with
string or atom keys. Unknown keys are ignored. All validation happens
in new/1 / new_internal/1.
Examples
Minimal valid device using atom keys:
iex> {:ok, device} =
...> GoveeLights.Device.new(%{id: "AA:BB:CC:DD:EE:FF:11:22", model: "H6008"})
iex> device.id
"AA:BB:CC:DD:EE:FF:11:22"
iex> device.model
"H6008"
iex> device.name
nil
iex> match?(%GoveeLights.Device.State{}, device.state)
true
iex> device.state.on
:unknown
iex> device.properties
%{}
iex> device.controllable
falseUsing a keyword list (e.g. from internal code):
iex> {:ok, device} =
...> GoveeLights.Device.new(
...> id: "AA:BB:CC:DD:EE:FF:11:22",
...> model: "H6008",
...> name: "Backrooms",
...> controllable: true
...> )
iex> device.name
"Backrooms"
iex> device.controllable
trueUsing string keys (e.g. decoded JSON from the Govee API):
iex> {:ok, device} =
...> GoveeLights.Device.new(%{
...> "id" => "AA:BB:CC:DD:EE:FF:11:22",
...> "model" => "H6008",
...> "name" => "Bedroom",
...> "state" => %{
...> on: true,
...> brightness: 42
...> }
...> })
iex> device.id
"AA:BB:CC:DD:EE:FF:11:22"
iex> device.model
"H6008"
iex> device.name
"Bedroom"
iex> device.state.on
true
iex> device.state.brightness
42Error when required fields are missing:
iex> GoveeLights.Device.new(%{model: "H6008"})
{:error, {:missing, :id}}
iex> GoveeLights.Device.new(%{id: "AA:BB:CC:DD:EE:FF:11:22"})
{:error, {:missing, :model}}Error when passing a non-keyword list:
iex> GoveeLights.Device.new([1, 2, 3])
{:error, {:invalid_attrs, :expected_keyword_list}}Error when the input is neither a map nor a keyword list:
iex> GoveeLights.Device.new("not-a-map-or-keyword-list")
{:error, {:invalid_attrs, :expected_map_or_keyword_list}}
Summary
Functions
Builds a new Elixir.GoveeLights.Device struct from a map or keyword list.
Types
Functions
Builds a new Elixir.GoveeLights.Device struct from a map or keyword list.
Accepted inputs:
- a map with atom and/or string keys
- a keyword list (list of
{atom, value}pairs)
Validation rules:
:idand:modelmust be present and non-empty strings.:nameis optional and may benilor a (possibly empty) string.:statemay be omitted or provided as a map; when present it must be a map suitable forGoveeLights.Device.State.new/1.:propertiesmust be a map when present; otherwise defaults to%{}.:controllablemust be boolean when present; otherwise defaults tofalse.
On success, returns {:ok, %Elixir.GoveeLights.Device{}}. On failure, returns
{:error, reason} where reason is one of the new_error/0 variants.