add SetState method

This commit is contained in:
Ryan Cavicchioni 2020-02-29 00:08:39 -06:00
parent cd04dd47b6
commit 2fbfe9cae8
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 27 additions and 0 deletions

View File

@ -74,6 +74,20 @@ func (s *Client) SetState(selector string, state State) ([]Result, error) {
return res, nil
}
func (s *Client) SetStates(states States) ([]Result, error) {
j, err := json.Marshal(states)
if err != nil {
return nil, err
}
res, err := s.Request("PUT", EndpointStates(), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
return res, nil
}
func (s *Client) Toggle(selector string, duration float64) ([]Result, error) {
j, err := json.Marshal(&Toggle{Duration: duration})
if err != nil {

View File

@ -17,6 +17,9 @@ var (
EndpointState = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector))
}
EndpointStates = func() string {
return BuildURL(Endpoint, "/lights/states")
}
EndpointToggle = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
}

View File

@ -18,6 +18,16 @@ type (
Fast bool `json:"fast,omitempty"`
}
StateWithSelector struct {
State
Selector string `json:"selector"`
}
States struct {
States []StateWithSelector `json:"states",omitempty`
Defaults State `json:"defaults",omitempty`
}
Client struct {
token string
Client *http.Client