add ListLights function
This commit is contained in:
parent
40a7a9d741
commit
e39d81f6cc
18
client.go
18
client.go
@ -167,3 +167,21 @@ func (c *Client) validateColor(color Color) (*http.Response, error) {
|
|||||||
|
|
||||||
return resp, nil
|
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
|
||||||
|
}
|
||||||
|
@ -17,6 +17,9 @@ var (
|
|||||||
EndpointState = func(selector string) string {
|
EndpointState = func(selector string) string {
|
||||||
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector))
|
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 {
|
EndpointStates = func() string {
|
||||||
return BuildURL(Endpoint, "/lights/states")
|
return BuildURL(Endpoint, "/lights/states")
|
||||||
}
|
}
|
||||||
|
57
lights.go
57
lights.go
@ -15,6 +15,44 @@ const (
|
|||||||
type (
|
type (
|
||||||
Status string
|
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 {
|
State struct {
|
||||||
Power string `json:"power,omitempty"`
|
Power string `json:"power,omitempty"`
|
||||||
Color Color `json:"color,omitempty"`
|
Color Color `json:"color,omitempty"`
|
||||||
@ -109,6 +147,25 @@ func (c *Client) Toggle(selector string, duration float64) (*Response, error) {
|
|||||||
return s, nil
|
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) {
|
func (c *Client) PowerOff(selector string) (*Response, error) {
|
||||||
return c.SetState(selector, State{Power: "off"})
|
return c.SetState(selector, State{Power: "off"})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user