add SetDelta function

This commit is contained in:
Ryan Cavicchioni 2020-03-07 20:02:37 -06:00
parent e39d81f6cc
commit 05566f480f
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 55 additions and 0 deletions

View File

@ -185,3 +185,26 @@ func (c *Client) listLights(selector string) (*http.Response, error) {
return resp, nil
}
func (c *Client) stateDelta(selector string, delta StateDelta) (*http.Response, error) {
var (
err error
j []byte
req *http.Request
resp *http.Response
)
if j, err = json.Marshal(delta); err != nil {
return nil, err
}
if req, err = c.NewRequest("POST", EndpointStateDelta(selector), bytes.NewBuffer(j)); 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))
}
EndpointStateDelta = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state/delta", selector))
}
EndpointListLights = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s", selector))
}

View File

@ -62,6 +62,16 @@ type (
Fast bool `json:"fast,omitempty"`
}
StateDelta struct {
Power *string `json:"power,omitempty"`
Duration *float64 `json:"duration,omitempty"`
Infrared *float64 `json:"infrared,omitempty"`
Hue *float64 `json:"hue,omitempty"`
Saturation *float64 `json:"saturation,omitempty"`
Brightness *float64 `json:"brightness,omitempty"`
Kelvin *int `json:"kelvin,omitempty"`
}
StateWithSelector struct {
State
Selector string `json:"selector"`
@ -128,6 +138,25 @@ func (c *Client) SetStates(selector string, states States) (*Response, error) {
return s, nil
}
func (c *Client) StateDelta(selector string, delta StateDelta) (*Response, error) {
var (
err error
s *Response
resp *http.Response
)
if resp, err = c.stateDelta(selector, delta); 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) Toggle(selector string, duration float64) (*Response, error) {
var (
err error