From 76fe39c2dbba38ae0c8797b3e2d0b5e0ed9b0c2b Mon Sep 17 00:00:00 2001 From: Sebastian Kroczek Date: Wed, 15 Dec 2021 19:02:49 +0100 Subject: [PATCH] Adds method that allows to identify a light optically (#46) * Adds method IdentifyLight to the Bridge that allows to identify a light optically by id * Adds test for IdentifyLight --- bridge.go | 33 +++++++++++++++++++++++++++++++++ huego_test.go | 6 ++++++ light_test.go | 18 ++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/bridge.go b/bridge.go index 075eb33..f72a131 100644 --- a/bridge.go +++ b/bridge.go @@ -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) diff --git a/huego_test.go b/huego_test.go index c1188ff..ae83f26 100644 --- a/huego_test.go +++ b/huego_test.go @@ -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"), diff --git a/light_test.go b/light_test.go index 26035a6..03608fc 100644 --- a/light_test.go +++ b/light_test.go @@ -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