Skip to content

Commit

Permalink
Adds method that allows to identify a light optically (#46)
Browse files Browse the repository at this point in the history
* Adds method IdentifyLight to the Bridge that allows to identify a light optically by id

* Adds test for IdentifyLight
  • Loading branch information
skroczek authored Dec 15, 2021
1 parent f543730 commit 76fe39c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
33 changes: 33 additions & 0 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,39 @@ func (b *Bridge) GetLightContext(ctx context.Context, i int) (*Light, error) {
return light, nil
}

// IdentifyLight allows identifying a light
func (b *Bridge) IdentifyLight(i int) (*Response, error) {
return b.IdentifyLightContext(context.Background(), i)
}

// IdentifyLightContext allows identifying a light
func (b *Bridge) IdentifyLightContext(ctx context.Context, i int) (*Response, error) {

var a []*APIResponse

url, err := b.getAPIPath("/lights/", strconv.Itoa(i), "/state")
if err != nil {
return nil, err
}
res, err := put(ctx, url, []byte(`{"alert":"select"}`))
if err != nil {
return nil, err
}

err = unmarshal(res, &a)
if err != nil {
return nil, err
}

resp, err := handleResponse(a)
if err != nil {
return nil, err
}

return resp, nil

}

// SetLightState allows for controlling one light's state
func (b *Bridge) SetLightState(i int, l State) (*Response, error) {
return b.SetLightStateContext(context.Background(), i, l)
Expand Down
6 changes: 6 additions & 0 deletions huego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func init() {
path: path.Join(username, "/lights/1/state"),
data: `[{"success":{"/lights/1/state/bri":200}},{"success":{"/lights/1/state/on":true}},{"success":{"/lights/1/state/hue":50000}}]`,
},
{
// Second route for identifying light testing
method: "PUT",
path: path.Join(username, "/lights/2/state"),
data: `[{"success":{"/lights/2/state/alert":"select"}}]`,
},
{
method: "PUT",
path: path.Join(username, "/lights/1"),
Expand Down
18 changes: 18 additions & 0 deletions light_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ func TestGetLight(t *testing.T) {

}

func TestIdentifyLight(t *testing.T) {
b := New(hostname, username)
id := 2
resp, err := b.IdentifyLight(id)
if err != nil {
t.Fatal(err)
} else {
t.Logf("Light %d identified", id)
for k, v := range resp.Success {
t.Logf("%v: %s", k, v)
}
}

b.Host = badHostname
_, err = b.IdentifyLight(id)
assert.NotNil(t, err)
}

func TestSetLight(t *testing.T) {
b := New(hostname, username)
id := 1
Expand Down

0 comments on commit 76fe39c

Please sign in to comment.