Browse Source

add forgetting devices to hue2 (untested yolo-deploy).

asmodeus
Gisle Aune 1 year ago
parent
commit
c4b391db66
  1. 11
      internal/drivers/hue2/bridge.go
  2. 32
      internal/drivers/hue2/client.go
  3. 4
      internal/drivers/hue2/driver.go

11
internal/drivers/hue2/bridge.go

@ -619,3 +619,14 @@ func (b *Bridge) SearchDevices(ctx context.Context, timeout time.Duration) ([]mo
// Return said devices.
return newDevices, nil
}
func (b *Bridge) Forget(ctx context.Context, device models.Device) error {
b.mu.Lock()
resource := b.resources[device.InternalID]
b.mu.Unlock()
if resource == nil || resource.LegacyID == "" {
return errors.New("resource not found")
}
return b.client.legacyDelete(ctx, resource.LegacyID, nil)
}

32
internal/drivers/hue2/client.go

@ -305,6 +305,38 @@ func (c *Client) legacyPost(ctx context.Context, resource string, body interface
return json.NewDecoder(res.Body).Decode(target)
}
func (c *Client) legacyDelete(ctx context.Context, resource string, target interface{}) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ch:
defer func() {
c.ch <- struct{}{}
}()
}
if c.token != "" {
resource = c.token + "/" + resource
}
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s/api/%s", c.host, resource), nil)
if err != nil {
return err
}
res, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer res.Body.Close()
if target == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(target)
}
func reqBody(body interface{}) (io.Reader, error) {
if body == nil {
return nil, nil

4
internal/drivers/hue2/driver.go

@ -100,6 +100,10 @@ func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models
return d.ensureBridge(bridge).Run(ctx, ch)
}
func (d *Driver) ForgetDevice(ctx context.Context, bridge models.Bridge, device models.Device) error {
return d.ensureBridge(bridge).Forget(ctx, device)
}
func (d *Driver) ensureBridge(info models.Bridge) *Bridge {
d.mu.Lock()
for _, bridge := range d.bridges {

Loading…
Cancel
Save