add ListLights function

This commit is contained in:
Ryan Cavicchioni 2020-03-06 23:00:21 -06:00
parent 40a7a9d741
commit e39d81f6cc
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 78 additions and 0 deletions

View File

@ -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
}

View File

@ -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")
}

View File

@ -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"})
}