From e39d81f6cc08ff7b95b28e0fca3fbab53e1b972d Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Fri, 6 Mar 2020 23:00:21 -0600 Subject: [PATCH] add ListLights function --- client.go | 18 +++++++++++++++++ endpoints.go | 3 +++ lights.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/client.go b/client.go index 0349327..f169426 100644 --- a/client.go +++ b/client.go @@ -167,3 +167,21 @@ func (c *Client) validateColor(color Color) (*http.Response, error) { return resp, nil } + +func (c *Client) listLights(selector string) (*http.Response, error) { + var ( + err error + req *http.Request + resp *http.Response + ) + + if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil { + return nil, err + } + + if resp, err = c.Client.Do(req); err != nil { + return nil, err + } + + return resp, nil +} diff --git a/endpoints.go b/endpoints.go index 330df99..9523332 100644 --- a/endpoints.go +++ b/endpoints.go @@ -17,6 +17,9 @@ var ( EndpointState = func(selector string) string { return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector)) } + EndpointListLights = func(selector string) string { + return BuildURL(Endpoint, fmt.Sprintf("/lights/%s", selector)) + } EndpointStates = func() string { return BuildURL(Endpoint, "/lights/states") } diff --git a/lights.go b/lights.go index 0909eea..aba4306 100644 --- a/lights.go +++ b/lights.go @@ -15,6 +15,44 @@ const ( type ( Status string + Selector struct { + ID string `json:"id"` + Name string `json:"name"` + } + + Product struct { + Name string `json:"name"` + Identifier string `json:"identifier"` + Company string `json:"company"` + Capabilities Capabilities `json:"capabilities"` + } + + Capabilities struct { + HasColor bool `json:"has_color"` + HasVariableColorTemp bool `json:"has_variable_color_temp"` + HasIR bool `json:"has_ir"` + HasChain bool `json:"has_chain"` + HasMultizone bool `json:"has_multizone"` + MinKelvin float64 `json:"min_kelvin"` + MaxKelvin float64 `json:"max_kelvin"` + } + + Light struct { + Id string `json:"id"` + UUID string `json:"uuid"` + Label string `json:"label"` + Connected bool `json:"connected"` + Power string `json:"power"` + Color HSBKColor `json:"color"` + Brightness float64 `json:"brightness"` + Effect string `json:"effect"` + Group Selector `json:"group"` + Location Selector `json:"location"` + Product Product `json:"product"` + LastSeen string `json:"last_seen"` + SecondsLastSeen float64 `json:"seconds_last_seen"` + } + State struct { Power string `json:"power,omitempty"` Color Color `json:"color,omitempty"` @@ -109,6 +147,25 @@ func (c *Client) Toggle(selector string, duration float64) (*Response, error) { return s, nil } +func (c *Client) ListLights(selector string) ([]Light, error) { + var ( + err error + s []Light + resp *http.Response + ) + + if resp, err = c.listLights(selector); err != nil { + return nil, err + } + defer resp.Body.Close() + + if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { + return nil, err + } + + return s, nil +} + func (c *Client) PowerOff(selector string) (*Response, error) { return c.SetState(selector, State{Power: "off"}) }